3

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

任何建议将不胜感激。

我试过这段代码:

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 是远程系统的 IP 地址,其中应用程序正在将字符串连续写入端口 7862。我需要通过 C# 应用程序从该端口读取值

4

1 回答 1

1

我之前写过一个这样的程序......我照原样复制粘贴它,不要忘记允许“端口”到防火墙和 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();
    }
}
于 2013-03-31T08:23:57.640 回答