tcpclient未连接到本地计算机

时间:2013-03-01 16:28:36

标签: c# tcpclient

我有三个应用程序:

PPSGPS - 这是传输tcp的gps corrdinates

PPS - 当前连接到PPSGPS并且没有问题的vb6应用程序

TestProj - 我在下面提到的程序。我无法连接到PPSGPS

我正在编写一个C#应用程序,需要连接到PPSGPS并获得lat / lon ..

这是我作为测试编写的代码:

private void button1_Click(object sender, EventArgs e)
{
          if (client == null)
          {
            client = new TcpClient("localhost", 5106);
          }

          // Get the stream
          NetworkStream stream = client.GetStream();
          Byte[] data = new Byte[256];

          // String to store the response ASCII representation.
          String responseData = String.Empty;

          // Read the first batch of the TcpServer response bytes.
          int bytes = stream.Read(data, 0, data.Length);
          responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
          Console.WriteLine("Received: {0}", responseData);

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

这一切都发生在同一台机器上。

我看到端口是从netstat打开的:

C:\>netstat

Active Connections

  Proto  Local Address          Foreign Address        State
  TCP    SSSVR1:4320            localhost:5106         ESTABLISHED

当我在笔记本电脑上运行测试程序时,我得到了这个:

************** Exception Text **************
System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it 127.0.0.1:5106

有人在测试中看到我做错了吗?

====编辑====

我将client.connect添加到下面的代码中,但仍收到相同的消息:

private void button1_Click(object sender, EventArgs e)
{
  IPAddress hostIPAddress1 = IPAddress.Parse("127.0.0.1");
  var port = 5106;

  if (client == null)
  {
    client = new TcpClient(hostIPAddress1.ToString(), port);
  }

  var endp = new IPEndPoint(hostIPAddress1, port);
  client.Connect(endp);
  // Get the stream
  NetworkStream stream = client.GetStream();
  Byte[] data = new Byte[256];

  // String to store the response ASCII representation.
  String responseData = String.Empty;

  // Read the first batch of the TcpServer response bytes.
  int bytes = stream.Read(data, 0, data.Length);
  responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
  Console.WriteLine("Received: {0}", responseData);

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

====编辑#2 ====

这是PPSGPS中正在“传输”我正在尝试检索的数据的代码:

Private Sub SendToPPS(By​​Val GPS_Message As String)     如果是mbDebug_PPSGS那么       LogToFile(“Starting Sub SendToPPS”)     万一     Dim CloseSocket As Boolean

If PPSIsRunning() Then
  CloseSocket = False
  If mbDebug_PPSGS Then
    LogToFile("SendToPPS() PPS is Running.  Sending: " & GPS_Message)
  End If
  ' Don't try to send to PPS if PPS is not running

  Try
    ' Create a TcpClient.
    ' Note, for this client to work you need to have a TcpServer 
    ' connected to the same address as specified by the server, port
    ' combination.
    If SocketClient Is Nothing Then
      ' The socket is not open yet.  Open it now. 
      Dim port As Int32 = 5106
      SocketClient = New TcpClient("localhost", port)
    End If


    ' Translate the passed message into ASCII and store it as a Byte array.
    Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(GPS_Message)

    ' Get a client stream for reading and writing.
    Dim stream As NetworkStream = SocketClient.GetStream()

    ' Send the message to the connected TcpServer. 
    stream.Write(data, 0, data.Length)

  Catch err As ArgumentNullException
    LogToFile("SendToPPS() ArgumentNullException: " & err.ToString)
    CloseSocket = True
  Catch err As SocketException
    LogToFile("SendToPPS() SocketException: " & err.ToString)
    CloseSocket = True
  End Try
Else
  CloseSocket = True
  LogToFile("SendToPPS() PPS is Not Running.")
  ' PPS is not running
  ' Make sure Socket is closed
End If

If CloseSocket Then
  If Not SocketClient Is Nothing Then
    Try
      SocketClient.Close()
    Catch ex As Exception
      LogToFile("SendToPPS() Closing Socket Exception: " & ex.ToString)
    End Try

    SocketClient = Nothing
  End If

End If

If mbDebug_PPSGS Then
  LogToFile("SendToPPS() Ending Sub SendToPPS")
End If

End Sub

0 个答案:

没有答案