c#tcp socket接收循环中断逻辑

时间:2015-04-03 10:53:09

标签: c# sockets tcp

我正在编写一个c#tcp套接字服务器,它将从c ++套接字应用程序接收数据。现在一切正常,除了我的c#接收线程中的循环中断逻辑。我在下面展示了一个代码片段,以便更好地解释。

    //creating socket object
    private Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    //accepting connections
     TcpListener tcpListener = new TcpListener(ipAdd, iPort);
     tcpListener.Start();
     this.socket = tcpListener.AcceptSocket();
     tcpListener.Stop();        

    //code for 
    //public byte[] ReceiveMessage(ref int intError)
    while (this.socket.Available > 0)
    {
      try
      {
        intBytesReceived = this.socket.Receive(byteReceivedBuffer, byteReceivedBuffer.Length, SocketFlags.None);
        Thread.Sleep(100);
      }
      catch (SocketException ex)
      {
        string str = ex.Message;
        intError = -1;
      }
    }

以下是连续接收数据的线程函数

  //Thread to continuous poll data from client
  //Now this has been done as I need to receive multiple messages from client after server ack received at client side. 

  //Thread quit logic based on manual stop button press
  //I want to have one more logic by which thread will exit if the connection closed by client socket. i.e. a call of 'CloseSocket()' function from c++ application.     

  if (TCPServerListener.serverStatus == TCPServerListener.ServerStatus.Stopped)
  {
    tcpCommunication.Disconnect();
    break;
  }

  while(true)
  {
    int sockError = 0;
    byte[] byteData = tcpCommunication.ReceiveMessage(ref sockError);
    if (byteData.Length > 0)
    {
      ParseBuffer(byteData, ref tcpCommunication);
    }
  }

为了更好的图片,我正在低于我的通信协议。

  1. 客户端发起连接。发送数据集
  2. 服务器接收,处理,发送确认
  3. 客户端处于暂停模式,直到从服务器收到确认,但未关闭套接字
  4. 这次,服务器不会收到任何消息,但接收线程应该处于活动状态,因为我正在为每个客户端创建一个线程。
  5. 一旦客户收到确认,它将继续发送下一个数据
  6. 服务器接收线程将立即获取数据并进行相应处理
  7. 一旦客户端关闭其套接字,服务器接收器线程也应该关闭
  8. 我能够完成我的工作,但服务器接收器线程没有关闭。那是我的问题。任何建议都将是一个很大的帮助。

1 个答案:

答案 0 :(得分:0)

根据我的经验,当您使用TCP服务器/客户端套接字关系时,TCP服务器套接字端将位于由标志控制的线程中,以便继续监听,直到被告知停止(您的评论提到停止按钮) )。

客户端运行直到完成所有数据,或者控制服务器端的相同标志被触发。

当客户端连接时,我会将AcceptSocket存储到列表中。当我这样做时,我有一个自定义类,它接受AcceptSocket和自定义类具有计数器,用于向该客户端发送/接收多少消息或字节。

// This should be a threaded method if you're using a button to stop
private void StartTcpListener()
{
    List<ClientClass> clientClassList = new List<ClientClass>();

    TcpListener tcpListener = new TcpListener(ipAdd, iPort);
    tcpListener.Start();

    while (keepListening)
    {
        Socket socket = tcpListener.AcceptSocket();

        // Create a client object and store it in a list
        ClientClass clientClass = new ClientClass(socket);
        clientClassList.Add(clientClass);

        // This method would start a thread to read data from the accept socket while it was connected.
        clientClass.Start();
    }

    foreach (ClientClass client in clientClassList)
    {
        // This method would stop the thread that reads data from the accept socket and close the accept socket
        client.Stop();
    }
    tcpListener.Stop();
}

对于客户端,当从客户端接受套接字读取时,这将是线程控制的,并且线程被中止或客户端关闭它的结束,导致读取为读取的字节数返回-1。

// Threaded method
private void Receive()
{
    // Don't know how much of a buffer you need
    byte[] dataIn = byte[1000];
    while (acceptSocket.Connected)
    {
        // Exception handling so you either end the thread or keep processing data
        try
        {
            int bytesRead = acceptSocket.Read(dataIn);
            if (bytesRead != -1)
            {
                // Process your data
            }
            else
            {
                // -1 Bytes read should indicate the client shutdown on their end
                break;
            }
        }
        catch(SocketException se)
        {
            // You could exit this loop depending on the SocketException
        }
        catch(ThreadAbortException tae)
        {
            // Exit the loop
        }
        catch (Exception e)
        {
            // Handle exception, but keep reading for data
        }
    }

    // You have to check in case the socket was disposed or was never successfully created
    if (acceptSocket != null)
    {
        acceptSocket.Close();
    }
}

// This is the stop method if you press your stop button
private void Stop()
{
    // Aborts your read thread and the socket should be closed in the read thread.  The read thread should have a ThreadState.Stopped if the while loop was gracefully ended and the socket has already been closed
    if (readThread != null && readThread.ThreadState != ThreadState.Stopped)
    {
        readThread.Abort();
    }
}

我认为这是我之前所做过的基础知识。我确定有更清洁的&#34;现在这样做的方法。这是多年前我用来实现TCP客户端/服务器套接字对象的时候。

希望这会有所帮助......