2013-03-02 13 views
0

我想從我無法更改的tcpclient應用程序獲取輸出。我知道它正在運行,因爲另一個應用程序可以連接到它,但它是用VB6編寫的,我沒有代碼。如何從串口上的tcpclient獲取輸出

這是我到目前爲止有:

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錯線的位置 的問題是,它是在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。

+0

如果它在'new TcpListener'失敗,我懷疑你的操作系統阻塞了這個端口。 – 2013-03-02 19:11:00

+0

我把它停在的錯誤行:TcpClient client = server.AcceptTcpClient();是它只是坐在不移動的線。 – ErocM 2013-03-02 22:14:03

回答

0

感謝您的澄清,這是有幫助的。

在高層次上,您的代碼看起來不錯。你可能會陷入代碼的'while(true)'部分,而不是'server.Start();'這與未連接的客戶端一致。

我建議附加一個調試器到連接的兩端。既然你有源代碼,我會假設你有這些符號,並且可以得到一個調試版本。把它和NetMon這樣的網絡監視器結合起來,你應該能夠找出失敗的地方。

+0

如果我理解正確,那麼正在運行一個tcpclient的程序正在運行我需要連接的程序,這是否意味着我還需要一個tcpclient來接收它的信息? – ErocM 2013-03-02 18:47:41

+0

我已更新我的問題以澄清一些事情。 – ErocM 2013-03-02 18:51:59

+0

13000來自微軟給出的例子。我有正確的端口。 – ErocM 2013-03-02 18:54:33