c++ - Concern over handling bad call to accept() -


I am writing a server for personal learning purposes and I happily managed to wrap the socket stuff up I'm certain sections and everything seems to work correctly; The server hears and accepts for the connection and currently takes the text from the client and sends it back.

I am not convinced of what to do with accepting the call () which gives me something other than WSAEWOULDBLOCK or a valid socket, I just reset the new socket to 0 And maybe an error message was returned by saying something bad? This is what I am currently doing, besides 20 times I will shut down the server. MUDControlSocket :: poll () {// Create a new connection here timed out; FD_ZERO (& ReadSet); FD_ZERO (& amp; WriteSet); FD_ZERO (& ExceptionSet); TopSocket = GetSocket (); NewSocket = 0; FD_SET (GetSocket (), and Reset); If (SocketList.size ()> 0) {for sockIter iter = SocketList.begin (); Iter! = SocketList.end (); ++ iter) {FD_SET ((* iter) -> GetSocket (), & ReadSet); FD_SET ((* Eater) -> GetSocket (), & WriteSet); FD_SET ((* etter) -> GetSocket (), and exceptionsets); Topsocket = (* Eater) - & gt; GetSocket (); }} If (Select (TopSocket + 1, and Reset, and WriteSet, and Exceptions, and Timeout) == SOCKET_ERROR) {cout & lt; & Lt; "Error on selection () Call:" & lt; & Lt; Socket error type (WSAGetLastError ()) & lt; & Lt; Endl; Erase it; Exhaust (EXIT_FAILURE); } / / As long as everything is working correctly, a new connection should be accepted if the block should always be entered if ((NewSocket = accept (GetSocket (), NULL, NULL) = = INVALID_SOCKET) {if (WSAGetLastError () == WSAEWOULDBLOCK) // This is not a real problem, just nothing to connect to come back yet; NewSocket = 0; Static int calculation = 0; Cout & lt; & Lt; "Error accepting new connection:" & lt; Socket error type (WSAGetLastError ()) << endl; if (++ calculation And gt; = 20) done = true; return;} SocketList.push_back (new MUDSocket (NewSocket)); // is accepted only when the returns of INVALID_SOCKET do not return, a new connection was accepted TopSocket = NewSocket Socket List is a std :: list of MUDSocket * and MUDControlSocket MUDSocket as a singleton.

Let me know that thank you for more information and any help. The Vshykta.

In addition, I would say just increase the exception: unless you run out of resources, then accept should not fail, which should be both extraordinary and errorMinister for ++ is a mechanism to handle such things, and this is the exception.

BTW: Delete it Almost always a very bad idea, which goes out in the middle of your code, it can make it difficult to debug (leave an exception) And let the caller exit when you need it and instead of trying to accept the socket from accept you can use the select to tell you whether it is acceptable - And to take special case to deal with the function Find new only. You can go a little further and apply a special supervisor pattern (as I did a month ago) not only to practice your networking code, but also to design design patterns. This will also help in making your code more portable, and will be able to be used again.

HH

Comments