0% found this document useful (0 votes)
113 views4 pages

Full Duplex Chat Using Tcp/Ip: Name: Pranjal Patel REG. NO.: RA1911003010355

This document contains code for implementing a full duplex chat application using TCP/IP. The server code creates a socket, binds it to a port, listens for connections and forks a child process to handle communication with each client. The client code connects to the server socket, forks a child process and the parent and child can then send and receive messages simultaneously, allowing full duplex communication.

Uploaded by

pranjal patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views4 pages

Full Duplex Chat Using Tcp/Ip: Name: Pranjal Patel REG. NO.: RA1911003010355

This document contains code for implementing a full duplex chat application using TCP/IP. The server code creates a socket, binds it to a port, listens for connections and forks a child process to handle communication with each client. The client code connects to the server socket, forks a child process and the parent and child can then send and receive messages simultaneously, allowing full duplex communication.

Uploaded by

pranjal patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

NAME : PRANJAL PATEL

REG. NO. : RA1911003010355

FULL DUPLEX CHAT USING TCP/IP

SERVER CODE :

#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<unistd.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<string.h>

int main(int argc,char *argv[])


{
int clientSocketDescriptor,socketDescriptor;
struct sockaddr_in serverAddress,clientAddress;
socklen_t clientLength;
char recvBuffer[1000],sendBuffer[1000];
pid_t cpid;
memset(&serverAddress, 0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=htonl(INADDR_ANY);
serverAddress.sin_port=htons(2070);
socketDescriptor=socket(AF_INET,SOCK_STREAM,0);
bind(socketDescriptor,(struct sockaddr*)&serverAddress,sizeof(serverAddress));
listen(socketDescriptor,5);
printf("%s\n","Server is running ...");
clientSocketDescriptor=accept(socketDescriptor,(struct sockaddr*)&clientAddress,&clientLength);
cpid=fork();
if(cpid==0)
{
while(1)
{
memset(&recvBuffer, 0,sizeof(recvBuffer));
/*Receiving the request from client*/
recv(clientSocketDescriptor,recvBuffer,sizeof(recvBuffer),0);
printf("\nCLIENT : %s\n",recvBuffer);
}

}
else
{
while(1)
{
memset(&sendBuffer, 0,sizeof(sendBuffer));
printf("\nType a message here ... ");
/*Read the message from client*/
fgets(sendBuffer,10000,stdin);
/*Sends the message to client*/
send(clientSocketDescriptor,sendBuffer,strlen(sendBuffer)+1,0);
printf("\nMessage sent !\n");

}
}
return 0;
}

SERVER OUTPUT :

CLIENT CODE :

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include "netdb.h"
#include "arpa/inet.h"

int main()
{
int socketDescriptor;
struct sockaddr_in serverAddress;
char sendBuffer[1000],recvBuffer[1000];
pid_t cpid;
memset(&serverAddress, 0,sizeof(serverAddress));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.s_addr=inet_addr("127.0.0.1");
serverAddress.sin_port=htons(2070);
socketDescriptor=socket(AF_INET,SOCK_STREAM,0);
connect(socketDescriptor,(struct sockaddr*)&serverAddress,sizeof(serverAddress));
cpid=fork();
if(cpid==0)
{
while(1)
{
memset(&sendBuffer, 0,sizeof(sendBuffer));
printf("\nType a message here ... ");
/*This function is used to read from server*/
fgets(sendBuffer,10000,stdin);
/*Send the message to server*/
send(socketDescriptor,sendBuffer,strlen(sendBuffer)+1,0);
printf("\nMessage sent !\n");

}
else
{
while(1)
{
memset(&recvBuffer, 0,sizeof(recvBuffer));
/*Receive the message from server*/
recv(socketDescriptor,recvBuffer,sizeof(recvBuffer),0);
printf("\nSERVER : %s\n",recvBuffer);

}
return 0;
}
CLIENT OUTPUT:

You might also like