如何从串行端口上的tcpclient获取输出

本文关键字:tcpclient 获取 输出 串行端口 | 更新日期: 2023-09-27 18:02:07

我试图从无法更改的tcpclient应用程序获取输出。我知道它正在运行,因为另一个应用程序可以连接到它,但它是用VB6编写的,我没有代码。

到目前为止我写的是:

  public static void Main()
  {    
    try
    {
      // Set the TcpListener on port 13000.
      Int32 port = 5107;
      IPAddress localAddr = IPAddress.Parse("127.0.0.1");
      // TcpListener server = new TcpListener(port);
      TcpListener server = new TcpListener(localAddr, port);
      // Start listening for client requests.
      server.Start();
      // Buffer for reading data
      Byte[] bytes = new Byte[256];
      String data = null;
      // Enter the listening loop.
      while(true) 
      {
        Log("Waiting for a connection... ");
        // Perform a blocking call to accept requests.
        // You could also user server.AcceptSocket() here.
        TcpClient client = server.AcceptTcpClient();            
        Log("Connected!");
        data = null;
        // Get a stream object for reading and writing
        NetworkStream stream = client.GetStream();
        int i;
        // Loop to receive all the data sent by the client.
        while((i = stream.Read(bytes, 0, bytes.Length))!=0) 
        {   
          // Translate data bytes to a ASCII string.
          data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
          Log(String.Format("Received: {0}", data));
          // Process the data sent by the client.
          data = data.ToUpper();
          byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
          // Send back a response.
          stream.Write(msg, 0, msg.Length);
          Log(String.Format("Sent: {0}", data));            
        }
        // Shutdown and end connection
        client.Close();
      }
    }
    catch(SocketException e)
    {
      Log("SocketException: " + e.Message);
    }
  }

UPDATE出错问题是,它停在TcpClient client = server.AcceptTcpClient();,从那里什么也不做。

其他应用程序正在运行,但没有连接(?)我猜。我以前没有这样使用过tcp,所以我不确定我在做什么。

我确信端口也正确。

如果客户端没有连接,我如何从客户端获取输出?

======= EDIT =======

这是我试图从应用程序中获取信息的代码:

 Private Sub SendToPPS(ByVal GPS_Message As String)
    If mbDebug_PPSGS Then
      LogToFile("Starting Sub SendToPPS")
    End If
    Dim CloseSocket As Boolean
    LogToFile("Starting SendToPPS")
    If PPSIsRunning() Then
      LogToFile("PPSIsRunning")
      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
        If SocketClient2 Is Nothing Then
          ' The socket is not open yet.  Open it now. 
          Dim port As Int32 = 5107
          SocketClient2 = 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()
        Dim stream2 As NetworkStream = SocketClient2.GetStream()
        ' Send the message to the connected TcpServer. 
        stream.Write(data, 0, data.Length)
        stream2.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

======= EDIT #2 =======

这两个应用程序都在同一台计算机上运行,所以我使用127.0.0.1。

如何从串行端口上的tcpclient获取输出

谢谢你的说明,这很有帮助。

从高层次上看,你的代码看起来不错。你可能被卡在代码的'while(true)'部分,而不是'server.Start();'这将与客户端未连接一致。

我建议在连接的两边附加一个调试器。既然您有源代码,我就假定您有这些符号,并且可以获得调试构建。将它与网络监视器(如NetMon)结合使用,您应该能够找出故障所在。