如何使用NetworkStream发送消息(C#)

时间:2019-06-19 12:58:07

标签: c# networkstream

我想使用networkstream读写来发送消息 但是我做不到(即使发送了消息我也不知道) 我想知道问题出在哪里,如何解决服务器代码。 (我只能修复服务器代码。)

这是服务器(侦听器和发送者)代码

    IPAddress ipadd = IPAddress.Parse("127.0.0.1");
    Int32 port = 8192;


    private void Button5_Click(object sender, EventArgs e)
    {
        TcpListener listener = new TcpListener(ipadd, port);
        listener.Start();
    }
    TcpClient tcpClient = null;
    public NetworkStream networkStream = null;
    string ServerIP = "127.0.0.1";
    string ServerPort = "8192";
    private void Button6_Click(object sender, EventArgs e)
    {
        tcpClient = new TcpClient();
        tcpClient.Connect(ServerIP, Convert.ToInt32(ServerPort));

        networkStream = tcpClient.GetStream();

        byte[] sendBytes = Encoding.ASCII.GetBytes(textBox3.Text);
        networkStream.Write(sendBytes, 0, sendBytes.Length);
    }

这是客户(接收方)代码

     TcpClient tcpClient = null;
     public NetworkStream networkStream = null;
     string ServerIP = "127.0.0.1";
     string ServerPort = "8192";

     tcpClient = new TcpClient();
     tcpClient.Connect(ServerIP, Convert.ToInt32(ServerPort));

     networkStream = tcpClient.GetStream();

     FileSizeBytes = new byte[1024];
     ByteSize = networkStream.Read(FileSizeBytes, 0, FileSizeBytes.Length);
     ae = new ASCIIEncoding();
     inputSource = ae.GetString(FileSizeBytes, 0, ByteSize);

0 个答案:

没有答案