开发者

Start listening again with Socket after a disconnect

开发者 https://www.devze.com 2023-01-28 15:12 出处:网络
I\'m writing a small C# Sockets application. Actually I have two, a server and a client. The user runs the client, enters the IP and port for the server, presses \'connect\', and then once connected

I'm writing a small C# Sockets application. Actually I have two, a server and a client.

The user runs the client, enters the IP and port for the server, presses 'connect', and then once connected they can enter text into a textbox and send it to the server.

The server simply displays either "No connection" or "Connection from [ip]:[port]", and the most recent received message underneath.

The server successfully receives messages, and even handles the client disconnect fine. Now I'm trying to make it listen again after the client has disconnected but for some reason nothing I try will allow it to start listening again.

Here is part of my code:

    Socket socket;
    private void listen()
    {
        socket = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);
        socket.Bind(new IPEndPoint(IPAddress.Any, 12345));
        socket.Listen(10);
        socket.BeginAccept(new AsyncCallback(acceptAsync), socket);
    }

and

    private void receiveAsync(IAsyncResult res)
    {
        Socket socket = (Socket)res.AsyncState;

        try
        {
            int nBytes = socket.EndReceive(res);
            if (nBytes > 0)
            {
                Invoke(new MethodInvoker(delegate()
             开发者_开发百科   {
                    lMessage.Text = encoder.GetString(buffer);
                }));
                setupReceiveAsync(socket);
            }
            else
            {
                Invoke(new MethodInvoker(delegate()
                {
                    lConnections.Text = "No Connections.";
                    lMessage.Text = "No Messages.";
                    socket.Shutdown(SocketShutdown.Both);
                    socket.Close();
                    listen();
                }));
            }
        }
        catch { }
    }

The last line: listen(); is what throws the error. I have tried simply calling socket.BeginAccept() again, but that also throws an exception.

The message I'm getting is:

Only one usage of each socket address (protocol/network address/port) is normally permitted

If I don't call my listen() function and instead just call socket.BeginAccept(), then I get "You must first call socket.listen()"

If I call the socket.listen() function, then it tells me it's already connected and cart start listening.

Once I have made an asynchronous connection, and received several asynchronous messages, how then do I begin receiving again?


Your socket variable already has an listening socket assigned to it the second time you call listen(), which is why it tells you only one usage is permitted. All you need to repeat is the socket.BeginAccept(new AsyncCallback(acceptAsync), socket) call. So try replacing the call to listen() inside your receiveAsync(...) method with socket.BeginAccept(new AsyncCallback(acceptAsync), socket).


In async Begin* is always followed by End*. See Using an Asynchronous Server Socket. Your accept method should be something like:

try {
        listener.Bind(localEP);
        listener.Listen(10);

        while (true) {
            allDone.Reset();

            Console.WriteLine("Waiting for a connection...");
            listener.BeginAccept(
                new AsyncCallback(SocketListener.acceptCallback), 
                listener );

            allDone.WaitOne();
        }
    } catch (Exception e) {

Server, means that an app that listens on specified port/ip. Is usually, always in a listening mode - that's why it is called a server. It can connect and disconnect a client, but is always in listening mode.

This means, when a server disconnects a client - even then - it is in listening mode; meaning it can accept the incoming connections as well.

Though, disconnection request can come from client or can be forcefully applied by server.

The process for a server is:

  1. Bind to socket
  2. Listen
  3. Accept connections

The process for client is:

  1. Connect to the server
  2. Send/receive messages

There are several ways for server to handle the incoming clients, couple, as follows:

  • Incoming connections are maintained in a list, for instance within a List<TcpClient>.
  • One way of handling the incoming clients is through threads. For instance, for each incoming client, spawn a thread that would handle the communication between server and client. For instance, checkout this example.

_

private  void ListenForClients()
{
  this.tcpListener.Start();

  while (true)
  {
    //blocks until a client has connected to the server
    TcpClient client = this.tcpListener.AcceptTcpClient();

    //create a thread to handle communication
    //with connected client
    Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
    clientThread.Start(client);
  }
}
  • Use single thread and use context switching to manage client communications (TX/RX).
0

精彩评论

暂无评论...
验证码 换一张
取 消