2013-03-31 45 views
1

我有一个应用程序在远程服务器上运行,它将数据(字符串)写入其本地端口,我想通过另一个在其他系统上运行的另一个C#应用程序读取此系统端口,当我连接到此端口的远程机器出现错误,目标机器主动拒绝连接。C#中的TCP端口读取#

任何建议将受到赞赏。

我曾尝试这样的代码:

Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
var ipaddress = IPAddress.Parse("192.168.1.12"); 
IPAddress add = new IPAddress(ipaddress.GetAddressBytes()); 
EndPoint ep = new IPEndPoint(add, 7862); 
sock.Connect(ep); 

if (sock.Connected) 
{ 
    byte[] bytes = new byte[256]; 
    int i = sock.Receive(bytes); 
    Console.WriteLine(Encoding.UTF8.GetString(bytes)); 
} 

这里192.168.1.12是远程系统,其中应用程序被写入字符串持续到端口7862.我需要从端口读取值的IP地址通过

+0

您是否尝试过Telnet?查看当您远程登录到远程端口时会发生什么。你应该在提出问题之前尝试过:) – Jeremy

+0

我不认为这是可能的。这看起来不安全,与目前的远程查询*方法相比,您需要配置许多权限或将arcitecture更改为*公开API *。 – oleksii

回答

1

我写了一个程序一样,前一阵子C#应用程序...我复制粘贴,因为它是,不要忘了让“移植”到了防火墙和NAT,使数据包实际上是通过

得到
class Transmitter 
{ 
    public Boolean Transmit(String ip ,String port, String data){ 
     TcpClient client = new TcpClient(); 
     int _port = 0; 
     int.TryParse(port, out _port); 
     IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(ip), _port); 
     client.Connect(serverEndPoint); 
     NetworkStream clientStream = client.GetStream(); 
     ASCIIEncoding encoder = new ASCIIEncoding(); 
     byte[] buffer = encoder.GetBytes(data); 
     clientStream.Write(buffer, 0, buffer.Length); 
     clientStream.Flush(); 
     return true; 
    } 
} 

class Listener 
{ 

    private TcpListener tcpListener; 
    private Thread listenThread; 
    // Set the TcpListener on port 13000. 
    Int32 port = 8081; 
    IPAddress localAddr = IPAddress.Parse("192.168.1.3"); 
    Byte[] bytes = new Byte[256]; 
    MainWindow mainwind = null; 
    public void Server(MainWindow wind) 
    { 
     mainwind = wind; 
     this.tcpListener = new TcpListener(IPAddress.Any, port); 
     this.listenThread = new Thread(new ThreadStart(ListenForClients)); 
     this.listenThread.Start(); 

    } 
    private void ListenForClients() 
    { 

     this.tcpListener.Start(); 

     while (true) 
     { 
      //blocks until a client has connected to the server 
      TcpClient client = this.tcpListener.AcceptTcpClient(); 

      //create a thread to handle communication 
      //with connected client 
      Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm)); 
      clientThread.Start(client); 
     } 
    } 
    private void HandleClientComm(object client) 
    { 
     TcpClient tcpClient = (TcpClient)client; 
     NetworkStream clientStream = tcpClient.GetStream(); 

     byte[] message = new byte[4096]; 
     int bytesRead; 

     while (true) 
     { 
      bytesRead = 0; 

      try 
      { 
       //blocks until a client sends a message 
       bytesRead = clientStream.Read(message, 0, 4096); 
      } 
      catch 
      { 
       //a socket error has occured 
       // System.Windows.MessageBox.Show("socket"); 
       break; 
      } 

      if (bytesRead == 0) 
      { 
       //the client has disconnected from the server 
       // System.Windows.MessageBox.Show("disc"); 
       break; 
      } 

      //message has successfully been received 
      ASCIIEncoding encoder = new ASCIIEncoding(); 
      mainwind.setText(encoder.GetString(message, 0, bytesRead)); 
      //System.Windows.MessageBox.Show(encoder.GetString(message, 0, bytesRead)); 
      // System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead)); 
     } 

     tcpClient.Close(); 
    } 
}