Form1.cs

(开头部分) 4KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace socket客户端与服务端通信
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Socket? socketfwq;//服务端服务
        Dictionary<string, Socket> keyValuePairs = new Dictionary<string, Socket>(); //把客户端的保存
        /// <summary>
        /// 服务端
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                IPAddress ip = IPAddress.Any;
                Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint point = new IPEndPoint(IPAddress.Any, 1456);
                socketWatch.Bind(point);
                socketWatch.Listen(10);//最多接收10个客户端
                textBox3.AppendText("启动服务成功");
                byte[] buffer = new byte[1024];
                Task.Run(() =>
                {
                    while (true)
                    {
                        socketfwq = socketWatch.Accept();
                        keyValuePairs.Add(socketfwq.RemoteEndPoint.ToString(), socketfwq);
                        BeginInvoke(new Action(() =>
                        {
                            comboBox1.Items.Add(socketfwq.RemoteEndPoint.ToString());
                            comboBox1.SelectedItem = socketfwq.RemoteEndPoint.ToString();
                        }));
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                int i = socketfwq.Receive(buffer);
                                string relust = Encoding.UTF8.GetString(buffer, 0, i);
                                BeginInvoke(new Action(() =>
                                {
                                    textBox3.AppendText("\r\n" + relust);
                                }));
                            }
                        });
                    }
                });
            }
            catch (Exception)
后面还有 56 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。