Socket closed/client disconnects

About Monkey 2 Forums Monkey 2 Programming Help Socket closed/client disconnects

This topic contains 11 replies, has 2 voices, and was last updated by  Hezkore 1 year, 6 months ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #10679

    Hezkore
    Participant

    As a TCP listen server I’m trying to detect when a user disconnects from a socket.
    Here’s my function:

    That always returns true though.
    Even after the client has disconnected, either by just closing the app or doing socket/stream.Close() on his end.
    It never seems to timeout either, I’ve waited 5 minutes for it to time out.

    It’s the same thing the other way around.
    If I, as the server, close a socket, that client is never notified and thinks it’s still connected.
    Infact! Even if I don’t do socket.Accept(), the client still thinks he successfully connected.

    #10680

    Mark Sibly
    Keymaster

    Socket.Closed just tell you if the local end of the socket connection has been closed, so will be false until you call  Socket.Close(). SocketStream.Eof just calls Socket.Closed.

    To tell if the peer socket has been gracefully closed (ie: not crashed), you currently have to test for 0 bytes read or written.

    This is the ‘clean’ way to do it, but I think a ‘Connected’ style property here would also be nice. SocketStream can use this for Eof too.

    #10681

    Hezkore
    Participant

    But when I’m not actually sending something, CanReceive and Read will be 0 even if they’re still connected?

    UPDATE: I think I get it now, thanks Mark 🙂
    But it would be very nice if there was a Connected property to check against.

    #10684

    Hezkore
    Participant

    There seems to be some junk print messages when a socket is closed by just closing the application.
    I keep getting stuff like:
    ‘socket_recv error! err=0, msg=No error, socket=5756, data=00BC656C, size=1’
    It seems like it also does it sometimes even if the socket is closed correctly… hmm…

    I also can’t seem to be able to connect to myself over the internet.
    I’m not using a router of any kind, so no DMZ to setup, I also have no firewall.
    Using Listen( “”, 4012 )
    Connecting to ‘localhost’ doesn’t work, ‘127.0.0.1’ doesn’t work, my own IP from the same computer doesn’t work.
    It just timesout when I try to connect.
    I’m running other servers on the computer, so it should be able to accept incoming connections.

    Easy to test with the ‘echoserver.monkey2’ example.

    UPDATE: Okay so..
    If you’re using a router you have to Listen( local_IP, port ) and then you can connect to your public IP.
    If you’re hosting without a router, you have to do Listen( public_IP, port ) and connect to the public IP.
    Listen( “”, port ) never works.

    And M2 doesn’t seem to be able to fetch your IP.
    So a bit of an issue there.

    #10753

    Mark Sibly
    Keymaster

    I’ve just pushed a new version of Socket. Bind and Listen now have simpler interfaces:

    SocketFlags.Passive means the socket can be connected to over a network. If this flag is not set (eg: flags parameter is Null), socket can only be connected too via loopback (ie: from same computer). Well, that’s the theory anyway.

    This stuff is a bit different than everyone may be used to because I’m trying to keep things ipv6 friendly.

    I’ve tested this with echoserver across a LAN – just change HOST on client to network address of server, and comment out ‘New Fiber( Server)’ on client machine and ‘New Fiber( Client )’ on server!

    You can’t curently use std.socket to get the network address of a computer. The problem here is that a computer may have several network addresses, eg: an ipv4 address, an ipv6 address and maybe even more. There’s probably a way around this eg: something like EnumHostAddresses, but for now you’ll need to use some other mechanism to find out the computer’s network address, eg: ipconfig on windows or system prefs->network on macos.

    Diagnostic messages will eventually be cleaned up, probably moved to a log system. Don’t worry about them for now.

    #10897

    Hezkore
    Participant

    It seems connecting across the internet still doesn’t work, even when using SocketFlags.Passive 🙁

    UPDATE: I’ve tried my own code and I’ve separated the example code into a server and client and it never works across the internet.
    I’ve had friends try the server and client and connecting to each other online and it sadly never works.

    #10926

    Hezkore
    Participant

    Alright so more testing and more problems.

    It seems UDP works over the net, at least for connecting.
    But it can’t send any data.

    socket_recv error! err=10054, msg=Unknown error, socket=5668, data=0A60FF80, size=4

    #10946

    Mark Sibly
    Keymaster

    Just tried both echoserver and echoserver_udp with nerobot in russia and both worked fine. Perhaps start with getting those working?

    #10947

    Hezkore
    Participant

    Those are the ones I’ve (we’ve) tried… 😮
    Who hosted of you two? The issues is the server.

    Could you share the code?

    #10950

    Mark Sibly
    Keymaster

    I hosted via port forwarding.

    The code is just the bananas code, but with the new client or server fibers commented out in the ctor.

    #10951

    Mark Sibly
    Keymaster

    I’m still running echoserver_udp in server mode, try and connect if you want – email me for IP/PORT.

    #10952

    Hezkore
    Participant

    Can you also connect to your public IP locally?
    So you host the server on your computer, have people connect to it via your public IP and you yourself connect to the same server by using your public IP from the same computer hosting the server?

    There’s 4 of us trying right now, both to our own IPs and to each others without luck. 🙁

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.