0% found this document useful (0 votes)
32 views

TCP Program Client and Server

Uploaded by

ssnitanu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

TCP Program Client and Server

Uploaded by

ssnitanu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Client

First run server.c by gcc server.c -o server

then run server by ./server

After that start client by gcc client.c -o client

then run client by ./client

*/

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<netdb.h>

#include<strings.h>

#include<unistd.h>

int main()

int clientsocket,port; //clientsocket is the socket descriptor , port is the port number

struct sockaddr_in serveraddr; //creating a structure of type sockaddr_in for server

socklen_t len; //creating a variable to store the length of the server address

char message[50];

clientsocket=socket(AF_INET,SOCK_STREAM,0);//creating a socket

bzero((char*)&serveraddr,sizeof(serveraddr));//initializing the server address to zero

len=sizeof(serveraddr); //storing the length of the server address in len

serveraddr.sin_family=AF_INET; //setting the family of the server address to AF_INET

printf("Enter the port number ");


scanf("%d",&port);

serveraddr.sin_port=htons(port);//setting the port number of the server address to port

printf("\nTrying to connect to the server.\n");

connect(clientsocket,(struct sockaddr*)&serveraddr,sizeof(serveraddr));//connecting to the server

printf("\nConnected to the server.\n");

printf("\nSending message for server connection");

send(clientsocket,"HI,IAM CLIENT...",sizeof("HI,IAM CLIENT..."),0);//sending the message to the server

printf("\nReceiving message from server.\n");

recv(clientsocket,message,sizeof(message),0);//receiving the message from the server

printf("\nMessage received.\t%s\n",message);

close(clientsocket);//closing the socket

/*

s6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/TCP$ gcc client.c -o client

s6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/TCP$ ./client

Enter the port number 6000

Trying to connect to the server.

Connected to the server.

Sending message for server connection

Receiving message from server.


Message received. YOUR MESSAGE RECEIVED.

s6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/TCP$

*/

Server

/*

First run server.c by gcc server.c -o server

Then run server by ./server

After that start client by gcc client.c -o client

Then run client by ./client

*/

#include<stdio.h>

#include<string.h>

#include<sys/socket.h>

#include<stdlib.h>

#include<netdb.h>

#include<unistd.h>

Int main()

Int serversocket,clientsocket,port; //clientsocket is the socket descriptor , port is the port number

Struct sockaddr_in serveraddr,clientaddr; //creating a structure of type sockaddr_in for server

Socklen_t len; //creating a variable to store the length of the server address

Char message[50]; //

Struct serveraddr; //creating a structure of type sockaddr_in for server


Serversocket=socket(AF_INET,SOCK_STREAM,0); //creating a socket

Bzero((char*)&serveraddr,sizeof(serveraddr));//initializing the server address to zero

Serveraddr.sin_family=AF_INET;//setting the family of the server address to AF_INET

Printf(“Enter the port number “);

Scanf(“%d”,&port);

Serveraddr.sin_port=htons(port); //setting the port number of the server address to port

Serveraddr.sin_addr.s_addr=INADDR_ANY; //setting the IP address of the server address to


INADDR_ANY

Bind(serversocket,(struct sockaddr*)&serveraddr,sizeof(serveraddr)); //binding the server address to


the socket

Bzero((char*)&clientaddr,sizeof(clientaddr)); //initializing the client address to zero

Len=sizeof(clientaddr); //storing the length of the client address in len

Listen(serversocket,5); //listening to the socket, 5 is the number of clients that can connect to the
server

Printf(“\nWaiting for client connection\n”);

Printf(“\nhai:”);

Clientsocket=accept(serversocket,(struct sockaddr*)&clientaddr,&len);//accepting the client


connection

Printf(“\nClient connectivity received.\n”);

Printf(“\nReading message from the client.\n”);

Read(clientsocket,message,sizeof(message));//reading the message from the client

Printf(“\nThe client has sent.%s”,message);

Printf(“\nSending message to the client.\n”);

Write(clientsocket,”YOUR MESSAGE RECEIVED.”,sizeof(“YOUR MESSAGE RECEIVED.”));//sending the


message to the client
Close(clientsocket);//closing the client socket

Close(serversocket);//closing the server socket

/*

OUTPUT

S6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/TCP$ gcc server.c -o server

S6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/TCP$ ./server

Enter the port number 6000

Waiting for client connection

Hai:

Client connectivity received.

Reading message from the client.

The client has sent.HI,IAM CLIENT…

Sending message to the client.

S6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/TCP$ ^C

*/

You might also like