0

我正在尝试从无法更改的 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 HAD WRONG LINE HERE 问题是,它停在TcpClient client = server.AcceptTcpClient();那里,从那里什么也不做。

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

我很肯定端口也是正确的。

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

======= 编辑 =======

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

 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

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

这两个应用程序都在同一台计算机上运行,​​这就是我使用 127.0.0.1 的原因。

4

1 回答 1

0

感谢您的澄清,这很有帮助。

在高层次上,您的代码看起来不错。您可能陷入了代码的“while(true)”部分,而不是“server.Start();” 这与客户端未连接是一致的。

我建议将调试器附加到连接的两侧。既然你有源代码,我会假设你有符号并且可以获得调试版本。将其与NetMon之类的网络监视器结合使用,您应该能够找出哪里出了问题。

于 2013-03-02T18:46:39.203 回答