TCP Chat Client Server Program
TCP Chat Client Server Program
TCP Chat Client Server Program
If we are creating a connection between client and server using TCP then it
has few functionality like, TCP is suited for applications that require high
reliability, and transmission time is relatively less critical. It is used by
other protocols like HTTP, HTTPs, FTP, SMTP, Telnet. TCP rearranges data
packets in the order specified. There is absolute guarantee that the data
transferred remains intact and arrives in the same order in which it was
sent. TCP does Flow Control and requires three packets to set up a socket
connection, before any user data can be sent. TCP handles reliability and
congestion control. It also does error checking and error recovery.
Erroneous packets are retransmitted from the source to the destination.
TCP Client –
SERVERSIDE PROGRAM
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
// Driver function
int main()
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
CLIENTSIDE PROGRAM:
filename :tcpchatclient.c
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#define MAX 80
#define PORT 8080
#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", buff);
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
}
int main()
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
sayantan@localhost:~$ ./tcpchatserver
Socket successfully created..
Socket successfully binded..
Server listening..
server acccept the client...
From client: Hi ! How are you ?
To client : I am fine brother.
sayantan@localhost:~$ ./tcpchatclient
Socket successfully created..
connected to the server..
Enter the string : Hi ! How are you ?
From Server : I am fine brother.
Enter the string :
filename:tcpchatserver.c
filename:tcpchatclient.c
tcp chat server is running .It accepted the client and started
chatting