c# - networkstream读写

时间:2016-01-04 22:19:42

标签: c# sockets networking

美好的一天

我正在通过一些网络编程来实现更大的应用程序,但是对于基础知识,我正在借助link

创建一个简单的网络聊天服务器客户端应用程序

会发生什么:

当从客户端接收数据时,会弹出消息框,显示与我的PC IP地址的套接字连接,但是

问题:

显示发送的消息的消息框是空的(又名“”),我不明白我做错了什么。

么?

看过这个,但我认为这不适合我的情况 network stream with buffers

客户端(发送数据)

        const int _PORT = 80;
        public Form1()
        {
            InitializeComponent();
        }

        private void sendText(string _reciever_ip, string _MESSAGE)
        {
            TcpClient _sender = new TcpClient(_reciever_ip, _PORT);
            try
            {
                Stream s = _sender.GetStream();
                StreamWriter sw = new StreamWriter(s);
                sw.WriteLine(_MESSAGE);
                s.Close();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
            }
            _sender.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sendText(inputT_IP.Text, inputT_Msg.Text);
        }

服务器(接收数据)

    const int _PORT = 80;

    static List<string> _ipaddress_list = new List<string>();

    public Form1()
    {
        InitializeComponent();
    }

    private void recieveText(string _IPADDRESS)
    {
        //open a listener for a tcp client, to the same for UDp client
        TcpListener _reciever_client = new     TcpListener(IPAddress.Parse(_IPADDRESS), _PORT); //how would you listen for a     connection from any port?
        _reciever_client.Start();
        while (true)
        {
            Socket _listener_socket = _reciever_client.AcceptSocket();
            try
            {
                MessageBox.Show("Recieving from : " +     _listener_socket.RemoteEndPoint.ToString());
                Stream _net_stream = new NetworkStream(_listener_socket);
                StreamReader sr = new StreamReader(_net_stream);
                MessageBox.Show(sr.ReadLine());
                //richTextBox1.AppendText();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
            }
            _listener_socket.Close();
        }
    }

    void GetLocalIPAddress()
    {
        var host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                _ipaddress_list.Add(ip.ToString());
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        GetLocalIPAddress();
        foreach (string item in _ipaddress_list)
        {
            (new Thread(() => recieveText(item))).Start();
        }
    }

1 个答案:

答案 0 :(得分:2)

StreamWriter缓冲写入,因此您的代码;

Stream s = _sender.GetStream();
StreamWriter sw = new StreamWriter(s);
sw.WriteLine(_MESSAGE);
s.Close();

...实际上是在内存缓冲区中写入StreamWriter并在数据从StreamWriter传递到网络之前关闭套接字。

如果您改为关闭StreamWriter;

Stream s = _sender.GetStream();
StreamWriter sw = new StreamWriter(s);
sw.WriteLine(_MESSAGE);
sw.Close();

... Close()实际上将缓冲区刷新到底层套接字,然后在数据发送后关闭底层套接字。