Socket Programming (Programming a Simple Client) in C++


All right now let’s move to the second part of the basic client-server programming, that is programming the client. It is more similar to programming a server but it’s little bit easy. Because we only have to handle two system calls here; socket() and connect(). But if you did not read the post about programming a server then this post may confusing since I’ll not cover anything state there, so first read it from here .
Since clients are not listening to incoming connections hence not accepting any connections listen() and accept() system calls are not needed for client.

Socket()
Exact system call is
int socket (int domain, int type, int protocol)
So, it is same as server program’s socket system call.

#include <sys/types.h>
#include <sys/socket.h>

int iSocketFD;
iSocketFD = socket(AF_INET, SOCK_STREAM, 0);
if(iSocketFD == -1)
{
 perror("Error while creating socket!");
 exit(1);
}

Connect()
Exact connect () system call is

int connect(int sockfd, struct sockaddr *serv_addr, int addrlen)

int sockfd :- Socket descriptor returned by socket() system call
struct sockaddr *serv_addr :- this struct holds the information (IP and port number) about the destination server.
int addrlen :- Length of the serv_addr

If connect () fails due to any reason it will send -1.

Nothing much to do here also, you have already practiced how to fill above three parameters in server program’s bind () system call. Sockaddr struct can be set via filling values to sockaddr_in struct and later casting to Sockaddr.
struct sockaddr_in {

     short sin_family; // Address family

     unsigned short sin_port; // Port number

     struct in_addr sin_addr; // Internet address

     char sin_zero[8]; // Same size as struct sockaddr

};


As in server program let's set values for sockaddr_in

//create a variable of type struct sockaddr_in
struct sockaddr_in sServerAddress; 

int iPortNumber = 2500;

sServerAddress.sin_family = AF_INET; 

/*I’ll going to run both server and client on my local machine.
 If you wish to run your server on remote machine then use
 that machine’s IP address here. htons () function used to
 convert address to network byte order */
sServerAddress.sin_addr.s_addr = inet_addr("127.0.0.1");  

//convert integer port number in to network byte order.
sServerAddress.sin_port = htons(iPortNumber); 

Complete connect system call is listed in following code segment
iPortNumber = 2500;
sServerAddress.sin_family = AF_INET;
sServerAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
sServerAddress.sin_port = htons(iPortNumber); 
 
if (connect(iSocketFD,(sockaddr*)&sServerAddress,sizeof(sServerAddress)) == -1)
{
 perror("Error while connecting!");
 exit(1);
}

Read and Write

This is also similar to read and write we have done in server program. Just for fun let’s prompt client to enter some message and send it to server.

// Buffer to store send and receive information
char zBuffer[256]; 

bzero(zBuffer,256); // Empty the buffer
printf("Enter Message : "); // Prompt user to enter message

// get user input and put it to the buffer
fgets(zBuffer,255,stdin); 

//write number of bytes (specified by third argument) to the socket
int iReturnValue = write(iSocketFD,zBuffer,strlen(zBuffer));

bzero(zBuffer,256); // Empty the buffer

// Read number of bytes from the socket and put to the buffer.
int iReturnValue2 = read(iSocketFD,zBuffer,255); 

Complete code for reading and writing is given below.
char zBuffer[256];
bzero(zBuffer,256);

printf("Enter Message : ");
fgets(zBuffer,255,stdin);
 
int iReturnValue = write(iSocketFD,zBuffer,strlen(zBuffer));
if(iReturnValue < 0)
 perror("Error while writing to the server!");
 
bzero(zBuffer,256);
int iReturnValue2 = read(iSocketFD,zBuffer,255);
if(iReturnValue2 < 0)
 perror("Error while reading from socket!");

printf("Client Received a Message: %s\n", zBuffer); 
To test the client you need to first run the server code and then client code. Server will first prompt that Client’s IP address. Meantime client program will prompt user to enter the message and after entering the message it will send to the server. Server will respond to that message with welcome message. Complete client program can be download from here .

No comments:

Post a Comment