1

我有三个应用程序:

PPSGPS - 这是传输 tcp 的 gps 坐标

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

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

我正在编写一个需要连接到 PPSGPS 并获取纬度/经度的 C# 应用程序。

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

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(ByVal GPS_Message As String) If mbDebug_PPSGS Then LogToFile("Starting Sub SendToPPS") End If 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

结束子

4

0 回答 0