4

1) The socket doesn't seem to unbind from the LocalEndPoint until the process ends.
2) I have tried the solutions from the other question, and also tried waiting a minute - to no avail.
3) At the moment I have tried the below to get rid of the socket and its connections:

public static void killUser(User victim)  
    {  
        LingerOption lo = new LingerOption(false, 0);  
victim.connectedSocket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.Linger,     lo);  
        victim.connectedSocket.Shutdown(SocketShutdown.Both);  
        victim.connectedSocket.Disconnect(true);  
        victim.connectedSocket.Close();  
        clients.RemoveAt(victim.ID);  
    }  

4) After a bit of googling, I can't seem to be able to unbind a port, thus if I have a sufficient amount of connecting clients, I will eventually run out of ports to listen on.

casablanca
  • 69,683
  • 7
  • 133
  • 150
ryan1894
  • 43
  • 1
  • 2
  • 4

1 Answers1

10

I suspect you are confusing the sockets of your connected clients with your server socket.

Your server socket is the one listening for incoming connections on a specific port. The socket you close in that function is your pipe to one of (potentially many) remote connections.

To "unbind the port", you'll want to Shutdown/Close the server socket.

Update to clear some confusion

You should have a "server" socket that you made a call to .Bind(EndPoint) on, followed by a call to .Listen(). This is the socket you want to Shutdown/Close to "unbind" and free up a port for later.

You then have multiple "client" sockets that you get references to whenever your "server" socket accepts a new connection. These can all be bound to the same port without problem. To close one of these connections and disconnect your client, do what you're doing now. You can actually trim the method down to:

  • Shutdown
  • Close
  • Remove from your list

Disconnect and the rest are unnecessary.

Sapph
  • 6,118
  • 1
  • 29
  • 32
  • I was just wondering how to kill a connection, and free up the port when a user unsafely disconnects or even just disconnect. EDIT: The socket I will potentially want to close would be one of many client sockets. ATM I have one socket listening for potential clients, then the client receives a port number to reconnect onto: considering only 1 client can connect per port/1 socket per port. (I think) – ryan1894 Jan 07 '11 at 06:36
  • Your "one socket listening for potential clients" had a call to .Bind() at some point, right? Where you specified an EndPoint (which should have had a port associated with it)? That port is the single port your clients should be connecting to (and you can have many client sockets connected on that single port). What you **can't** do is start a **second server socket** listening for connections on the same EndPoint until you unbind your **first** server from it (by closing that first socket). – Sapph Jan 07 '11 at 06:42
  • "until you unbind your first server from it (by closing that first socket)." Which is exactly what I'm having problems with.EDIT: The situation is: I'm listening on port 5900. Connection received -> reconnect back on 5901. Connection success. Client disconnects, and so I try to free up 5901 -> then try rebind -> exception – ryan1894 Jan 07 '11 at 06:45
  • Then refer to my original answer. :P – Sapph Jan 07 '11 at 06:46
  • The code I posted in my OP is the code I'm using, and it ain't working >_> – ryan1894 Jan 07 '11 at 06:49
  • I updated my post to distinguish between the sockets a bit more. Hopefully it helps, if not, let me know if you're still confused. And when you say "not working", what do you mean? Are you getting an Exception somewhere? Which Exception is it? What code causes the problem? Basically, what problem prompted you to ask a question? :) – Sapph Jan 07 '11 at 06:49
  • 1) I never start a second listening socket. I only ever start a potential multitude of Sockets that just sit there and BeginReceive/BeginSend. 2) Its just that I want to reuse ports SHOULD have already disconnected. 3) BTW its a console app so it seems to crash silently... 4) Essentially my question is: Why is my program crash? I've already released the port (I think) thus: this thread. – ryan1894 Jan 07 '11 at 06:51
  • Any crashing is almost certainly unrelated to "reusing ports" if you aren't starting a second server socket. My advice is to find the line of code that's causing the crash and work from there. You don't need to fret about clients taking up resources as long as you close the sockets when they disconnect (using client.connectedSocket.Close()), and then making sure you call .Close() on your server socket when you're done with everything. – Sapph Jan 07 '11 at 06:55
  • 1
    Whoa wait a minute, I just read your above edit. When a client connects on 5900, why are you reconnecting them to 5901? That's unnecessary, just keep them on 5900. – Sapph Jan 07 '11 at 06:57
  • 1
    Hmm I reread my code and the line that is causing it to crash is tempSock.Bind(IPEP); which is because I'm wanting to reuse a port. I'm wondering if its possible to reuse a port - which it should be (I assume)... "That's unnecessary, just keep them on 5900" I want to keep 5900 as a port to initially connect to: and the rest as ports to use for actual data transfer... If I don't reassign ports to all clients then clients will have to manually check which ports are in use or not etc. – ryan1894 Jan 07 '11 at 06:58
  • 2
    You should only ever call Bind **once**, and that should be called only by your server socket (the one that you are also using to call Listen). Once your clients (as many as you like) connect on 5900, there's no need to reconnect them anywhere. You can have all of them on 5900 at once. If you do want to reconnect, you'll have to set up additional server sockets Listening on the other ports. – Sapph Jan 07 '11 at 07:00
  • I hope it works out for you! If you still can't puzzle it out later, feel free to post a comment and I'll check back tomorrow. Going to bed now though. :) Sockets are pretty fresh in my mind because I've been working on a library to handle it all for me. – Sapph Jan 07 '11 at 07:05