C#TCP客户端在localhost中接收但不在网络上接收

时间:2015-06-18 15:33:11

标签: c# sockets tcp

我的c#客户端应用程序与服务器进行通信,在localhost上,一切似乎都有效。 问题是当我将应用程序放在另一台PC上时:我看到TCP数据包到达wireshark但我的套接字什么也没做。 在此图像中,9字节缓冲区是服务器TX。

Wireshark Screenshot

PS。我的防病毒和防火墙已被禁用。

可能是什么问题?

这里的代码(简称只有RX方法)

public partial class CSocket
{

  private const int RX_BUFF_SIZE = 1024;

  private Socket _clientSkt;
  private byte[] _rx_buffer = new byte[RX_BUFF_SIZE];

  private MemoryStream _rx_stream = new MemoryStream();

  // ManualResetEvent instances signal completion.
  private static ManualResetEvent connectDone = new ManualResetEvent(false);


  public bool Connect(string IP, int Port)
  {
    if (_clientSkt != null)
      if (_clientSkt.Connected) return true;

    IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(IP), Port);

    // Create a TCP/IP socket.
    _clientSkt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    connectDone.Reset();
    // Connect to the remote endpoint.
    _clientSkt.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), _clientSkt);
    connectDone.WaitOne();

    if (_clientSkt.Connected)
    {
      // Begin receiving the data from the remote device.
      _clientSkt.BeginReceive(_rx_buffer, 0, RX_BUFF_SIZE, 0, new AsyncCallback(ReceiveCallback), _clientSkt);
      return true;
    }
    else
    {
      return false;
    }
  }

  protected void ReceiveCallback(IAsyncResult ar)
  {
    try
    {
      if (_clientSkt == null) return;

      // Read data from the client socket.
      int bytesRead = _clientSkt.EndReceive(ar);

      if (bytesRead > 0)
      {
        // There might be more data, so store the data received so far.
        Monitor.Enter(_clientSkt);
        _rx_stream.Write(_rx_buffer, 0, bytesRead);
        Monitor.Exit(_clientSkt);

        DecodeData(_rx_stream.ToArray());
      }

      _clientSkt.BeginReceive(_rx_buffer, 0, RX_BUFF_SIZE, 0, new AsyncCallback(ReceiveCallback), _clientSkt);

    }
    catch (System.Net.Sockets.SocketException es)
    {
    }
    catch (Exception e)
    {
    }
  }

}

0 个答案:

没有答案