Sunday 29 January 2012

Socket programming ,A server with multiple clients;Guest starring "fork()"

A network socket is an endpoint of an inter-process communication flow
across a computer network. Today, most communication between computers
is based on the Internet Protocol; therefore most network sockets are Internet
sockets.
A socket address is the combination of an IP address and a port number,
much like one end of a telephone connection is the combination of a phone
number and a particular extension. Based on this address, internet sockets deliver incoming data packets to the appropriate application process or
thread.
Computer processes that provide application services are called servers, and
create sockets on start up that are in listening state. These sockets are waiting for initiatives from client programs.
Thus the client will initiate a connection with server by means of socket and
communicates by means of read,write instruction.The server on the other
hand will perform listening and binding operation to accept the connection
from the client.

SERVER


CLIENT
Program which can allow server to service multiple clients using fork() ,here a simple arithmetic operation entered by the user as a client will be computed at the server end and the result is displayed back to the client.It allows you to setup communication between different systems in a LAN or the internet itself.



client program:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }
    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
        error("ERROR opening socket");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr,
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
        error("ERROR connecting");
    printf("Please enter the operation: ");
    bzero(buffer,256);
    fgets(buffer,255,stdin);
    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0)
         error("ERROR writing to socket");
    bzero(buffer,256);
    n = read(sockfd,buffer,255);
    if (n < 0)
         error("ERROR reading from socket");
    printf("%s\n",buffer);
    close(sockfd);
    return 0;
}
//ends

Server program

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include<math.h>
#include<stdlib.h>

int main()
{
    int sfd, cfd,i=0,a1,a2,b,j=0;
    socklen_t len;
char a[100],b1[50];
    char ch[255],buff[INET_ADDRSTRLEN],c;
    struct sockaddr_in saddr, caddr;

    sfd= socket(AF_INET, SOCK_STREAM, 0);

    saddr.sin_family=AF_INET;
    saddr.sin_addr.s_addr=htonl(INADDR_ANY);
    saddr.sin_port=htons(1205);

    bind(sfd, (struct sockaddr *)&saddr, sizeof(saddr));

    listen(sfd, 5);
    signal(SIGCHLD, SIG_IGN);

    while(1) {
        printf("Server waiting\n");
        len=sizeof(caddr);
        cfd=accept(sfd, (struct sockaddr *)&caddr, &len);

        if( fork() == 0) {
            printf("Child Server Created Handling connection with %s\n",
                inet_ntop(AF_INET, &caddr.sin_addr, buff, sizeof(buff)));

            close(sfd);

          if(read(cfd, ch, 255)<0) perror("read");

         
   
memset(a, 0, 100);
// parsing
while((ch[i]!='-') && (ch[i]!='+') && (ch[i]!='*') && (ch[i]!='/'))
{

a[i]=ch[i];
i++;
}
 a1 = atoi(a);
c=ch[i];i++;

memset(a, 0, 100);
while(ch[i] != '\0')
{

a[j]=ch[i];
i++;
j++;
}

memset(ch,0,255);
a2 = atoi(a);
//parsing ends here
switch(c)
{case '+': b =a1+a2;
break;
case '-':b = a1 - a2;
break;
case '*': b= a1 * a2;
break;
case '/':
if(a2 != 0)
b= a1 / a2;
else
b=0;

}

sprintf(ch,"%d",b);


                if(write(cfd, ch, sizeof(ch))<0) perror("write");

            
            close(cfd);
            return 0;

        }

        close(cfd);
    }
}

//ends



How to run ?

compile the above programs and execute the server first..note the server uses a port no of 1205..you need to pass this for the client program in command line

for linux
executing server program

>gcc filename.c -o s
>./s

execute client
>gcc filename1.c -o c
>./c localhost 1205


instead of localhost you can supply ip address.


Tutorial awaits

and yeah it was a lousy assignment of mine...

No comments:

Post a Comment