Unknown socket crash (10054) - C code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheGTA
    New Member
    • Jan 2009
    • 6

    Unknown socket crash (10054) - C code

    Hi there!

    I am working on a project for like 1 year now.
    Most of it works well.
    But somehow my socketenvironme nt craps out sometimes...

    For a totally unknown reason a socket crashes with 10054, connection refused.
    That socket usually processes easy packets with size being under 50, its a characterserver -> loginserver connection.
    Well, I found out it crashes after some time when the send() command got used.
    Before the "crashing" send() the app executed many other send()s, which are even the same functionspot, passing successfully.
    The odd thing is that only the server socket crashes, the socket at the client stays alive, which of course is being checked frame-by-frame by recv().
    Sadly, this "poison" goes over to another socket of the same DLL and makes it "crash", too, the client->characterserve r connection.

    Now I tell you about my "socketsyst em".
    The core application has a simple loop which looks like that:
    Code:
    while(IsActive())
    {
        FOR_EACH_SERVER
        {
            ServerDLLFrame();
            CheckNewConnections();
            HandleAllClients();
        }
        Sleep(1);
    }
    And this is the singlethreaded build option.
    Multithreaded looks similiar.

    CheckNewConnect ions() is a "accept" for my unencrypted and encrypted sockets.
    If accept() returns number above 0 I add this number as a socket to my list.

    HandleAllClient s() is a read-and-execute function.
    It checks all clients in a loop if they have anything to send to me or just disconnected, without a Sleep() seperating recv() calls.
    If recv() returns 0, client disconnected.
    If recv() returns -1, error occurred or no data in buffer (WSAEWOULDBLOCK ?).
    And if recv() got some number above 0 it has put something into buffer and I process this.
    Yea. All sockets are non-block.

    And the last thing, which I think surely wont matter:
    All servers are being run on the same application, 3 in total.

    But what now? I really dont know how to fix this "little" bug :/
    Do I need to let the app sleep 1msec after every recv() if I need to call a recv() afterwards?
    Is it needed to sleep 1msec before using send() after recv()?
    Is it okay to send 24kb data in one packet?

    Thanks for any help incoming :)
    TheGTA
  • TheGTA
    New Member
    • Jan 2009
    • 6

    #2
    dang...

    Alright, I have fixed this... So this topic may be closed or whatever :P

    Anyone knows SO_KEEPALIVE?
    What a cool socket setting, makes your socket "stay alive".
    I really didnt find this in the C socket tutorials out there, why?
    (I mean, in no code example...)

    My logical reason for the need of KEEPALIVE is the following:
    If a socket "stays" too long without responding to the other socket you have connected to, the OS thinks this socket is "dead".
    By reading some nice GNU TCP keepalive page I noticed something about an "ACK" signal in a "zero bytes long packet".
    So you need to send a packet with ACK from time to time to tell the receiver you are still connected with this?
    This also explains the disconnect, it thinks the connected to socket isnt connected anymore and simply deletes this socket without notice?
    I always thought there would be some automatic communication between TCP sockets to keep them alive (by default) but I was wrong.

    I hope this will help anyone who got into this trouble, too.
    Always "setsockopt(soc ket, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt));" your sockets, at "accept()" and "socket()" calls! :)
    (ONLY if the sockets are meant to stay for a longer period of time)

    Comment

    • osfreak
      New Member
      • Oct 2008
      • 22

      #3
      I have set the client socket on creation set to SO_KEEPALIVE

      I have set both the server socket and also the socket that is created in the accept call too


      Yet I get the same error :(

      Comment

      • TheGTA
        New Member
        • Jan 2009
        • 6

        #4
        Hmm.

        Alright, as I dont have your code I can only guess and give you some tipps:
        - In Windows, setsockopt() requires a "char" cast as opt value, did you make sure you dont give a int or short?
        - If yes, did you make sure the opt value is set to TRUE (1)?

        SO_KEEPALIVE was tested using non-blocking sockets.
        It might work else in blocked ones, idk.

        "setsockopt ()" changes OS settings, it doesnt matter if you do that before or after the connect.
        In my server I set this option to all clients which have connected, I just do that after I got connected, this means on every socket, server and client.

        Comment

        • osfreak
          New Member
          • Oct 2008
          • 22

          #5
          TheGTA ............. You did it,

          Thanks a lot :-> :->

          Comment

          Working...