EXP No.
3
Implementation of Client-Server communication using Socket
Programming and TCP as transport layer protocol
Aim: Client sends a string to the server using tcp protocol. The server
reads the data and returns it to the client, which then displays the data.
Algorithm
Client
1. Create socket
2. Connect the socket to the server
3. Read the read from the standard input and send it
to the server using socket
4. Read data from the socket and display it on the
standard output
[Link] the socket
Server
1. Create listening socket
2. bind IP address and port number to the socket
3. listen for incoming requests on the listening socket
4. accept the incoming request
5. connection socket is created when accept returns
6. Read the string using the connection socket from the client
7. Display the data
8. close the connection socket
9. close the listening socket
Program
tcpc.c
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
char buf[100];
int k;
int sock_desc;
struct sockaddr_in client;
sock_desc=socket(AF_INET,SOCK_STREAM,0);
if(sock_desc==-1)
printf("Error in socket creation");
client.sin_family=AF_INET;
client.sin_addr.s_addr=INADDR_ANY;
client.sin_port=3003;
k=connect(sock_desc,(struct sockaddr*)&client,sizeof(client));
if(k==-1)
printf("Error in connecting to server");
printf("\nEnter data to be send:");
fgets(buf,100,stdin);
k=send(sock_desc,buf,100,0);
printf("Error in sending");
close(sock_desc);
return 0;
tcps.c
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
char buf[100];
int k;
socklen_t len;
int sock_desc,temp_sock_desc;
struct sockaddr_in server,client;
sock_desc=socket(AF_INET,SOCK_STREAM,0);
if(sock_desc==-1)
printf("Error in socketcreation");
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=3003;
client.sin_family=AF_INET;
client.sin_addr.s_addr=INADDR_ANY;
client.sin_port=3003;
k=bind(sock_desc,(struct sockaddr*)&server,sizeof(server));
if(k==-1)
printf("Error in binding");
k=listen(sock_desc,5);
if(k==-1)
printf("Error in listening");
len=sizeof(client);
temp_sock_desc=accept(sock_desc,(struct sockaddr*)&client,&len);
if(temp_sock_desc==-1)
printf("Error in temporary socket creation");
k=recv(temp_sock_desc,buf,100,0);
if(k==-1)
printf("Error in receiving");
printf("Message got from client is : %s",buf);
close(temp_sock_desc);
return 0;