Hello, I was wondering how I could connect to a server using the client that I wrote below, that requires a username and password. I'm a little confused when it comes to reading the man pages.

#include <arpa/inet.h>
#include <errno.h>
#include <math.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <values.h>

int errno;  /* Required for perror and error returned values */

int main(int argc, char **argv) {

  int socknum, i, z, *myipaddr, numconns, newpid, done, received, recvlen;
  int msglen, msglen2, retval, conn, r;
  int msgnum;
  unsigned int remoteaddrlen;
  char hostname[80];
  unsigned int integer[11];
  struct sockaddr_in mysockaddr;
  struct sockaddr_in remoteaddr;

  struct hostent *myhostent = 0;

  unsigned short int *sbuf = (unsigned short int *)integer;
  sbuf[0] = htons(44);
  sbuf[1] = htons(0);
for (z=1;z<11;z++){
scanf("%d",&(integer[z]));
integer[z] = htonl(integer[z]);
}
  /* Open main socket */
  socknum = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

  if (socknum == -1) {
    perror("Socket creation failed");
    return -1;
  }
  
  mysockaddr.sin_family = AF_INET;
  mysockaddr.sin_port = htons(53432);

  myhostent = gethostbyname("/*the host name*/");
  if (myhostent == 0) {
    perror("gethostbyname failed");
    return -2;
  }

  /* Set up so address can be reused quickly after socket is taken down */
  /* Otherwise, annoying wait reqired after each server deactivation */
  i = 1;
  if (setsockopt(socknum, SOL_SOCKET, SO_REUSEADDR, (void *)&i, sizeof(int))==-1) {
    perror("setsockopt failed");
    return -99;
  }

  myipaddr = (int *)myhostent->h_addr_list[0];
  mysockaddr.sin_addr.s_addr = *myipaddr;  // Could be IN_ADDR_ANY

  /* Make connection */
  retval = connect(socknum, (struct sockaddr*)&mysockaddr, sizeof(struct sockaddr));
  if (retval == -1) {
    perror("Connection  failed");
    return -3;
  }

  /* Handle messages from remote */

     retval = send(socknum,integer,44,0);
     if (retval == -1) {
	 perror("Send  failed");
	 return -4;
     }



	 retval = recv(socknum,sbuf,sizeof(unsigned short int)*2,0);
         if (retval <= 0) {
	    perror("Recv failed");
	 }

	  printf("I got: %d,%d\n", sbuf[0], sbuf[1]);  // Print the message here 
	  
	  retval = recv(socknum,integer,sizeof(unsigned int),0);
         if (retval <= 0) {
	    perror("Recv failed");
	 }

	  printf("I got for integer: %d\n", *integer);  // Print the message here 

  close(conn);
  return 0;
}

After connecting to the server there is probably some sort of communication protocol strings the client has to pass to the server in order for the server to authenticate it. What strings that is will depend on the server. You will have to read the server's documentation to find out how to do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.