Socket Communication

To run the socket programsin Linux - client.cpp and server.cpp (see Appendix for code),

1. You need to install g++ compiler for compiling C++ programs. You need to type the following command in the terminal window:

2. After successful installation open one more terminal as shown below:

3. Now copy the server.cpp program in one terminal window, and client.cpp in another, as shown below:

4. Come out of the vi editors, compile and run the server program first. Then compile and run the client.

APPENDIX

1. SERVER.CPP

//////////////////////////////////////////////////////////////////////////////////

// server.cpp: Server receiving and processing requests of Clients, and sending

// back results using Socket Communication

//

// version: 1.0

//

// Language: C++

// Platform: Red Hat Linux, Version 9

// Application: Simple Client-Server Interaction, Networking Lab, Spring 2006

// Author: Rajika Tandon, Manipal University ()

// Source: Dr. Balachandra

///////////////////////////////////////////////////////////////////////////////////

#include<iostream

#include<stdlib.h

#include<sys/types.h

#include<sys/socket.h

#include<netinet/in.h

#include<unistd.h

#include<string.h

#define MAX 50

using namespace std;

main()

{

intsockfd,newsockfd,actuallen,retval;

intrecdbytes,sentbytes;

socklen_t n;

structsockaddr_inserveraddr,clientaddr;

charbuf[MAX];

sockfd=socket(AF_INET,SOCK_STREAM,0);

if(sockfd==-1)

{ cout<" Fail ";

exit(1);

}

serveraddr.sin_family=AF_INET;

serveraddr.sin_port=htons(5040);

serveraddr.sin_addr.s_addr=htonl(INADDR_ANY);

retval=bind(sockfd,(structsockaddr*)&serveraddr,sizeof(serveraddr));

if(retval==-1)

{

cout<"\n error bind ";

close(sockfd);

exit(1);

}

retval=listen(sockfd,1);

if(retval==-1)

{

cout<" listen fail";

close(sockfd);

}

newsockfd=accept(sockfd,(structsockaddr*)&clientaddr,&n);

if(newsockfd==-1)

{

cout<" Accept fail";

close(sockfd);

exit(1);

}

recdbytes=recv(newsockfd,buf,sizeof(buf),0);

if(recdbytes==-1)

{

cout<"\n receive fail";

close(newsockfd);

close(sockfd);

exit(1);

}

cout< "\n Received: " < buf < "\n";

cout<"\n Now sending \"Hi\" \n";

strcpy(buf,"Hi");

sentbytes=send(newsockfd,buf,sizeof(buf),0);

if(sentbytes==-1)

{

cout<" send fail";

close(sockfd);

close(newsockfd);

exit(1);

}

close(sockfd);

close(newsockfd);

}

2. CLIENT.CPP

//////////////////////////////////////////////////////////////////////////////////

// client.cpp: Client sending requests to Server and receiving results

// from it using Socket Communication

//

// version: 1.0

//

// Language: C++

// Platform: Red Hat Linux, Version 9

// Application: Simple Client-Server Interaction, Networking Lab, Spring 2006

// Author: Rajika Tandon, Manipal University ()

// Source: Dr. Balachandra

///////////////////////////////////////////////////////////////////////////////////

#include<iostream

#include<stdlib.h

#include<sys/types.h

#include<sys/socket.h

#include<netinet/in.h

#include<unistd.h

#include<arpa/inet.h

#include<string.h

#define MAX 50

using namespace std;

main()

{

intsockfd,retval;

intrecdbytes,sentbytes;

socklen_t n;

structsockaddr_inserveraddr;

charbuf[MAX];

sockfd=socket(AF_INET,SOCK_STREAM,0);

if(sockfd==-1)

{ cout<" Fail ";

exit(1);

}

serveraddr.sin_family=AF_INET;

serveraddr.sin_port=htons(5040);

serveraddr.sin_addr.s_addr=inet_addr("127.0.0.1");

retval=connect(sockfd,(structsockaddr*)&serveraddr,sizeof(serveraddr));

if(retval==-1)

{

cout<"\n connection error ";

close(sockfd);

exit(1);

}

cout < "\n Enter a string to send to server : ";

cinbuf;

coutendl;

sentbytes=send(sockfd,buf,sizeof(buf),0);

if(sentbytes==-1)

{

cout<" send fail";

close(sockfd);

exit(1);

}

recdbytes=recv(sockfd,buf,sizeof(buf),0);

if(recdbytes==-1)

{

cout<"\n receive fail";

close(sockfd);

exit(1);

}

cout<"\n Received from server: "< buf < "\n";

close(sockfd);

}

Page | 1

Rajika Tandon