C#中的TCP端口读取

时间:2013-03-31 08:04:37

标签: c# sockets tcp

我有一个在远程服务器上运行的应用程序,它将数据(字符串)写入其本地端口,我想通过在其他系统上运行的另一个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#应用程序从该端口读取值

1 个答案:

答案 0 :(得分: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();
    }
}