Sean
Sean

Reputation: 105

C# program hangs on Socket.Accept()

I created a server "middleman" application that uses sockets and multi-threading techniques (ServerListener is run in a new thread). I found early on that when I would use the Socket.Accept() method, the program would hang indefinitely, waiting for that connection to happen. The problem is, as far as I can tell there is no reason for it not to.

I spent a good portion of the day trying lots of different things to make it work, and somewhere something changed because it suddenly started working for a while. However, as soon as I accidentally chose a different data source than "localhost" for the client application, the problem popped back up again. I have tried running the program without the firewall OR antivirus running, but no luck. The client program IS set to connect on port 10000. Here is my code:

    public void ServerListener() {
        UpdateStatus("Establishing link to server");
        server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        server.Bind(new IPEndPoint(IPAddress.Any, defaultPort));
        server.Listen(queue);
        UpdateStatus("Accepting Connections");
        while (true) {
            Socket client = default(Socket);
            try {
                client = server.Accept();
                if (client != null) {
                    ++count;
                    UpdateCount(count.ToString());
                    new Thread(
                    () => {
                        Client myclient = new Client(client, defaultPort, this);
                    }
                    ).Start();
                }
            }
            catch( Exception ex ){
                MessageBox.Show(ex.ToString());
                client.Close();
            }
        }
    }

It will run just fine right up until server.Accept(), then hangs. As stated, it did work for a while earlier, but is now hanging again. I've tried to see if any other programs are using port 10000, and they aren't. I went over and over this with a friend, and we couldn't find the problem. Please help!

EDIT To be clear, I do know that Accept is a blocking call. The client program makes the connection on port 10000, but this program keeps on waiting on the Accept as if nothing happened. It did work for a time, so I know the connection is working like it is supposed to from the client program's end. However, I can't fathom why this program is now acting like that connection never happens, and continues to wait on the Accept.

Upvotes: 1

Views: 6020

Answers (1)

Nikola Dimitroff
Nikola Dimitroff

Reputation: 6237

Accept blocks on purpose. If you want to do other things while waiting for another client to connect you can:

  1. Run the ServerListener in another Thread or better - a long running task:

    using System.Threading.Tasks;
    ...
    Task.Factory.StartNew(ServerListener, TaskCreationOptions.LongRunning);
    
  2. Use the AcceptAsync method which uses the SocketAsyncEventArgs class. For that to work, you create a new SocketAsyncEventArgs instance, set its values and pass it to socket.AcceptAsync.

Upvotes: 5

Related Questions