LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (http://www.linuxquestions.org/questions/linux-networking-3/)
-   -   IPC in socket using select system call (http://www.linuxquestions.org/questions/linux-networking-3/ipc-in-socket-using-select-system-call-283241/)

Gomathy 01-28-2005 07:37 AM

IPC in socket using select system call
 
Hi

I am doing final yr BE CSE
my final yr Project is RIP(ROUTING INFORMATION PROTOCOL)
USING LINUX/UNIX WITH C

my first step is IPC socket using select system call.I have implemented
but some problem has occured

The server process can be able to handle more number of client process
Now my program the server can accept many clients but it response is send only one client( first request client )

plz sir can u modify the server process can be ableto handle multiple clients

The same client program is used as many client with various name
.................................................................................................

Programs
..............
server

Code:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(char *msg)
{
    perror(msg);
    exit(1);
}
int main(int argc, char *argv[])
{
    int sockfd, newsockfd, portno, clilen, pid,n;
    struct sockaddr_in serv_addr, cli_addr;
        char buffer[256];
    if (argc < 2) {
        fprintf(stderr,"ERROR, no port provided\n");
        exit(1);
    }
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
        error("ERROR opening socket");
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = atoi(argv[1]);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
    if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0)
              error("ERROR on binding");
    listen(sockfd,5);
    clilen = sizeof(cli_addr);
    int p;
    while (1) {
        newsockfd = accept(sockfd,
              (struct sockaddr *) &cli_addr, &clilen);
        if (newsockfd < 0)
                        error("ERROR on accept");
        pid = fork();
        if (pid < 0)
            error("ERROR on fork");
        if (pid == 0)  {
      close(sockfd);
p=fork();
if(p==0)
{
      for(;;)
                {
                        bzero(buffer,256);
                        n=recv(newsockfd,buffer,sizeof(buffer),0);

                        printf("Here is the message:%s\n",buffer);
                }                                             
               
                }
else
{
        for(;;)
                {
                  bzero(buffer,256);
                  fgets(buffer,256,stdin);
                  n=send(newsockfd,buffer,sizeof(buffer),0);       
                    }
}
}
else
                {
close(newsockfd);
        }
    }
    return 0;
}
.....................................................................................

client
......

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno,m,n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256],buff[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,&serv_addr,sizeof(serv_addr)) <0)
error("ERROR connecting");
printf("Please enter the message: ");
m=fork();
if(m==0)
        {
                for(;;)
                {
        fgets(buffer,255,stdin);
n = send(sockfd,buffer,strlen(buffer),0);
if (n < 0)
error("ERROR writing to socket");
                }
        }
        else
        {
                for(;;)
                {
n = recv(sockfd,buff,255,0);
if (n < 0)
error("ERROR reading from socket");
printf("The return message:");
printf("%s\n",buff);
}
}
return 0;
}

plz reply me
Thank u


All times are GMT -5. The time now is 01:07 AM.