Unix Programming - socket select send

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > July 2005 > socket select send





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author socket select send
Wenfei

2005-07-28, 6:00 pm

Hi,

When use select and send to handle multi connections, should we use
listen and accept also, like select and recv?
Which of the follwoing is correct?

1)
....
FD_SET(sockfd, &fdwrite);
listen(sockfd, NET_TCP_MAX_SYN_BACKLOG);
select(...);
connectionfd = accept(sockfd, ...);
FD_SET(connectionFD, &fdwrite);
if (FD_ISSET(connectionfd, &fdwrite))
send();

OR, 2)

FD_SET(sockfd, &fdwrite);
listen(sockfd, NET_TCP_MAX_SYN_BACKLOG);
select(...);
connectionfd = accept(sockfd, ...);
// run through the existing connections to send
for( i = 0; i <= maxFD; i++ )
if ( (FD_ISSET(connectionfd, &fdwrite)) && (i != sockfd) )
send();

OR , 3)
....
FD_SET(sockfd, &fdwrite);
listen(sockfd, NET_TCP_MAX_SYN_BACKLOG);
select(...);
if (FD_ISSET(sockfd, &fdwrite))
send();

OR , 4)
....
FD_SET(sockfd, &fdwrite);
select(...);
if (FD_ISSET(sockfd, &fdwrite))
send();

Or, we don't have to call select() since we already call it when we
call recv()?
Also, shoud we set the socket to non-blocking first and then send?
Thanks,

Wenfei

Maxim Yegorushkin

2005-07-29, 2:51 am

Wenfei wrote:
> Hi,
>
> When use select and send to handle multi connections, should we use
> listen and accept also, like select and recv?
> Which of the follwoing is correct?


It looks like you lack knowledge of socket basics. Buy or download
yourself a socket programming book, like those by R.W.Stevens.

This might also help you.
http://www.ecst.csuchico.edu/~beej/...ced.html#select

Wenfei

2005-07-29, 6:01 pm

Maxim,
Beej's guide is what I refer to. But it only talk about recv(). What I
want to do is the applications which own the server socket may
initially send data over the socket to multi clients. But all the
source codes I searched on the internet seems only talk about the
recv(). Can you address more on my questions, I mean the detail code
for select() and send().
Thanks.
Wenfei

Maxim Yegorushkin

2005-07-29, 6:01 pm

Wenfei wrote:
> Maxim,
> Beej's guide is what I refer to. But it only talk about recv(). What I
> want to do is the applications which own the server socket may
> initially send data over the socket to multi clients. But all the
> source codes I searched on the internet seems only talk about the
> recv(). Can you address more on my questions, I mean the detail code
> for select() and send().


Well, the code on the page whose link I posted does accept(), recv()
and send() in a select() loop.

Here is a hard to miss link
http://www.ecst.csuchico.edu/~beej/.../selectserver.c

Wenfei

2005-07-29, 6:01 pm

Thanks for your quick reply, Maxim.

>if (FD_ISSET(j, &master)) {
>// except the listener and ourselves
> if (j != listener && j != i) {

I am wondering, besides i and listener, whoelse is in the master set?

The following is my code( not completed), do you think it is right? if
not, could you please correct it?
Thanks,

//for receiving:
void receiving(){
....
while(1){
FD_SET(sockfd, &fdwrite);
FD_SET(sockfd, &fdread);
listen(sockfd, NET_TCP_MAX_SYN_BACKLOG);
select(maxfd+1, &fdread, &fdwrite, ...);
if ( FD_ISSET(sockfd, &readFDList) )
connectionfd = accept(sockfd, ...);
if (connectionfd > maxfd)
connectionfd = maxfd;
FD_SET(connectionfd, &fdread);
FD_SET(connectionfd, &fdwrite);
// run through the existing connections to recvive
for( i = 0; i <= maxfd; i++ )
if ( (FD_ISSET(connectionfd, &fdread)) && (i != sockfd) )
recv(connectionfd, ...);
}
....
}

//for sending:
void sending(){
....
for( i = 0; i <= maxfd; i++ )
if ( (FD_ISSET(connectionfd, &fdwrite)) && (i != sockfd) )
send(connectionfd, ...);
....
}

Here fdwrite and connectionfd are global variables.

Wenfei

Wenfei

2005-07-29, 6:01 pm

>for( i = 0; i <= maxfd; i++ )
> if ( (FD_ISSET(connectionfd, &fdread)) && (i != sockfd) )
> recv(connectionfd, ...);

should be :
for( i = 0; i <= maxfd; i++ )
if ( (FD_ISSET(i, &fdread)) && (i != sockfd) )
recv(i, ...);
same for send().

Wenfei

Maxim Yegorushkin

2005-07-29, 6:01 pm

Wenfei wrote:
> Thanks for your quick reply, Maxim.
>
> I am wondering, besides i and listener, whoelse is in the master set?


There are listening and accepted sockets (i.e. client connections) in
the master set.

> The following is my code( not completed), do you think it is right? if
> not, could you please correct it?
> Thanks,
>
> //for receiving:
> void receiving(){
> ...
> while(1){
> FD_SET(sockfd, &fdwrite);
> FD_SET(sockfd, &fdread);
> listen(sockfd, NET_TCP_MAX_SYN_BACKLOG);
> select(maxfd+1, &fdread, &fdwrite, ...);
> if ( FD_ISSET(sockfd, &readFDList) )
> connectionfd = accept(sockfd, ...);


You should check accept() result because it may fail.

> if (connectionfd > maxfd)
> connectionfd = maxfd;
> FD_SET(connectionfd, &fdread);
> FD_SET(connectionfd, &fdwrite);


As you just set connectionfd it will be processed in the following loop
as ready for read, although it may not.

> // run through the existing connections to recvive
> for( i = 0; i <= maxfd; i++ )
> if ( (FD_ISSET(connectionfd, &fdread)) && (i != sockfd) )
> recv(connectionfd, ...);
> }
> ...
> }
>
> //for sending:
> void sending(){
> ...
> for( i = 0; i <= maxfd; i++ )
> if ( (FD_ISSET(connectionfd, &fdwrite)) && (i != sockfd) )
> send(connectionfd, ...);
> ...
> }
>
> Here fdwrite and connectionfd are global variables.


Man, get yourself a book, they discuss in details all those things.

Wenfei

2005-07-29, 6:01 pm


Maxim Yegorushkin wrote:
> Wenfei wrote:
>
> There are listening and accepted sockets (i.e. client connections) in
> the master set.


"listener" is the listening socket, and "i" are accepted sockets.
Besides those, anything else?
I mean (j != listener && j != i) will give you no sockets.

>
>
> You should check accept() result because it may fail.
>
>
> As you just set connectionfd it will be processed in the following loop
> as ready for read, although it may not.


This is the same logic as your link
http://www.ecst.csuchico.edu/~beej/.../selectserver.c
which set newfd after select() and accept(). It will not be processed
in the following loop until next turn while loop or your for(;;) loop
if you are right.

>
>
> Man, get yourself a book, they discuss in details all those things.


I did. but they didn't talk about what I am concerned. And also, for
your link
http://www.ecst.csuchico.edu/~beej/.../selectserver.c
I found, it didn't use writefd in select() and didn't check writebility
before send().

Thanks,

Wenfei

2005-07-29, 6:01 pm

The thing I am try to figure out is that for multi clients, we have to
know which socket we will use to send. We cannot random chose one.
My solution is when receive data, we remember the readfd and pass it to
the above applications, and then when the applications want to send,
the application will pass that readfd to the socket and the socket use
that readfd to send out data. Am I right?

David Schwartz

2005-07-30, 7:48 am


"Wenfei" <ye_wenfei@hotmail.com> wrote in message
news:1122671553.220537.22820@z14g2000cwz.googlegroups.com...

> The thing I am try to figure out is that for multi clients, we have to


What do you mean by "multi clients"?

> know which socket we will use to send. We cannot random chose one.


If you mean a client that you have multiple connections to, and they are
all equal, why can't you randomly choose one?

> My solution is when receive data, we remember the readfd and pass it to
> the above applications, and then when the applications want to send,
> the application will pass that readfd to the socket and the socket use
> that readfd to send out data. Am I right?


No clue. What are you trying to do?

DS


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com