Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 06-04-2005, 09:58 AM   #1 (permalink)
TheSheep
Registered User
 
TheSheep's Avatar
 
Join Date: Mar 2005
Location: UK
Posts: 11
TheSheep is on a distinguished road
Send a message via ICQ to TheSheep Send a message via AIM to TheSheep Send a message via MSN to TheSheep Send a message via Yahoo to TheSheep
Socket problem [Win32, ws2]

I've set up a client/server application communicating over a TCP port. Without going into too much detail, the client sends a message, the server replies, the client replies, etc. etc. until the server has sent all its data and transmits a termination message. At this point the server disconnects and the client should do the same.

For whatever reason, the client seems not to receive the final message. Since the socks API is single-threaded I'd expect that the entire packet would be sent before the send() function even returns, so I'm fairly confident the message is actually sent. It's also not a loss or lag problem, since this even occurs over my loopback.

Can anyone suggest why, instead of returning (even if returning an error), the recv() function on the client just hangs on the final message?
TheSheep is offline   Reply With Quote
Old 06-04-2005, 10:29 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Just a thought.. Description of recv() tells you that
Quote:
If no messages are available at the socket, the receive calls wait for a message to arrive, unless the socket is nonblocking.
Could this be the case, it recieved the End Of Transmission message, but for some reason your recieve loop on the client runs one more time, and here it hangs in the recv() since theres no messages in the socket.

But since you didn't provide any code, it is hard to make any assumption on what is happening.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 06-04-2005, 12:41 PM   #3 (permalink)
TheSheep
Registered User
 
TheSheep's Avatar
 
Join Date: Mar 2005
Location: UK
Posts: 11
TheSheep is on a distinguished road
Send a message via ICQ to TheSheep Send a message via AIM to TheSheep Send a message via MSN to TheSheep Send a message via Yahoo to TheSheep
Thanks for the suggestion; I'll certainly look at the possibility but I'd expect it to receive the termination message in that case and respond to it.

Unfortunately it's not exactly easy to post up the code since it's spread between about 5 different modules. As an outline, the server and client are both running timers and checking receiving every 1/2 second, and are 1/4 second out-of-sync with each other (I figured this would be far more practical than some sort of negotiation since the max possible message size is 1KB). Each application receives the message, processes it (which only consists of quick memory I/O) and sends its reply.

I thought maybe there's some feature of the sockets API that I'm missing altogether. This is my first ever sockets program so kinda lends itself to disaster. Does initialising the API or socket allow the API to buffer input until it's retrieved or something?
TheSheep is offline   Reply With Quote
Old 06-04-2005, 02:48 PM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
What is your flags given to recv() ? You can control most of it's handling through those.. If you are using MSG_PEEK, then you have to realize that the message in the socket won't be removed, thus still be there on next read.
If you are using MSG_WAITALL, then it will block the socket queue untill the read is fully satisfied, so since you let your server app terminate, a read might return with EAGAIN or EINTR, and thus terminate the read befor it is completed, this will let what ever is left of the End Of Transmission signal remain in the socket, thus cludging up the recv() call on next read, since the remainder isn't matching what you'd expect as the termination signal.
Quote:
Does initialising the API or socket allow the API to buffer input until it's retrieved or something?
I havn't got my Unix Network Programming book with me where I am right now, so I can't say for sure, but you might wanna check errno for EAGAIN, to check for the blocking of the recv() call, or check for ECONNREFUSED to see if the server has terminated the socket.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 06-05-2005, 04:38 AM   #5 (permalink)
TheSheep
Registered User
 
TheSheep's Avatar
 
Join Date: Mar 2005
Location: UK
Posts: 11
TheSheep is on a distinguished road
Send a message via ICQ to TheSheep Send a message via AIM to TheSheep Send a message via MSN to TheSheep Send a message via Yahoo to TheSheep
I'm usings flags=0. The only flags supported under Win32 are PEEK and OOB which are no use to me.

I've managed to bypass the problem by using select() to check the readability of the socket but it seems slower.
TheSheep is offline   Reply With Quote
Old 06-24-2005, 05:38 PM   #6 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 56
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
It would be better to use WSAAsyncSelect() and do your recv() calls inside the message loop. That way you don't have to worry about when or if a message has been sent.
QUantumAnenome is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JSP code problem j.gohel Java 7 04-15-2005 03:07 PM
Hashing problem jodders Standard C, C++ 1 02-09-2005 02:51 PM
Problem Assignment (Urgent help req.) Boltress Standard C, C++ 0 01-12-2005 08:59 AM
Help debugging a power problem Belisarius Lounge 0 10-25-2003 05:44 PM
Winsock problem.. oops Standard C, C++ 2 06-25-2002 04:09 PM


All times are GMT -8. The time now is 01:57 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting