断开/关闭客户端

时间:2013-02-07 06:32:54

标签: c# sockets tcpclient tcplistener

我有一个简单的客户端 - 服务器应用程序,它的工作原理如下:服务器总是监听(在一个单独的线程中)客户端连接(它发送它希望服务器终止的进程名称)。

这是服务器:

private void btnStart_Click(object sender, EventArgs e)
{
  _port = int.Parse(comboBoxPorts.SelectedItem.ToString());

  _tcpListener = new TcpListener(_ipAddress, _port);

  _keepRunning = true;
  _listenerThread = new Thread(Listen);
  HandleListenerThreadStartListenEvent += HandleListenerThreadStartedEventMethod;
  ListenerThreadStartedEvent += HandleListenerThreadStartListenEvent;
  _listenerThread.Start();
}

private void btnStop_Click(object sender, EventArgs e)
{ 
  if (_tcpListener != null)
  {
    _keepRunning = false; 

    if (_tcpListener.Server.Connected)
    {
      _tcpListener.Server.Disconnect(true); 
    } 

    _tcpListener.Stop(); 
  }

  labelServerStatus.Text = "Server is stopped";   
  comboBoxPorts.Enabled = true; 
  btnStart.Enabled = true; 
  btnStop.Enabled = false; 
} 

private void Listen()
{
  try
  {
    _tcpListener.Start();
    OnListenerThreadStartListenEvent(); // just update the GUI
  }
  catch(Exception e)
  {
    MessageBox.Show("Port " + _port + " is NOT available." + Environment.NewLine +
                    "Please choose another one: " + e.Message);
    return;
  }

  _keepRunning = true;
  string ballonMessage = "Socket Server Running at " + _ipAddress + ", port: " + _port;
  notifyIcon1.ShowBalloonTip(2000, "Simplex Listener", ballonMessage, ToolTipIcon.Info);

  while (_keepRunning)
  {
    try
    {          
      #region using AcceptSocket()

      _clientSocket = _tcpListener.AcceptSocket();
      string checkString = string.Empty;
      IPAddress ipOfClient = ((IPEndPoint) _clientSocket.LocalEndPoint).Address;
      notifyIcon1.ShowBalloonTip(2000, "Simplex Listener", "New client has connected from ip " + ipOfClient, ToolTipIcon.Info);

      byte[] buffer = new byte[SIZE_OF_BUFFER];
      int bytesReceived = _clientSocket.Receive(buffer);

      // Concatenate chars as bytes to a received string.
      for (int i = 0; i < bytesReceived; i++)
        checkString += Convert.ToChar(buffer[i]);



      //..... getting the name of process and kill it (and than open it...
      RestartProcess(nameOfProcess, windowName, pathToExeFile);

      // Client is waiting to know operation is complete- so send him some char...
      ASCIIEncoding encoder = new ASCIIEncoding();
      _clientSocket.Send(encoder.GetBytes("v"));
      _clientSocket.Disconnect(true);
      _clientSocket.Close();
      #endregion
    }
    catch (Exception )
    {    
    }
  }
}

客户方:

public void RestartTheSoftwareInServerComputer(string ipOfServer, int portNumber)
{
  TcpClient client = new TcpClient();

  if (_serverEndPoint == null)
  {
    _serverEndPoint = new IPEndPoint(IPAddress.Parse(ipOfServer), portNumber);     
  }

  client.Connect(_serverEndPoint);

  // Send the command to the server:
  NetworkStream clientStream = client.GetStream();
  ASCIIEncoding encoder = new ASCIIEncoding();
  byte[] buffer = encoder.GetBytes("....detailsOfProcess....");

  clientStream.Write(buffer, 0, buffer.Length);
  clientStream.Flush();

  // Now, wait for the server's response [which means the process had been restart].
  NetworkStream stream = client.GetStream();
  byte[] bytes = new byte[5];
  stream.Read(bytes, 0, 5);
  string response = Encoding.UTF8.GetString(bytes, 0, 1);

  if (response.Equals("x"))
  {
    throw new Exception("Failed to restart X software.");
  }

  stream.Close();
  client.Close();
}

当我停止并重新启动服务器时(没有连接客户端时),一切正常。

问题是当服务器连接了一些客户端并重新启动时,客户端已断开连接,需要重新启动服务器。当我们再次点击“START SERVER”时,它将获得异常:

  

每个套接字地址只有一种用法(协议/网络地址/端口)   通常是允许的。

我应该如何关闭端口?

1 个答案:

答案 0 :(得分:0)

退出服务器时,应调用_tcpListener.Stop()来关闭服务器正在侦听的主套接字。

编辑:您也可以尝试在停止按钮单击中调用_listenerThread.Join(),等待侦听器线程完成,然后再启动下一个。

private void btnStop_Click(object sender, EventArgs e)
{ 
  if (_tcpListener != null)
  {
    _keepRunning = false;

    if (_tcpListener.Server.Connected)
    {
      _tcpListener.Server.Disconnect(true); 
      _tcpListener.Stop();
      if (_clientSocket != null)
      {
        _clientSocket.Close();
        _clientSocket = null;
      }
      _listenerThread.Join();
    }
  }

  labelServerStatus.Text = "Server is stopped";   
  comboBoxPorts.Enabled = true; 
  btnStart.Enabled = true; 
  btnStop.Enabled = false; 
} 

编辑2:

这是一个与您的服务器类似的Windows窗体。我不需要客户端,只需在命令提示符下使用“telnet localhost 49152”来“假装”为客户端连接。

public partial class Form1 : Form
{
    private TcpListener _tcpListener;
    private bool _keepRunning;
    private Thread _listenerThread;
    private Socket _clientSocket;


    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var address = IPAddress.Parse("127.0.0.1");
        _tcpListener = new TcpListener(address, 49152);

        _keepRunning = true;
        _listenerThread = new Thread(Listen);
        _listenerThread.Start();

        button1.Enabled = false;
        button2.Enabled = true;
    }

    private void Listen()
    {
        try
        {
            _tcpListener.Start();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            return;
        }

        _keepRunning = true;

        while (_keepRunning)
        {
            try
            {
                _clientSocket = _tcpListener.AcceptSocket();

                var buffer = new byte[8192];
                var bytesReceived = _clientSocket.Receive(buffer);
                var checkString = String.Empty;

                if (_keepRunning)
                {
                    // bytesReceived can be 0 if the remote socket disconnected
                    if (bytesReceived > 0)
                    {
                        checkString = Encoding.ASCII.GetString(buffer, 0, bytesReceived);

                        // Client is waiting to know operation is complete- so send him some char...
                        var encoder = new ASCIIEncoding();
                        _clientSocket.Send(encoder.GetBytes("v"));
                    }
                    if (_clientSocket.Connected) _clientSocket.Disconnect(true);
                }
                _clientSocket.Close();
            }
            catch (Exception ex)
            {
                // really should do something with these exceptions
            }
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (_tcpListener != null)
        {
            _keepRunning = false;

            if (_tcpListener.Server.Connected)
            {
                _tcpListener.Server.Disconnect(true);
            }
            _tcpListener.Stop();

            if (_clientSocket != null)
            {
                _clientSocket.Close();
                _clientSocket = null;
            }
            _listenerThread.Join();
        }

        button1.Enabled = true;
        button2.Enabled = false;
    }
}

此代码存在许多问题,例如在线程之间共享变量等,但在我的机器上,Join似乎没有阻塞任何时间长度。 _keepRunning的问题在于,在某些系统上,一个线程可能无法看到从true变为false的变化,因为它被优化或缓存。你应该使用某种形式的线程同步,使它变得易失,或将它包装在锁等中。我建议你阅读here。我也建议您也阅读套接字,或者如果其他评论者提到过,如果您对了解套接字和线程的所有特性不感兴趣,也许您应该寻找一个更高级别的库来隐藏这一切?