LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-28-2005, 06:37 AM   #1
Gomathy
LQ Newbie
 
Registered: Jan 2005
Location: India
Posts: 1

Rep: Reputation: 0
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

Last edited by Gomathy; 01-28-2005 at 06:42 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question about select sys call xatzipol Programming 1 10-30-2005 09:22 AM
Select() did not select my socket thvo Programming 1 05-08-2005 12:20 AM
C sockets Select call when to use? cranium2004 Programming 1 03-14-2005 05:56 AM
C: Socket weirdness: select() -> fork() -> accept() maxfacta Programming 1 03-01-2005 01:33 PM
Socket and Select() problems strikernzl Programming 2 09-01-2003 08:34 AM


All times are GMT -5. The time now is 02:59 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration