0% found this document useful (0 votes)
34 views3 pages

Chat Server (TCP) //program To Perform Chat Server

This document contains code for a TCP chat server and client program written in C. The server code creates a socket, binds it to an IP address and port, listens for incoming connections, accepts a connection, and then exchanges messages with the client in a loop. The client code creates a socket, connects to the server, and also exchanges messages in a loop. Both use standard C networking and I/O functions to implement the client-server chat functionality.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

Chat Server (TCP) //program To Perform Chat Server

This document contains code for a TCP chat server and client program written in C. The server code creates a socket, binds it to an IP address and port, listens for incoming connections, accepts a connection, and then exchanges messages with the client in a loop. The client code creates a socket, connects to the server, and also exchanges messages in a loop. Both use standard C networking and I/O functions to implement the client-server chat functionality.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

CHAT SERVER (TCP) //Program to perform chat server

SERVER : #include<stdio.h> #include<sys/socket.h> #include<sys/types.h> #include<string.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdlib.h> #define tcpport 7999 #define ipaddr "127.0.0.1" #define max 50 int main() { int serfd,clntfd; int length; struct sockaddr_in server,client; char buffer[max]; serfd=socket(AF_INET,SOCK_STREAM,0); if(serfd==1) { perror("\nSOCKET CREATION FAILED"); exit(1); } bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(tcpport); inet_pton(AF_INET,ipaddr,&(server.sin_addr)); if(bind(serfd,(struct sockaddr*)&server,sizeof(struct sockaddr))>0) { perror("\nBIND ERROR"); close(serfd); exit(1); } if(listen(serfd,1)<0) { perror("\nLISTEN error"); close(serfd); exit(1); } printf("listen success\n"); length=sizeof(client);

clntfd=accept(serfd,(struct sockaddr*)&client,&length); if(clntfd==-1) { perror("ACCEPT FAILED"); exit(1); } printf("\nCLIENT CONNECTED"); while(1) { bzero(buffer,sizeof(buffer)); if(read(clntfd,buffer,sizeof(buffer),0)<0) { perror("\nRead failed"); close(serfd); exit(1); } printf("\n\nReceived message from client is :: %s",buffer); printf("\nMy reply ::"); fgets(buffer,max-1,stdin); if(write(clntfd,buffer,sizeof(buffer),0)<=0) { perror("server send error"); close(serfd); exit(1); } } } CLIENT : #include<stdio.h> #include<sys/socket.h> #include<sys/types.h> #include<string.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdlib.h> #define tcpport 7999 #define ipaddr "127.0.0.1" #define max 50 int main() { int clntfd; int length; struct sockaddr_in server,client; char buffer[max];

clntfd=socket(AF_INET,SOCK_STREAM,0); if(clntfd<0) { perror("\nSOCKET CREATION FAILED"); exit(1); } bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(tcpport); if(inet_pton(AF_INET,ipaddr,&(server.sin_addr))<0) { perror("\nERROR"); close(clntfd); exit(1); } if(connect(clntfd,(struct sockaddr*)&server,sizeof(server))<0) { perror("\nCONNECT FAILED"); close(clntfd); exit(1); } printf("\nConnected"); while(1) { bzero(buffer,sizeof(buffer)); printf("\nEnter your message :"); fgets(buffer,max-1,stdin); if(write(clntfd,buffer,sizeof(buffer),0)<0) { perror("Client send error"); close(clntfd); exit(1); } if(read(clntfd,buffer,sizeof(buffer),0)<=0) { perror("\nClient receive error"); close(clntfd); exit(1); } printf("\nReply from server is :: %s",buffer); } }

You might also like