C#WPF实现TCP服务器客户端的通信

avatar
作者
猴君
阅读量:0

一、TCP服务器

1、三个重要对象:Tcp监听、客户端、数据网络流 

2、设置监听,捕获客户端的连接;

3、客户端连入,开线程通过网络流接收数据;

t3.IsBackground = true;//设置线程为后台线程 

服务器主体代码如下:

        private TcpListener listener;         private TcpClient client;         private NetworkStream stream;         private void button_accept_Click(object sender, RoutedEventArgs e)         {             if (textBoxIP.Text == "" || textBoxport.Text == "")                 MessageBox.Show("请输入IP和端口!");             else             {                 try                 {                     listener = new TcpListener(IPAddress.Parse(textBoxIP.Text), Int32.Parse(textBoxport.Text));                     listener.Start();//开始监听                     MessageBox.Show("服务器开始监听!");                     Thread t3 = new Thread(() => listen_connct());                     t3.IsBackground = true;//设置t3为后台进程                     t3.Start();                 }                 catch (Exception ex)                 {                     MessageBox.Show(ex.Message);                 }             }         }         private void listen_connct()         {             while (true)             {                 if (client == null)                 {                     client = listener.AcceptTcpClient();                 }                 stream = client.GetStream();//获取客户端网络流                 byte[] data1 = new byte[1024];                 if (stream.DataAvailable)                     Msg_Recv(stream, data1);                 Thread.Sleep(10);             }         }          private void Msg_Recv(NetworkStream st, byte[] data)         {             int len = st.Read(data, 0, 1024);             string messge = Encoding.UTF8.GetString(data, 0, len);             Dispatcher.Invoke(() => { textBoxRecv.Text = DateTime.Now + "\t" + messge; });         }          private void button_send_Click(object sender, RoutedEventArgs e)         {             try             {                 string message = textBoxSend.Text;                 byte[] data = new byte[1024];                 data = Encoding.UTF8.GetBytes(message);                 stream.Write(data, 0, data.Length);             }             catch (Exception ex)             {                 MessageBox.Show(ex.Message);             }         }

二、TCP客户端 

1、主要对象:客户端、字节流

2、连接指定服务器;

3、连接成功后,开线程通过字节流接受数据;

 TCP客户端主体代码如下:

        TcpClient client;         NetworkStream stream;         private void button_connect_Click(object sender, RoutedEventArgs e)         {             if (textBoxIP.Text == "" || textBoxport.Text == "")                 MessageBox.Show("请写入IP地址和端口!");             else             {                 try                 {                     client = new TcpClient();                     client.Connect(IPAddress.Parse(textBoxIP.Text), Int32.Parse(textBoxport.Text));                     stream = client.GetStream();                                       byte[] buffer = new byte[1024];                     Thread t1 = new Thread(() => Msg_Recv(stream, buffer));                     t1.Start();                     t1.IsBackground = true;                     MessageBox.Show("连接成功!");                 }                 catch (Exception ex)                 {                     MessageBox.Show(ex.Message);                 }             }         }         private void Msg_Recv(NetworkStream stream, byte[] data)         {             while (true)             {                 int len=stream.Read(data, 0, 1024);                 string message = Encoding.UTF8.GetString(data,0,len);                 Dispatcher.BeginInvoke(new Action(() => { textBoxRecv.Text = "接收:" + message; }));                               Thread.Sleep(10);             }         }         private void button_send_Click(object sender, RoutedEventArgs e)         {             string message = textBoxSend.Text;             byte[] buffer = Encoding.UTF8.GetBytes(message);             stream.Write(buffer, 0, buffer.Length);         }

 效果图

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!