LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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
 
Search this Thread
Old 01-13-2005, 04:17 AM   #1
msriram_linux
LQ Newbie
 
Registered: Nov 2004
Posts: 19

Rep: Reputation: 0
Question multiple clients for echo server


Hi all!
I am new to n/w programming...
I was trying out with an echo server which can handle multiple clients simultaneously.

-------------------------Here is the code for server----------------------------

#include<sys/select.h>
#include<unistd.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<time.h>

int main(int argc, char **argv)
{
int connfd0,connfd1;
typedef struct sockaddr SA;

int listenfd,n0=0,n1=0,maxfd,rdfd0=0,rdfd1=0,wfd0=0,wfd1=0;
fd_set rset,wset,rset1;
socklen_t len;
struct sockaddr_in servaddr,clinaddr0,clinaddr1;
char buff0[81],buff1[81];
struct timeval tout;


tout.tv_sec=0;
tout.tv_usec=50;

FD_ZERO(&rset); FD_ZERO(&rset1);
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr.sin_zero,8);
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port=htons(13);
servaddr.sin_port=htons(13);
bind(listenfd,(SA*)&servaddr,sizeof(servaddr));
listen(listenfd,5);

len=sizeof(clinaddr0);
connfd0=accept(listenfd,(SA*)&clinaddr0,&len);
FD_SET(connfd0,&rset);

len=sizeof(clinaddr1);
connfd1=accept(listenfd,(SA*)&clinaddr1,&len);
FD_SET(connfd1,&rset);

close(listenfd);

if(connfd0 > connfd1) { maxfd= connfd0+1; } else { maxfd=connfd1+1; }
rset1=rset;
for( ; ; )
{
n0=0;n1=0;
rset=rset1;
select(maxfd,&rset,NULL,NULL,&tout);

if(FD_ISSET(connfd1,&rset))
{
rdfd1 =1;
}
if(FD_ISSET(connfd0,&rset))
{
rdfd0 =1;
}
}
if(rdfd0)
{
n0=read(connfd0,buff0,80);
write(connfd0,buff0,n0);
}
if(rdfd1)
{
n1=read(connfd1,buff1,80);
wriite(connfd1,buff1,n1);
}

tout.tv_sec=0;
tout.tv_usec=50;

}
exit(0);
}
------------------------------------------here is the code for client --------------------------------------------

#include<netinet/in.h>
#include<stdio.h>
#include<errno.h>
#include<sys/select.h>
#include<sys/types.h>
#include<unistd.h>
typedef struct sockaddr SA;

int main(int argc, char **argv)
{
int sockfd,connfd,n1=0,n2=0,maxfd,srd=0,inrd=0;
fd_set rset,rset1;
char recvline[81];//buffer for read socket
char buff[81];//buffer for read std input
struct sockaddr_in servaddr;

struct timeval tout;
tout.tv_sec =0;
tout.tv_usec=50;
FD_ZERO(&rset);FD_ZERO(&rset1);

if(argc !=2) exit(1);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) exit(1);

bzero(&servaddr,32);
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(13);

if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<0) exit(1);

if(connect(sockfd,(SA*)&servaddr,sizeof(servaddr))<0)
{
printf("%d",errno);exit(1);
}

FD_SET(0,&rset); FD_SET(sockfd,&rset);
rset1=rset; //save rset into rset1
if(sockfd>0) { maxfd =sockfd+1;} else { maxfd = (0+1); }

for( ; ; )
{
rset=rset1;
select(maxfd,&rset,NULL,NULL,&tout);

if(FD_ISSET(0,&rset))
{
inrd=1; //if input is ready for reading then
//read-input flag is set to 1
}
if(FD_ISSET(sockfd,&rset))
{
srd=1; //if input is ready for reading then
//read-input flag is set to 1
}

if(srd)
{
n1=read(sockfd,recvline,80);//read socket and write to stdout
write(1,recvline,n1);
}

if(inrd)
{
n2=read(0,buff,80);//read stdin and write to socket
write(sockfd,buff,n2);
}

tout.tv_sec =0;
tout.tv_usec=50;

}
exit(0);
}
-------------------------------------------------------------------------------------------------
Server accepts only two clients.
while the server is running and accepts the conn from two clients,client 1 and client 2 :
client1 : user i/p : hello
ech o/p: hello
client2 : user i/p : hi
echo o/p : hi
client2: user i/p : good morning
echo o/p: /* nothing comes out now */

as said above : here when client2 initiates the reponse there is nothing echoed until the
client1 input anything....
I have struggled for 3 days to debug...but failed..
!! could anyone please help me debug it...
 
Old 12-05-2007, 03:27 PM   #2
sm1443
LQ Newbie
 
Registered: Dec 2007
Posts: 2

Rep: Reputation: 0
starting client and server

what is the instruction to start server and cleint?


thanks
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple-echo-clients-server msriram_linux Linux - Networking 2 12-05-2007 12:10 PM
echo multiple lines of text twistedpair Linux - Software 9 08-08-2007 06:07 PM
Multiple clients communicating to one socket dragondad Programming 3 09-10-2005 08:22 AM
Mail Server for Multiple Hosting Clients bytebrowser Linux - Networking 2 01-24-2005 03:56 PM
Multiple Clients One Authentication jpbarto Linux - Networking 4 10-02-2003 01:50 PM


All times are GMT -5. The time now is 05:55 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