Wednesday, June 17, 2009

Transfer Control Protocol (TCP)

Posted by Sarjukottapuram

TRANSFER CONTROL PROTOCOL(TCP)


Server

Algorithm

The activity involved in a tcp server are..........
1.Create a socket.
2.Bind it to the operating system.
3.Listen over it.
4.Accept connections.
5.Read/Write processes.
6.Close the socket.

Program

/*Program to demonstrate the creation and usage of tcp socket.
* This module acts as the tcp server which listens to a socket and accepts connection.
* The server initially reads a message from the client and then writes a message to it.
* This module should be compiled using the command. 'c++ tcpserver.cpp' and execute './a.out'.*/

//inculsion.
#include
#include
#include
#include
#include
int main()
{

//variables to store the socket id.
int serversocket,clientsocket;

//variables to store the network host addresses.
sockaddr_in serveraddr,clientaddr;

//variable to store address length.
socklen_t len;
char message[50];

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

//steps to include the host address
bzero((char*)&serveraddr,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(5030);
serveraddr.sin_addr.s_addr=INADDR_ANY;

//binding the socket to the operating system.
bind(serversocket,(sockaddr*)&serveraddr,sizeof(serveraddr));
bzero((char*)&clientaddr,sizeof(clientaddr));
len=sizeof(clientaddr);

//listening over the socket.
listen(serversocket,5);
printf("\nWaiting for client connectivity.\n");

//accepting the connection.
clientsocket=accept(serversocket,(sockaddr*)&clientaddr,&len);
printf("\nClient connectivity received.\n");
printf("\nReading message from the client.\n");

//reading activity.
read(clientsocket,message,sizeof(message));
printf("\nThe client has send:\t%s\n",message);
printf("\nSending message to the client.\n");

//writing activity.
write(clientsocket,"YOUR MESSAGE RECEIVED.",sizeof("YOUR MESSAGE RECEIVED."));
close(clientsocket);
close(serversocket);
}
Output

Client

Algorithm

The different processes involved in a udp client process are........
1.Create a datagram socket.
2.Receive/Send message to the server.
3.Close the socket.

Program
/* Program to demonstrate the creation and usage of datagram sockets.
* This module acts as a udp client which sends and receives messages from a udp server.
* This module should be compiled into different folder using the command 'c++
udpclient.cpp -o b' and execute using './b'
* For execution the 'udpserver' module should be executed first then this module.

//inculsion.
#include
#include
#include
#include
#include
int main()
{

//variable to store the socket_id.
int clientsocket;

//variable to store the address.
sockaddr_in serveraddr;

//variableto store the address length.
socklen_t len;

//variable to store the network byte order address.
hostent *server;
char message[50];

//socket creation.
clientsocket=socket(AF_INET,SOCK_DGRAM,0);

//steps involved in the server address creation.
bzero((char*)&serveraddr,sizeof(serveraddr));
len=sizeof(serveraddr);
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(5015);
server=gethostbyname("127.0.0.1");
bcopy((char*)server->h_addr,(char*)&serveraddr.sin_addr.s_addr,sizeof(server->h_addr));
printf("\nPRESS ENTER TO START THE CONNECTION PROCESS.\n");
fgets(message,2,stdin);
printf("\nSending message for server connection\n");

//sending message.
sendto(clientsocket,"HI I AM CLIENT...",sizeof("HI I AM CLIENT...."),0,(sockaddr*)&serveraddr,sizeof(serveraddr));
printf("\nReceiving message from server.\n");

//receiving messages.
recvfrom(clientsocket,message,sizeof(message),0,(sockaddr*)&serveraddr,&len);
printf("\nMessage received:\t%s\n",message);
close(clientsocket);
}

Output

0 comments:

Post a Comment