7/21/2017
Echo client-server application
• 1. The Client reads a line of text from its standard
input and writes the line to the server.
Basic TCP Client-Server • 2. The server reads the line from its network input
programming on UNIX and echoes the line back to the client.
• 3. The client reads the echoed line and prints it on its
Harshad B. Prajapati standard output.
Associate Professor
Information Technology Department, stdin
fgets
read write
Dharmsinh Desai University, Nadiad stdout
TCP
client
TCP
server
write read
fputs
echoTCPServer.c echoTCPServer.c
/*This server program is concurrent*/ int main(){
#include <sys/types.h> /*Create socket*/
#include <sys/socket.h> if((listensd=socket(AF_INET,SOCK_STREAM,0))<0){
#include <stdio.h> fprintf(stderr,"Cannot create socket\n");
#include <netinet/in.h> exit(-1);
#define MAXLINESIZE 100 }
#define SERV_PORT 5555 /*Initialize socket address structure*/
int listensd,clientsd;
char buffer[MAXLINESIZE+1]; bzero(&servaddr,sizeof(servaddr));
struct sockaddr_in servaddr; servaddr.sin_family=AF_INET;
int noBytesRead=0; servaddr.sin_port=htons(SERV_PORT);
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
/*this function will server client that connects*/
void processClient(int);
echoTCPServer.c echoTCPServer.c
/*bind socket address to the socket*/ for(;;){
/*wait for client connection*/
if(bind(listensd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0){ clientsd=accept(listensd,(struct sockaddr*)NULL,NULL);
fprintf(stderr,"Error in bind\n"); if(fork()==0){
exit(-1); /*close listening socket in child. So that reference count
} remains one. The child serves the client. It does not need listening socket
/*Make the socket listening socket*/ to do this. */
close(listensd);
if(listen(listensd,5)<0){ /*server client*/
fprintf(stderr,"Error in listen\n"); processClient(clientsd);
exit(-1); /*close connected socket*/
} close(clientsd);
exit(0);
}
1
7/21/2017
echoTCPServer.c echoTCPClient.c
/*close connected socket in parent #include <sys/types.h>
so that reference count remains one. #include <sys/socket.h>
*/ #include <stdio.h>
close(clientsd); #include <netinet/in.h>
} #include <string.h>
return 0; #define MAXLINESIZE 100
} #define SERV_PORT 5555
void processClient(int clientsd){
/*read message from client and send back*/ int main(int argc,char** argv){
while((noBytesRead=read(clientsd,buffer,sizeof(buffer)))>0) int connectsd;
write(clientsd,buffer,noBytesRead); char sendBuffer[MAXLINESIZE+1];
} char recvBuffer[MAXLINESIZE+1];
struct sockaddr_in servaddr;
int noBytesRead=0;
echoTCPClient.c echoTCPClient.c
if(argc!=2){ /*assign server address in socket address structure*/
fprintf(stderr,"Usage: %s IP-Address\n",argv[0]); if(inet_pton(PF_INET,argv[1],&servaddr.sin_addr)<=0){
exit(-1); fprintf(stderr,"Error in inet_pton\n");
} exit(-1);
/*Create socket*/ }
if((connectsd=socket(AF_INET,SOCK_STREAM,0))<0){ /*Get connected with the server*/
fprintf(stderr,"Cannot create socket\n"); if(connect(connectsd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0){
exit(-1); fprintf(stderr,"Error in connect\n");
} exit(-1);
/*Initialize socket address structure*/ }
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(SERV_PORT);
echoTCPClient.c Echo client-server application
/*Read message from user through keyboard*/ • Compile echoTCPServer.c and echoTCPClient.c using gcc.
for(;gets(sendBuffer)!=NULL;){ • First run the server.
/*Send the message to the server*/
• Run clients and test.
write(connectsd,sendBuffer,strlen(sendBuffer)+1);
if(noBytesRead=read(connectsd,recvBuffer,sizeof(recvBuffer))<0) • Exercises:
exit(0); – Understand how socket states of server and client sockets change.
/*Display what the server sent in reply*/ – Implement iterative version of the server. And test behavior/error
conditions while using multiple clients.
fprintf(stdout,"%s\n",recvBuffer);
} • Important Note: Don’t blindly copy programs from the text
return 0; book. The text book uses wrapper functions and structures.
} These functions and structures start with capital letter (E.g,
Bind rather that bind) and they are placed in user-defined
header file unp.h that you will find included as #include
“unp.h” (not <unp.h>).
2
7/21/2017
Basic TCP Client-Server
programming on UNIX
• We discussed up to 5.7 of chapter 5. TCP
Client-Server Example of the text book.