/* Start of the Program SERVERTCP.C */
/* SIVA SANTOSH VARMA ALLURI, K00351411 , Operating System Sec 003 */
/* This is an server side program for TCP based echo server using socket programming in UNIX using C programming. */
/* Include the necessary header files */
#include<stdio.h> /* IO stream Interface */
#include<stdlib.h> /* Standard General Utilities Library */
#include<unistd.h> /* misc. UNIX functions */
#include<sys/socket.h> /* socket definitions, pointer to socket address */
#include<sys/types.h> /* data type of socket address structure */
#include<netinet/in.h> /* for IPv4 socket address */
#include<netdb.h> /* structure for network host entry */
#include<strings.h> /* String manipulation operations */
#include<fcntl.h> /* file control options */
#include<arpa/inet.h> /* This is used to convert internet addresses between ASCII */
/* strings and network byte ordered binary values */
int main(int asrgc,char*argv[]) /* start of main function */
{
int bd,sd,ad; /* bd is local protocol address to the socket */
/* sd is Internet socket address structure */
/* ad is accepted address */
char buff[1024]; /* Buffer size */
struct sockaddr_in cliaddr,servaddr; /* Create a socket using socket function with */
/* family AF_INET, type as SOCK_STREAM */
socklen_t clilen; /* Size of Host address */
clilen=sizeof(cliaddr); /* Size of Client address */
bzero(&servaddr,sizeof(servaddr)); /* Initialize server address to 0 using the bzero function.*
/* Socket address structure*/
/* Assign the sin_family to AF_INET, sin_addr to INADDR_ANY, sin_port to a dynamically
assigned port number. */
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(1999);
/*TCP socket is created, an Internet socket address structure is filled with wildcard address
& server’s well known port*/
sd=socket(AF_INET,SOCK_STREAM,0);
/*Bind function assigns a local protocol address to the socket*/
bd=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));
/*Listen function specifies the maximum number of connections that kernel should queue
for this socket*/
listen(sd,5);
printf("SIVA SANTOSH VARMA ALLURI, OS, Sec 003 Server is running….\n");
/*The server to return the next completed connection from the front
of the completed connection Queue calls it*/
/* Accept connection request from the client using accept function.*/
ad=accept(sd,(struct sockaddr*)&cliaddr,&clilen);
/* Within an infinite loop, using the recv function receive message from the client and print it
on the console */
while(1)
{
bzero(&buff,sizeof(buff));
recv(ad,buff,sizeof(buff),0); /*Receiving the request message from the client*/
printf(" Message received by SIVA SANTOSH VARMA ALLURI, OS , Sec 003”);
printf("Operating System Sec 003 is %s\n",buff);
/* Print Acknowledgement of received message */
} /* End of Program */
}