Home > Net >  C, socket communication problems, the accept function sometimes need to press the Enter key to conti
C, socket communication problems, the accept function sometimes need to press the Enter key to conti

Time:09-29

I written in c + + socket procedures client and server communication, there has been a very strange phenomenon:
On the server side and client communication under the condition of normal links, stop the client communication program, if the client communication program stop time is longer, once again open the client communication program, link is normal, while the server-side program prompts Waiting Connect... State, have been waiting for, in this case just press the keyboard enter key, the server program to link success, this kind of situation and sometimes without, the

Exclude a which, more urgent, online urgent!

CodePudding user response:

Sure is a question of procedure

CodePudding user response:

Of the server socket to disconnect, waiting for a new client to link, the server is always active recv, network disconnect after recv returns 0, GetErrorCode has to be used to determine whether the network connection disconnected (synchronous mode need not, can be directly determine disconnected)

Your client's prompt problems! (and possibly server logic problem)
Analysis is as follows:
A: no problem of your server, as described in I start there
Your client only need to send it again, if the server is not a bug, so at this time the client send fail, sent return zero zero bytes, socket synchronous mode here can directly determine the socket broken links, your client need tip: with the server's network connection has been disconnected, please connect to the server again and again to send,
Under asynchronous need GetErrorCode it and see whether wouldblock (need to wait to resend network congestion), otherwise the same as the above link disconnected, suggested above,

Server has a bug, and logic you want to rewrite the,