Write a Program to implement inter process communication(chat) using stream sockets with thehelp of socket interfaces provided TCP sockets.

Algorithm
TCP Server
1. Create a socket
2. Bind it to the operating system.
3. Listen over it.
4. Accept connections.
5. Receive data from client and  send it back to client.
6. Close the socket.
TCP Client

1.Create a socket.
2.connect to the server using connect().
3.send data to server and receive data from the server.
4.Close the socket.

Client

//TCP Client

#include<sys/socket.h>
#include<stdio.h>
#include<string.h>
#include<netdb.h>
#include<stdlib.h>
int main()
{
char buf[100];
int k;
int sock_desc;
struct sockaddr_in client;
memset(&client,0,sizeof(client));
sock_desc=socket(AF_INET,SOCK_STREAM,0);
if(sock_desc==-1)
{
printf(“Error in socket creation”);
exit(1);
}
client.sin_family=AF_INET;
client.sin_addr.s_addr=INADDR_ANY;
client.sin_port=3002;
k=connect(sock_desc,(struct sockaddr*)&client,sizeof(client));
if(k==-1)
{
printf(“Error in connecting to server”);
exit(1);
}
while(1)
{
printf(“\nEnter data to be send to server: “);
fgets(buf,100,stdin);
if(strncmp(buf,”end”,3)==0)//Use “end” to end communication with server
break;
k=send(sock_desc,buf,100,0);
if(k==-1)
{
printf(“Error in sending”);
exit(1);
}
k=recv(sock_desc,buf,100,0);
if(k==-1)
{
printf(“Error in receiving”);
exit(1);
}
printf(“Message got from server is : %s”,buf);
}
close(sock_desc);
exit(0);
return 0;
}

Server

//TCP server

#include<sys/socket.h>
#include<stdio.h>
#include<string.h>
#include<netdb.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;
memset(&server,0,sizeof(server));
memset(&client,0,sizeof(client));
sock_desc=socket(AF_INET,SOCK_STREAM,0);
if(sock_desc==-1)
{
printf(“Error in socket creation”);
exit(1);
}
server.sin_family=AF_INET;
server.sin_addr.s_addr=inet_addr(“127.0.0.1”);
server.sin_port=3002;
k=bind(sock_desc,(struct sockaddr*)&server,sizeof(server));
if(k==-1){
printf(“Error in binding”);
exit(1);
}
k=listen(sock_desc,20);
if(k==-1)
{
printf(“Error in listening”);
exit(1);
}
len=sizeof(client);//VERY IMPORTANT

temp_sock_desc=accept(sock_desc,(struct sockaddr*)&client,&len);//VERY //IMPORTANT
if(temp_sock_desc==-1)
{
printf(“Error in temporary socket creation”);
exit(1);
}
while(1)
{
k=recv(temp_sock_desc,buf,100,0);
if(k==-1)
{
printf(“Error in receiving”);
exit(1);
}
printf(“Message got from client is : %s”,buf);
printf(“\nEnter data to be send to client: “);
fgets(buf,100,stdin);
if(strncmp(buf,”end”,3)==0)
break;
k=send(temp_sock_desc,buf,100,0);
if(k==-1)
{
printf(“Error in sending”);
exit(1);
}
}
close(temp_sock_desc);
exit(0);
return 0;
}
Output obtained

[root@localhost ~]$ gcc -o c tcpc.c
[root@localhost ~]$ ./c

Enter data to be send to server: hi
Message got from server is : how are u?

Enter data to be send to server: i am fine
Message got from server is : i am fine

Enter data to be send to server: end
[root@localhost ~]$

Server

[root@localhost ~]$ gcc -o s tcps.c
[root@localhost ~]$ ./s
Message got from client is : hi

Enter data to be send to client: how are u?
Message got from client is : i am fine

Enter data to be send to client: end
[root@localhost ~]$

Leave a comment