Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

APPLICATIONS OF TCP

TCP CHAT
Aim:
To implement the chat client and server using tcp sockets.

Concepts involved:
A TCP socket is an endpoint defined by an IP address and a port in the context of either a
particular TCP connection or the listening state. An echo server is usually an application which is
used to test if the connection between a client and a server is successful. It consists of a server
which sends back whatever text the client sent. A client-server is any environment in which you
have a main node(server) to which other nodes connect to, usually to request some information.
Single Threaded servers are servers which use one main thread to handle all the requests. Usually
these are used to handle very short requests such as synchronizing computer clocks. These are
known as Iterative servers. Multi threaded servers are which use one or more threads per client.
This is usually the case with most application server sand is good for scalability, It also allows
multiple points at any point in time. These are known Concurrent servers.
Server:
Algorithm:
1. Create a passive socket using socket function.
2. Clear the content of the screen socket address in bzero function.
3. Set the socket family address and port number using byte manipulation function.
4. Assign a socket address structure and port address to the server side sockets using bind()
function.
5. Convert the passive socket into active socket and set the number of connection using listen
function.
6. Accept the client connection using accept() function.
7. Call the TCP chat function
8. Close the socket connection or logical using close() function.

Code:
Server:
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#define MAX 80
#define PORT 6600
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for(;;)
{
bzero(buff,MAX);
read(sockfd,buff,sizeof(buff));
printf("From client:%sTo client:",buff);
bzero(buff,MAX);
n=0;
while((buff[n++]=getchar())!='\n');
write(sockfd,buff,sizeof(buff));
if(strncmp("exit",buff,4)==0)
{
printf("Server exit..\n");
break;
}
}
}
int main()
{
int sockfd,connfd,len;
struct sockaddr_in servaddr,cli;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf("socket creation failed..\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(PORT);
if((bind(sockfd,(SA *)&servaddr,sizeof(servaddr)))!=0)
{
printf("socket bind failed..\n");
exit(0);
}
else
printf("Socket successfully binded..\n");
if((listen(sockfd,5))!=0)
{
printf("listen failed..\n");
exit(0);
}
else
printf("Server listening..\n");
len=sizeof(cli);
connfd=accept(sockfd,(SA *)&cli,&len);
if(connfd<0)
{
printf("server accept failed..\n");
exit(0);
}
else
printf("server accept the client..\n");
func(connfd);
close(sockfd);
}

Client:
Algorithm:
1. Create a passive socket using socket function.
2. Close the content of the screen socket address in bzero function.
3. Set the socket family address and port number using byte manipulation function.
4. Get the connection with server using connect() function.
5. Call the TCP chat function.
6. Close the socket connection or logical using close() function.

Code:
Client:
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#define MAX 80
#define PORT 6600
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for(;;)
{
bzero(buff,sizeof(buff));
printf("Enter the string:");
n=0;
while((buff[n++]=getchar())!='\n');
write(sockfd,buff,sizeof(buff));
bzero(buff,sizeof(buff));
read(sockfd,buff,sizeof(buff));
printf("From server:%s\n",buff);
write(sockfd,buff,sizeof(buff));
if(strncmp("exit",buff,4)==0)
{
printf("client Exit...\n");
break;
}
}
}
main()
{
int sockfd,connfd;
struct sockaddr_in servaddr,cli;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf("socket creation failed");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=htons(PORT);
if(connect(sockfd,(SA *)&servaddr,sizeof(servaddr))!=0)
{
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");
func(sockfd);
close(sockfd);
}

You might also like