• 大小: 36KB
    文件类型: .7z
    金币: 1
    下载: 0 次
    发布日期: 2021-06-07
  • 语言: C#
  • 标签: wpf  socket  C#  

资源简介

两个wpf程序,一个是服务器,一个是客户端。两个进行通信。

资源截图

代码片段和文件信息

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
 
namespace ClientSocket
{
    public partial class Form1 : Form
    {
        //定义Socket对象
        Socket clientSocket;
        //创建接收消息的线程
        Thread threadReceive;
        //接收服务端发送的数据
        string str;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender EventArgs e)
        {
 
        }
 
        private void button_connect_Click(object sender EventArgs e)
        {
            IPAddress ip = IPAddress.Parse(this.text_ip.Text.Trim());
            clientSocket = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
            try {
                //连接服务端
                clientSocket.Connect(ip Convert.ToInt32(this.text_port.Text.Trim()));
                //开启线程不停的接收服务端发送的数据
                threadReceive = new Thread(new ThreadStart(Receive));
                threadReceive.IsBackground = true;
                threadReceive.Start();
                //设置连接按钮在连接服务端后状态为不可点
                this.button_connect.Enabled = false;
            } catch {
                MessageBox.Show(“连接服务端失败,请确认ip和端口是否填写正确“ “连接服务端失败“);
            }
        }
 
        //接收服务端消息的线程方法
        private void Receive()
        {
            try {
                while (true) {
                    byte[] buff = new byte[20000];
                    int r = clientSocket.Receive(buff);
                    str = Encoding.Default.GetString(buff0r);
                    this.Invoke(new Action(() => { this.text_log1.Text += (str + “\r\n“); }));
                }
            } catch {
                MessageBox.Show(“获取服务端参数失败““获取服务端参数失败“);
            }
        }
 
        private void button_close_Click(object sender EventArgs e)
        {
            //clientSocket关闭
            clientSocket.Close();
            //threadReceive关闭
            threadReceive.Abort();
        }
 
        private void button_send_Click(object sender EventArgs e)
        {
            try {
                string strMsg = this.text_log2.Text.Trim();
                byte[] buffer = new byte[20000];
                buffer = Encoding.Default.GetBytes(strMsg);
                clientSocket.Send(buffer);
            } catch {
                MessageBox.Show(“发送数据失败“ “发送数据失败“);
            }
        }
    }
}

评论

共有 条评论