Code Comments
Programming Forum and web based access to our favorite programming groups.I am having a bit of a problem, and it might have to do with my
understanding of how select works...
basically what i am trying to simulate is a system:
client->gateway->server model. The client breaks a file up into
sections and sends them to the server, through teh gateway which just
forwards info. Now upon getting the info, the server sends back an
ACK through the gateway, when the client sees the ack it sends the
next part (simeple stop_and_wait protocol) now my server is set to
randomly drop packets. and here is the problem i am having.
when no packets get dropped, everything works fine. if the first ACK
gets dropped from teh server then the client goes into a weird loop,
even if the server resends that ACK, the client never sees info coming
to it.
here is the code:
int sock;
fd_set fds;
struct time_val tv;
tv.tv_sec=3; //set timeout to 3sec
FD_ZERO(&fds);
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) //open new
socket
WriteLogDie("socket() failed to open a new socket",client_log);
FD_SET(sock, &fds);
if (bind(sock, (struct sockaddr *) &ClientAddr,
sizeof(ClientAddr)) < 0) /*bind to local addr*/
if (packet==0)
SendFileName();
if (packet>0)
SendData();
select (sock+1, &fds,NULL,NULL,&tv);
if(select(sock+1,&fds,NULL, NULL, &tv)
RecievePacket();
else {
//TIMEOUT
cout << "got a timeout\n";
do {
ResendPacket();
cout << "Resending\n";
}while (select(sock+1,&fds,NULL,NULL,&tv));
}
This code is nested in a while loop that just checks to see when were
done sending. This works perfectly if no packets are dropped. but if
the first ACK from the server is dropped, it goes into a loop, and
even if the gateway forwards information with the ACK later to the
client, it never shows that select() chooses to get info from the
socket. i dont know why. does anyone?
Thanks,
Adam
Post Follow-up to this messageIn article <a5c8620f.0410170717.226f5aa6@posting.google.com>,
ffld@hotmail.com (Adam Balgach) wrote:
> I am having a bit of a problem, and it might have to do with my
> understanding of how select works...
>
> basically what i am trying to simulate is a system:
> client->gateway->server model. The client breaks a file up into
> sections and sends them to the server, through teh gateway which just
> forwards info. Now upon getting the info, the server sends back an
> ACK through the gateway, when the client sees the ack it sends the
> next part (simeple stop_and_wait protocol) now my server is set to
> randomly drop packets. and here is the problem i am having.
>
> when no packets get dropped, everything works fine. if the first ACK
> gets dropped from teh server then the client goes into a weird loop,
> even if the server resends that ACK, the client never sees info coming
> to it.
>
> here is the code:
>
> int sock;
> fd_set fds;
> struct time_val tv;
>
> tv.tv_sec=3; //set timeout to 3sec
> FD_ZERO(&fds);
> if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) //open new
> socket
> WriteLogDie("socket() failed to open a new socket",client_log);
>
> FD_SET(sock, &fds);
>
> if (bind(sock, (struct sockaddr *) &ClientAddr,
> sizeof(ClientAddr)) < 0) /*bind to local addr*/
>
> if (packet==0)
> SendFileName();
> if (packet>0)
> SendData();
>
> select (sock+1, &fds,NULL,NULL,&tv);
>
> if(select(sock+1,&fds,NULL, NULL, &tv)
> RecievePacket();
> else {
> //TIMEOUT
> cout << "got a timeout\n";
> do {
> ResendPacket();
> cout << "Resending\n";
> }while (select(sock+1,&fds,NULL,NULL,&tv));
> }
>
>
>
> This code is nested in a while loop that just checks to see when were
> done sending. This works perfectly if no packets are dropped. but if
> the first ACK from the server is dropped, it goes into a loop, and
> even if the gateway forwards information with the ACK later to the
> client, it never shows that select() chooses to get info from the
> socket. i dont know why. does anyone?
You need to do the FD_SET(sock, &fds) each time in the while loop, since
select() modifies fds to indicate which sockets were ready. In the
timeout case, it unsets the bit for sock, since it's not ready.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.