Winsock send() stops working after a few uses

Posted on 2003-11-29
Last Modified: 2010-04-07

It seems I have ran into another problem with my client/server application.

Some background on my problem.
Im using Visual Studios C++
All Console Apps

I have a client that connects to a server. Right now the client sends a object to the server and the server prints out the objects values to its screen. Object and its attributes:

struct Player
    char Name[15];
    char Msg[4096];

Example Message to server:
                       Ericka: I sent this message from the client.

Everything compiles and works nicely as its suppose to do. However after I send messages to the server 8 times, my server will no longer print out anymore messages. I have debugged the client and have verified that the PlayerObjs values are being updated before the client attempts to send the object.

Another strange thing is if I continue to try to send messages after the server stops printing them out on its console, the client stops printing out my message on the client console after the 16th attempt. If I close out the server application before the client application I get a

ERROR: Send failed...

message (on my client console) for each send attempt that didnt print out on the server side. I wrote the check for the ERROR message to let me know that a send was unsuccesful.

It looks to me that the send attempts are getting backlogged or something somehow and when I shutdown the server they then attempt to be sent to the server and I get a ERROR: Send failed message because it has lost connection. Why would the send()'s stop after only 8 times of sending data? This program is very small and simple so I dont see how the socket could be getting bottlenecked or something. Im very new to network programming. If anyone has any ideas on how to fix this I would be very grateful.

Thanks in advance.
Question by:Ericka123
  • 5
  • 3
LVL 45

Expert Comment

ID: 9848596
Ericka, can you post the whole code ?

Author Comment

ID: 9850420
This problem has seemed to fix itself. The problem Im running into now is looking like my recv() call.

Everything works fine if its all running on one pc. If the client is running on the same pc as the server it send()s and recv()s fine. If a client is running on a seperate pc it recv()'s messages just fine, however if the client on the separate pc trys to send() a message then the server and all the clients gets jibberish that fills up the entire screen. It looks like its printing out the total size of the buffer and not just the used portion.

Im using ReadConsoleInput() to detect keypresses and ioctlsocket() for a non-blocking recv(). It wasnt doing all this until I tried to create a non-blocking recv() call with ioctlsocket() yesterday.

My code isnt extremely large but I do have it in separate .h and .cpp files for easier readability. I could post it but it would be a bit of a pain. Could I just email it to you? My email is


Author Comment

ID: 9850642

Im trying to duplicate the results of this. I find that if I run the server on my second pc with one client on the same pc as the server and one client on my first pc that I get the above mentioned results.

However if I run the server on my first pc with one client on the same pc as the server and a client on my second pc that everything works fine except that the client on the second pc gets total gibberish but the first with the server and a client work totally fine. The client running with the server gets all the sent messages that the second client sends along with the server.

I know none of that probally makes since hehe. Im starting to wonder if this might have something to do with me running multiple clients on my little home network? Im using 2WIRE, could this have something to do with it?

Wouldnt it give duplicate results if it had something to do with my coding?
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud


Author Comment

ID: 9865022
I fixed the above problem already. Well sorta. Now everything works 100% correctly if the clients are in debug mode. If they arent in debug mode it prints out the entire buffer and the data in the buffer is garbage.

I put breaking points right after the recv() calls on the client. As I "F5" through the breakpoints all the varibles are correct and it correctly prints the message out on the client. I have also noticed that the client that is running on the same pc as the server is working fine in or out of debug mode. Only clients on separate pc's seem to be having this problem.

Im hoping this is something very trivial and that someone might have ran into this before. Im going crazy with this please help.

So far I can put my experience of learning net programming in one sentence.
"Its been like breaking into Fort Knox with a dull spoon."  I am enjoying it though, just seems like a uphill battle for me hehe.

Anyways, Thanks in advance
LVL 45

Expert Comment

ID: 9865083
I am sorry ... I have been keeping a bit busy lately ... I shall try to look into it and post back

Author Comment

ID: 9865090
Since I posted this I tried out a idea and thought maybe the debugger was stopping the program at the breakpoint so I put in    
and it jury rigged my problem. It is now printing out the message correctly instead garbage. Can anyone explain whats going on with this? Surely my pc isnt too fast. Why does it need to slow down while accepting the message from the server? And can someone explain how to achieve this same result without using Sleep(); ?

Accepted Solution

Ericka123 earned 0 total points
ID: 9865152
Hiya Sunny!

Sorry didnt see you before I made my last post. Its ok, I think I figured it out now.

I forgot that I used ioctlsocket(); when I was trying to figure out how I should make my socket not get blocked by recv() calls. As I was looking over my code I knoticed it and commented it out and the sleep() call and it seems to be working properly now. Hopefully its good to go finally.

I used FD_SET and select() on my client to see if the server had any data ready to be read so it wouldnt block my recv() call. I had just forgot to take the ioctlsocket out after I went that route instead.


Thread Closed
LVL 45

Expert Comment

ID: 9865163
thats great ..
you can post in community support to get your points refunded

good luck

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question