Saturday, September 24, 2011

NP LAB RECORD


NP Lab Programs:

This contains programs of 4,5,7,8 weeks

Note: 4th and 5th week programs are same.
Write two questions of 4th and 5th and start writing the same program.
Week - 4 -iterative
Concurrent Server-client Reverse
Name: tcpClientReverse.c and tcpServerReverse.c


Week - 5 -Interactive
Concurrent Server-client Reverse
Name: tcpClientReverse.c and  tcpServerReverse.c

Week - 7
Concurrent Server-Client Upper Case
Name:
tcpClientUpper.c & tcpServerUpper.c

Week - 8
Concurrent Server-Client Using Poll
Name: tcpClientPoll.c and tcpServerPoll.c



/***************************************************************************
TcpClientReverse


* FILENAME : democlient.c
* DESCRIPTION:Contains Code for a client that will send a string
* to a server process and exits.
* Invoke the Executable as a.out IPAddress PortNo string
* Copyright 2007 Aricent
*****************************************************************************/






#include < stdio.h >
#include < stdlib.h >
#include < unistd.h >
#include < errno.h >
#include < string.h >
#include < sys/types.h >
#include < sys/stat.h >
#include < sys/socket.h >
#include < netinet/in.h >
#include < arpa/inet.h >
#include < fcntl.h > 
#define MAXBUFFER 1024
void sendstring(int , char *);
int main( int C, char *V[] )
{
int sd,fd;

char c;
struct sockaddr_in serveraddress;
char text[100];

int i=0;
sd = socket( AF_INET, SOCK_STREAM, 0 );
if( sd <0 ) {

perror( "socket" );

exit( 1 );
}
if (V[1] == NULL ) {
printf ("PL specfiy the server's IP Address \n");
exit(0);
}
if (V[2] == NULL ) {
printf ("PL specify the server's Port No \n");
exit(0);
}


// if (V[3] == NULL ) {

// printf ("PL specfiy the string to be send to the server \n");
// exit(0);
// }


memset( &serveraddress, 0, sizeof(serveraddress) );
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(atoi(V[2]));//PORT NO
serveraddress.sin_addr.s_addr = inet_addr(V[1]);//ADDRESS
if (connect(sd,(struct sockaddr*)&serveraddress,


sizeof(serveraddress))<0)

{
printf("Cannot Connect to server");
exit(1);


}
printf("enter sentence to end enter #");
while(1)
{
c=getchar();
if(c=='#')
break;
text[i++]=c;
}
text[i]='\0';

sendstring(sd,text);
close(sd);
return 0;


}
/************************************************************************

*
FUNCTION NAME:sendstring
*
DESCRIPTION: sends a string over the socket .
*
NOTES : No Error Checking is done .
* RETURNS :void
************************************************************************/
void sendstring(
int sd, /*Socket Descriptor*/
char *fname) /*Array Containing the string */

/*************************************************************************/

{
int n , byteswritten=0 , written ;
char buffer[MAXBUFFER];
strcpy(buffer , fname);
n=strlen(buffer);
while (byteswritten
{

written=write(sd , buffer+byteswritten,(n-byteswritten));
byteswritten+=written;

}

printf("String : %s sent to server \n",buffer);

}
/****************************************************************************/


-----------------------------------------------------------------


TcpClientReverseUsingSelect
* FILENAME : democlient.c
* DESCRIPTION:Contains Code for a client that will send a string
* to a server process and exits.
* Invoke the Executable as a.out IPAddress PortNo string
* Copyright 2007 Aricent
*****************************************************************************/




#include < stdio.h >
#include < stdlib.h >
#include < unistd.h >
#include < errno.h >
#include < string.h >
#include < sys/types.h >
#include < sys/stat.h >
#include < sys/socket.h >
#include < netinet/in.h >
#include < arpa/inet.h >
#include < fcntl.h > 
#define MAXBUFFER 1024
void sendstring(int , char *);
int main( int C, char *V[] )
{
int sd,fd;

char c;
struct sockaddr_in serveraddress;
char text[100];

int i=0;
sd = socket( AF_INET, SOCK_STREAM, 0 );
if( sd <0 ) {

perror( "socket" );

exit( 1 );
}
if (V[1] == NULL ) {
printf ("PL specfiy the server's IP Address \n");
exit(0);
}
if (V[2] == NULL ) {
printf ("PL specify the server's Port No \n");
exit(0);
}


// if (V[3] == NULL ) {

// printf ("PL specfiy the string to be send to the server \n");
// exit(0);
// }


memset( &serveraddress, 0, sizeof(serveraddress) );
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(atoi(V[2]));//PORT NO
serveraddress.sin_addr.s_addr = inet_addr(V[1]);//ADDRESS
if (connect(sd,(struct sockaddr*)&serveraddress,


sizeof(serveraddress))<0)

{
printf("Cannot Connect to server");
exit(1);


}
printf("enter sentence to end enter #");
while(1)
{
c=getchar();
if(c=='#')
break;
text[i++]=c;
}
text[i]='\0';

sendstring(sd,text);
close(sd);
return 0;


}
/************************************************************************

*
FUNCTION NAME:sendstring
*
DESCRIPTION: sends a string over the socket .
*
NOTES : No Error Checking is done .
* RETURNS :void
************************************************************************/
void sendstring(
int sd, /*Socket Descriptor*/
char *fname) /*Array Containing the string */

/*************************************************************************/

{
int n , byteswritten=0 , written ;
char buffer[MAXBUFFER];
strcpy(buffer , fname);
n=strlen(buffer);
while (byteswritten
{

written=write(sd , buffer+byteswritten,(n-byteswritten));
byteswritten+=written;

}

printf("String : %s sent to server \n",buffer);

}
/****************************************************************************/

----------------------------------------------------------------- 

TcpServerReverse

#include < stdio.h >
#include < stdlib.h>
#include < errno.h >
#include < string.h >
#include < sys/types.h >
#include < sys/socket.h >
#include < netinet/in.h >
#include < arpa/inet.h >
#include < sys/wait.h >
#include < fcntl.h >
#include < unistd.h > 
#define MYPORT 13154 /*The port users will be connecting to*/

void readstring(int,char *);

int main(int C, char *V[] )

{

int listensocket,connectionsocket,retbind;

struct sockaddr_in

serveraddress,cliaddr;

socklen_t len;

char buf[100],databuf[1024];

listensocket = socket(AF_INET, SOCK_STREAM, 0 );
if (listensocket < 0 )
{


perror("socket" );
exit(1);
}


memset(&serveraddress, 0, sizeof(serveraddress) );
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(MYPORT);/*PORT NO*/
serveraddress.sin_addr.s_addr = htonl(INADDR_ANY);/*ADDRESS*/
retbind=bind(listensocket,(struct sockaddr*)&serveraddress,


sizeof(serveraddress));
/*Check the return value of bind for error*/
if(-1==retbind)
{


perror("BIND ERROR\n");
exit(1);
}


listen(listensocket,5);
/*Beginning of the Main Server Processing Loop*/


for (;;)

{
printf("Server:I am waiting-----Start of Main Loop\n");
len=sizeof(cliaddr);
connectionsocket=accept(listensocket,

(struct sockaddr*)&cliaddr,&len);
if (connectionsocket < 0)
{

if (errno == EINTR)
printf("Interrupted system call ??");
continue;

}
printf("Connection from %s\n",


inet_ntop(AF_INET,&cliaddr.sin_addr,buf,sizeof(buf)));
readstring(connectionsocket , databuf);
close(connectionsocket);
printf("Finished Serving One Client\n");

}

}



/********************************************************************

* FUNCTION NAME:readstring
* DESCRIPTION: Reads the string sent by the client over the
* socket and stores it in the array fname .
* NOTES : No Error Checking is done .
* RETURNS :void
*********************************************************************/
void readstring(

int connectionsocket, /*Socket Descriptor*/

char *fname) /*Array , to be populated by the string from client*/
/********************************************************************/
{

int pointer=0,n;

int len=0,a,b;

char rev[50],temp[50],temp1[50];

int k,i;

while ((n=read(connectionsocket,(fname + pointer),1024))>0)

{

pointer=pointer+n;

}

fname[pointer]='\0';

printf("enter the string\n");

printf("Server :Received %s\n " ,fname);

//strcpy(temp,fname);
k=strlen(fname);

// for(k=0;temp[k]!=0;k++);

// len=k;
a=0;
for(i=k-1;i>=0;i--)
temp[a++]=fname[i];
temp[a]='\0';

printf("\nrev is %s\n", temp);

}
/**********************************************************************/

----------------------------------------------------------------- 

TcpServerReverseUsingSelect






#include < stdio.h >
#include < stdlib.h >
#include < errno.h >
#include < string.h >
#include < sys/types.h >
#include < sys/socket.h >
#include < netinet/in.h >
#include < arpa/inet.h >
#include < sys/wait.h >
#include < fcntl.h >
#include < unistd.h  > 
#define MYPORT 13154 /*The port users will be connecting to*/

void readstring(int,char *);

int main(int C, char *V[] )

{

int listensocket,connectionsocket,retbind;

struct sockaddr_in

serveraddress,cliaddr;

socklen_t len;

char buf[100],databuf[1024];

listensocket = socket(AF_INET, SOCK_STREAM, 0 );
if (listensocket < 0 )
{


perror("socket" );
exit(1);
}


memset(&serveraddress, 0, sizeof(serveraddress) );
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(MYPORT);/*PORT NO*/
serveraddress.sin_addr.s_addr = htonl(INADDR_ANY);/*ADDRESS*/
retbind=bind(listensocket,(struct sockaddr*)&serveraddress,


sizeof(serveraddress));
/*Check the return value of bind for error*/
if(-1==retbind)
{


perror("BIND ERROR\n");
exit(1);
}


listen(listensocket,5);
/*Beginning of the Main Server Processing Loop*/


for (;;)

{
printf("Server:I am waiting-----Start of Main Loop\n");
len=sizeof(cliaddr);
connectionsocket=accept(listensocket,

(struct sockaddr*)&cliaddr,&len);
if (connectionsocket < 0)
{

if (errno == EINTR)
printf("Interrupted system call ??");
continue;

}
printf("Connection from %s\n",


inet_ntop(AF_INET,&cliaddr.sin_addr,buf,sizeof(buf)));
readstring(connectionsocket , databuf);
close(connectionsocket);
printf("Finished Serving One Client\n");

}

}



/********************************************************************

* FUNCTION NAME:readstring
* DESCRIPTION: Reads the string sent by the client over the
* socket and stores it in the array fname .
* NOTES : No Error Checking is done .
* RETURNS :void
*********************************************************************/
void readstring(

int connectionsocket, /*Socket Descriptor*/

char *fname) /*Array , to be populated by the string from client*/
/********************************************************************/
{

int pointer=0,n;

int len=0,a,b;

char rev[50],temp[50],temp1[50];

int k,i;

while ((n=read(connectionsocket,(fname + pointer),1024))>0)

{

pointer=pointer+n;

}

fname[pointer]='\0';

printf("enter the string\n");

printf("Server :Received %s\n " ,fname);

//strcpy(temp,fname);
k=strlen(fname);

// for(k=0;temp[k]!=0;k++);

// len=k;
a=0;
for(i=k-1;i>=0;i--)
temp[a++]=fname[i];
temp[a]='\0';

printf("\nrev is %s\n", temp);

}
/**********************************************************************/

 ----------------------------------------------------------------


tcpClientUpper

/*
*ME : democlient.c
* DESCRIPTION:Contains Code for a client that will send a string
* to a server process and exits.
* Invoke the Executable as a.out IPAddress PortNo string
* Copyright 2007 Aricent
*****************************************************************************/




#include < stdio.h >
#include < stdlib.h >
#include < unistd.h >
#include < errno.h >
#include < string.h >
#include < sys/types.h >
#include < sys/stat.h >
#include < sys/socket.h >
#include < netinet/in.h >
#include < arpa/inet.h >
#include < fcntl.h > 
#define MAXBUFFER 1024
void sendstring(int , char *);
int main( int C, char *V[] )
{
int sd,fd;

char c;
struct sockaddr_in serveraddress;
char text[100];

int i=0;
sd = socket( AF_INET, SOCK_STREAM, 0 );
if( sd <0 ) {

perror( "socket" );

exit( 1 );
}
if (V[1] == NULL ) {
printf ("PL specfiy the server's IP Address \n");
exit(0);
}
if (V[2] == NULL ) {
printf ("PL specify the server's Port No \n");
exit(0);
}


// if (V[3] == NULL ) {



// printf ("PL specfiy the string to be send to the server \n");
// exit(0);
// }


memset( &serveraddress, 0, sizeof(serveraddress) );
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(atoi(V[2]));//PORT NO
serveraddress.sin_addr.s_addr = inet_addr(V[1]);//ADDRESS
if (connect(sd,(struct sockaddr*)&serveraddress,


sizeof(serveraddress))<0)

{
printf("Cannot Connect to server");
exit(1);


}
printf("enter sentence to end enter #");
while(1)
{
c=getchar();
if(c=='#')
break;
text[i++]=c;
}
text[i]='\0';

sendstring(sd,text);
close(sd);
return 0;


}
/************************************************************************

* FUNCTION NAME:sendstring
* DESCRIPTION: sends a string over the socket .
* NOTES : No Error Checking is done .
* RETURNS :void
************************************************************************/
void sendstring(
int sd, /*Socket Descriptor*/

char *fname) /*Array Containing the string */
/*************************************************************************/
{ int n , byteswritten=0 , written ;

char buffer[MAXBUFFER];
strcpy(buffer , fname);
n=strlen(buffer);
while (byteswritten
{


written=write(sd , buffer+byteswritten,(n-byteswritten));
byteswritten+=written;



}

printf("String : %s sent to server \n",buffer);

}
/****************************************************************************/

 


tcpServerUpper





#include < stdlib.h >
#include < stdio.h >
#include < unistd.h >
#include < errno.h >
#include < string.h >
#include < sys/types.h >
#include < sys/stat.h >
#include < sys/select.h >
#include < sys/time.h >
#include < sys/socket.h >
#include < netinet/in.h >
#include < arpa/inet.h >
#include < fcntl.h > 
#define MAXLINE 100
#define SERV_PORT 13153 

int main(int argc, char **argv)
{
int k, i, maxi, maxfd, listenfd, connfd, sockfd;
int nready, client[FD_SETSIZE];
ssize_t n;
fd_set rset, allset;
char line[MAXLINE],buf[100];
socklen_t clilen;

struct sockaddr_in cliaddr, servaddr;


listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0 )
{


perror("socket" );

exit(1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);

bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

listen(listenfd,5);

maxfd = listenfd; /* initialize */
maxi = -1; /* index into client[] array */
for (i = 0; i < FD_SETSIZE; i++)


client[i] = -1; /* -1 indicates available entry */
FD_ZERO(&allset);
FD_SET(listenfd, &allset);


/* end fig01 */

/* include fig02 */

for ( ; ; ) {
printf("Server:I am waiting-----Start of Main Loop\n");
rset = allset; /* structure assignment */
nready = select(maxfd+1, &rset, NULL, NULL, NULL);


if (FD_ISSET(listenfd, &rset)) { /* new client connection */
clilen = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);

#ifdef NOTDEF

printf("new client: %s, port %d\n",
inet_ntop(AF_INET, &cliaddr.sin_addr, buf, NULL),
ntohs(cliaddr.sin_port));

#endif

for (i = 0; i < FD_SETSIZE; i++)

if (client[i] < 0) {
client[i] = connfd; /* save descriptor */
break;

}

if (i == FD_SETSIZE)
{
printf("too many clients");
exit(0);
}


FD_SET(connfd, &allset); /* add new descriptor to set */
if (connfd > maxfd)
maxfd = connfd; /* for select */
if (i > maxi)
maxi = i; /* max index in client[] array */

if (--nready <= 0)
continue; /* no more readable descriptors
*/
}

for (i = 0; i <= maxi; i++) { /* check all clients for data */

if ( (sockfd = client[i]) < 0)

continue;
if (FD_ISSET(sockfd, &rset)) {

if ( (n = read(sockfd, line, MAXLINE)) == 0) {

/*4connection closed by client */
close(sockfd);
FD_CLR(sockfd, &allset);
client[i] = -1;

} else

{
printf("\n output at server\n");
for(k=0;line[k]!='\0';k++)
printf("%c",toupper(line[k]));

write(sockfd, line, n);
}
if (--nready <= 0)
break; /* no more readable descriptors
*/
}
}

}
}
/* end fig02 */ 

-----------------------------------------------------------------


tcpClientPoll

/***************************************************************************

* FILENAME : democlient.c
* DESCRIPTION:Contains Code for a client that will send a string
* to a server process and exits.
* Invoke the Executable as a.out IPAddress PortNo string
* Copyright 2007 Aricent
*****************************************************************************/




#include < stdio.h >
#include < stdlib.h >
#include < unistd.h >
#include < errno.h >
#include < string.h >
#include < sys/types.h >
#include < sys/stat.h >
#include < sys/socket.h >
#include < netinet/in.h >
#include < arpa/inet.h >
#include < fcntl.h > 
#define MAXBUFFER 1024
void sendstring(int , char *);
int main( int C, char *V[] )
{
int sd,fd;

char c;
struct sockaddr_in serveraddress;
char text[100];

int i=0;
sd = socket( AF_INET, SOCK_STREAM, 0 );
if( sd <0 ) {

perror( "socket" );

exit( 1 );
}
if (V[1] == NULL ) {
printf ("PL specfiy the server's IP Address \n");
exit(0);
}
if (V[2] == NULL ) {
printf ("PL specify the server's Port No \n");
exit(0);
}


// if (V[3] == NULL ) {

// printf ("PL specfiy the string to be send to the server \n");
// exit(0);
// }


memset( &serveraddress, 0, sizeof(serveraddress) );
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(atoi(V[2]));//PORT NO
serveraddress.sin_addr.s_addr = inet_addr(V[1]);//ADDRESS
if (connect(sd,(struct sockaddr*)&serveraddress,


sizeof(serveraddress))<0)

{
printf("Cannot Connect to server");
exit(1);


}
printf("enter sentence to end enter #");
while(1)
{
c=getchar();
if(c=='#')
break;
text[i++]=c;
}
text[i]='\0';

sendstring(sd,text);
close(sd);
return 0;


}
/************************************************************************

*
FUNCTION NAME:sendstring
*
DESCRIPTION: sends a string over the socket .
*
NOTES : No Error Checking is done .
* RETURNS :void
************************************************************************/
void sendstring(
int sd, /*Socket Descriptor*/
char *fname) /*Array Containing the string */

/*************************************************************************/

{
int n , byteswritten=0 , written ;
char buffer[MAXBUFFER];
strcpy(buffer , fname);
n=strlen(buffer);
while (byteswritten
{

written=write(sd , buffer+byteswritten,(n-byteswritten));
byteswritten+=written;
}
printf("String : %s sent to server \n",buffer);


}
/****************************************************************************/
 

-----------------------------------------------------------------





tcpServerPoll






#include < stdlib.h >
#include < stdio.h >
#include < string.h >
#include < sys/types.h >
#include < sys/stat.h >
#include < sys/socket.h >
#include < netinet/in.h >
#include < arpa/inet.h >
#include < fcntl.h >
#include < limits.h > /* for OPEN_MAX */
#include < poll.h >
#include < errno.h > 
#define MAXLINE 100
#define SERV_PORT 13154
#define POLLRDNORM 5
#define INFTIM 5
#define OPEN_MAX 5


int main(int argc, char **argv)
{
      int k, i, maxi, listenfd, connfd, sockfd;
      int nready;
      ssize_t n;
      char line[MAXLINE];
      socklen_t clilen;
      struct pollfd client[OPEN_MAX];
      struct sockaddr_in cliaddr, servaddr;
      listenfd = socket(AF_INET, SOCK_STREAM, 0);

      bzero(&servaddr, sizeof(servaddr));
      servaddr.sin_family = AF_INET;
      servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
      servaddr.sin_port = htons(SERV_PORT);


      bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

      listen(listenfd, 5);

      client[0].fd = listenfd;
      client[0].events = POLLRDNORM;
      for (i = 1; i < OPEN_MAX; i++)
      client[i].fd = -1; /* -1 indicates available entry */
      maxi = 0; /* max index into client[] array */
      /* end fig01 */

      /* include fig02 */

      for ( ; ; )
      {
            nready = poll(client, maxi+1, INFTIM);
           
            if (client[0].revents & POLLRDNORM)
            {    
                   /* new client connection */
                  clilen = sizeof(cliaddr);
                  connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);

                  #ifdef NOTDEF
                  printf("new client: %s\n", sock_ntop((struct sockaddr *) &cliaddr,clilen));
                  #endif

                  for (i = 1; i < OPEN_MAX; i++)
                  if (client[i].fd < 0)
                  {
                        client[i].fd = connfd; /* save descriptor */
                        break;
                  }
     
                  if (i == OPEN_MAX)
                  {
                        printf("too many clients");
                        exit(0);
                  }
     
                  client[i].events = POLLRDNORM;
                  if (i > maxi)
                  maxi = i; /* max index in client[] array */

                  if (--nready <= 0)
                  continue; /* no more readable descriptors */
            }    

            for (i = 1; i <= maxi; i++)
            {
                   /* check all clients for data */
                  if ( (sockfd = client[i].fd) < 0)
                  continue;
                  if (client[i].revents & (POLLRDNORM | POLLERR))
                  {
                        if ( (n = read(sockfd, line, MAXLINE)) < 0)
                        {
                              if (errno == ECONNRESET)
                              {
                                    /*4connection reset by client */
                                    #ifdef NOTDEF
                                    printf("client[%d] aborted connection\n", i);
                                    #endif

                                    close(sockfd);
     
                                    client[i].fd = -1;

                              }
                              else
                              printf("readline error");
                        }
                        else if (n== 0)
                        {
                              /*4connection closed by client */
                              #ifdef NOTDEF
                              printf("client[%d] closed connection\n", i);
                              #endif

                              close(sockfd);

                              client[i].fd = -1;
                        }
                        else
                        {
                              printf("\n data from client is \n");
                              k=strlen(line);
                              printf(" length=%d data = %s\n", k,line);

                              //write(sockfd, line, n);
                              strcpy(line," ");
                        }
                        if (--nready <= 0)
                        break; /* no more readable descriptors */
                  }
            }
      }
}
/* end fig02 */
 

-----------------------------------------------------------------




KUMCLIENT





#include < stdio.h >
#include < stdlib.h >
#include < error.h >
#include < unistd.h >
#include < sys/socket.h >
#include < sys/types.h >
#include < netinet/in.h >
#include < string.h >
#include < arpa/inet.h >
#define ERROR -1
#define BUFFER 1024
main(int argc, char **argv)
 {
struct sockaddr_in remote_server;
int sock;
char input[BUFFER];
char output[BUFFER];
int len;
  if((sock = socket(AF_INET, SOCK_STREAM, 0)) == ERROR)
    {
       perror("socket");
         exit(-1);
     }
 remote_server.sin_family = AF_INET;
 remote_server.sin_port = htons(atoi(argv[2]));
 remote_server.sin_addr.s_addr = inet_addr(argv[1]);
 bzero(&remote_server.sin_zero, 8);
 if((connect(sock, (struct sockaddr *) &remote_server, sizeof(struct sockaddr_in))) == ERROR)
  {
   perror("connect");
   exit(-1);
  }
  while(1)
  {
 fgets(input, BUFFER, stdin);
 send(sock, input, strlen(input), 0);
len = recv(sock, output, BUFFER,0);
output[len] = '\0';
printf("%s\n",output);
 }
close(sock);
}
 

-----------------------------------------------------------------





KUMSERVER






#include < stdio.h >
#include < stdlib.h >
#include < sys/socket.h >
#include < unistd.h >
#include < sys/types.h >
#include < strings.h >
#include < error.h >
#include < netinet/in.h >
#include < arpa/inet.h >
#define ERROR   -1
#define MAX_CLIENTS 2
#define  MAX_DATA  1024
 main(int argc, char **argv)
   {
       struct  sockaddr_in server;
       struct sockaddr_in client;
        int sock;
        int new;
       int sockaddr_len = sizeof(struct sockaddr_in);
       int data_len;
       char data[MAX_DATA];
        if((sock = socket(AF_INET, SOCK_STREAM, 0)) == ERROR)
          {
            perror(" server socket:");
            exit(-1);
          }
       server.sin_family =AF_INET;
       server.sin_port =htons(atoi(argv[1]));
       server.sin_addr.s_addr = INADDR_ANY;
       bzero(&server.sin_zero,8);
        if((bind(sock,(struct sockaddr*)&server,sockaddr_len)) == ERROR)
         {
           perror("bind:");
           exit(-1);
         }
         if((listen(sock,MAX_CLIENTS)) == ERROR)
          {
            perror("listen:");
             exit(-1);
           }
        while(1) //better signal handling required//
           {
            if ((new = accept(sock,(struct sockaddr*)&client,&sockaddr_len))==ERROR)
           {
             perror("accept:");
             exit(-1);
            }
     printf("NEW client connected from port no %d and IP %s\n", ntohs(client.sin_port),inet_ntoa(client.sin_addr));
      data_len = 1;
      while(data_len)
       {
        data_len = recv(new,data,MAX_DATA,0);
        if(data_len)
        {
         send(new, data, data_len, 0);
         data[data_len] = '\0';
         printf("sent mesg: %s", data);
        }
       }
        printf("client disconnected\n");
        close(new);
}
 }
 

-----------------------------------------------------------------






















 





No comments:

Post a Comment