1

3 つのアプリケーションがあります。

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