Results 1 to 10 of 10

Thread: SDL_Net Destroying socket problems..

  1. #1

    SDL_Net Destroying socket problems..

    Hi,

    I've finally got a server and client working, thanks to the tutorials by technomage , Everythings pretty much working great, except when i try to free all the sockets in the server... i have the following code:

    Code:
    procedure TXServer.Finalize;
    var
      i: Integer;
      socket: PTCPSocket;
    begin
      SDLNet_UDP_Close(fUDPSocket);
      For i := 1 To fAllSockets.numsockets-1 Do
      begin
        socket := PXTCPSocketArray(fAllSockets.sockets)[i];
        SDLNet_TCP_Close(socket);
      End;
      SDLNet_TCP_DelSocket(fAllSockets, fTCPSocket);
      SDLNet_FreeSocketSet(fAllSockets);
      fConnected := False;  
    end;
    i always get an access violation on the following line:
    Code:
        socket := PXTCPSocketArray(fAllSockets.sockets)[i];
    I dont get any troubles with the server and client running, just when i shutdown the server.

    Any ideas would be great thanks
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  2. #2
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    SDL_Net Destroying socket problems..

    Try freeing them drom the last in the array to the first

    [pascal] For i := fAllSockets.numsockets-1 downto 1 Do
    begin
    socket := PXTCPSocketArray(fAllSockets.sockets)[i];
    SDLNet_TCP_Close(socket);
    End; [/pascal]

    If its using a list and you free an item it may be removed form the list, making the end of the list smaller so when it tries the next one its an access violation because there is no longer an object in that position.

    (This is a guess and may be incorrect)
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  3. #3

    SDL_Net Destroying socket problems..

    And, actually due to the optimizer, you may also have to do this in a while loop so that the array pointer doesn't get optimized down.[pascal]procedure TXServer.Finalize;
    var
    socket: PTCPSocket;
    begin
    SDLNet_UDP_Close(fUDPSocket);
    while fAllSockets.numsockets>0 do
    begin
    socket := PXTCPSocketArray(fAllSockets.sockets)[fAllSockets.numsockets-1];
    SDLNet_TCP_Close(socket);
    end;
    SDLNet_TCP_DelSocket(fAllSockets, fTCPSocket);
    SDLNet_FreeSocketSet(fAllSockets);
    fConnected := False;
    end;[/pascal]

    This is what I had to do in my TCP wrapper for SDL, its on the board some place if you want a reference. Something about some versions of FPC and/or Delphi optimizing down the array pointer for speed improvements. At times, SDL will move pointers in the background for you. As William said though, you do have to go from the top of the list to the bottom

  4. #4

    SDL_Net Destroying socket problems..

    Thanks for the replys
    I shall try them soon, and let you know what happens.
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  5. #5

    SDL_Net Destroying socket problems..

    Thanks, both options worked

    Sorry, but another question.. in the examples a string was sent as a PChar, in mine im using Records containing a cardinal, shortstring and an integer this passes fine through the tests i've done locally, but i just want to know that this is going to work over a network/internet, since my pc's is in pieces at the moment i cant test it either way... so does anyone know if this will cause problems?
    I am planning to expand the record too, to maybe a class with the player information, etc..
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  6. #6

    SDL_Net Destroying socket problems..

    Shouldn't be a problem, as long as your sending a CRC that you can test against to make sure you load against the proper record format. This is where handshaking and packet headers start to come into play. Its also the most common place to get memory overruns that allow hackers to wreak havoc on a system.

  7. #7

    SDL_Net Destroying socket problems..

    Cool thats great, il have to experiment a little... i was thinking about having a record like below:
    Code:
    TNetworkPacket = Record
      Hash&#58; THash;
      MsgClass&#58; TNetworkMsg; // Class
    end;
    The Hash being of course an MD5/SHA1 Hash of the TNetworkMsg class, and possibly have the class itself as an encrypted stream.. i will have to play around with it... since on the subject of hackers, when they hack into the server would it appear as a normal player is connecting? i mean i have an event that captures when a new connection comes in, would this event also occour with a hacker? sorry if its a dumb question :?
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  8. #8

    SDL_Net Destroying socket problems..

    every connection is treated equally so yes, hackers are just malicious players you could say....they have to connect on the same port your server uses to send/receive data so you cannot distinguish them...perhaps you can add a check for unknown packet type and as soon as you encounter one, you disconnect the offending connection....add a counter for 3 such violations from the same IP and ban it for a few minutes....

    you can encrypt packet headers to make packet tinkering a lot harder....by encrypting the header (opcode + data size) you can make reverse-engineering your packet structures a lot harder.....computing MD5/SHA for every packet sent is an overkill because they are slow plus they take up a lot of extra bytes....CRC might be better, on the other hand it's so common that once hackers would find out it's CRC, they could calculate CRC of the altered data and you wouldn't detect it....

    so the new packet structure could be...

    [pascal]TNetworkPacket = packed record
    Size: Word;
    Opcode: Word;
    { Data: array[0..Size-1] of Byte }
    end;[/pascal]

    where first 4 bytes would be encrypted...that way you would not know the type of data being transfered nor its size....in a typical client-server architecture requiring a secure connection there is an authentification process, where client and server exchange data and generate some key in the process in a safe way (without ever transmitting the key)...then use the key to encrypt further communication....if you need references for such algorithms, search google.....one relatively common one and very secure one is SRP (secure remote protocol)...it involves some biginteger math but other than that it's relatively easy to implement when you have a biginteger library at your disposal...

  9. #9

    SDL_Net Destroying socket problems..

    Just to point out, that the common way to attack a game server isn't to write a complete client. Instead you develop a specialized proxy that lets you inject your messages at will. From there a custom client can (and typically will) be built out to toy with the game.

    Encryption/Decryption is a great thing, but it takes processor speed and doesn't actually appear to slow down hackers. Instead it seems to give them more of a reason to jack with you.

    Focus on a simple CRC and packet size along with other knowns such as a rolling packet id and packet type id. These add minimal overhead to your data, and should be able to keep your game safe if implemented properly.

    Of course, and I'm guessing here, your so far away from a real game being available on the web to 1000's of people with a massive players list that you really don't have anything to worry about yet. Might be better to simply focus on completing the game and keep the transmit/receive stuff in specialized objects or methods. This way, as the game progresses you can add in security features as they are needed.

  10. #10

    SDL_Net Destroying socket problems..

    Great thanks for the suggestions

    I guess i didnt think about how many times it will have to cipher everything :s, and would probably cause more problems, i have thought of another way to this... i was think that when the client first makes a connection it sends 2 CRC's or Hash strings, 1 being the players username and the other being a random hash, then these will be stored in both the communication will be ignoured unless these 2 match?

    Encryption is'nt really what im worried about since nothing secure needs to be sent, except in the first message, all other communications will only be that of position data.

    Yeah the game is far from complete, its just a case of getting it out of the way ops:
    And because my world engine and scene editor are heavily mixed in with the server/clients i thought it would be good to get it done now.
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •