请选择 进入手机版 | 继续访问电脑版

Hello Mat

 找回密码
 立即注册
查看: 1965|回复: 0

Socket网络通信基础教程【客户端/服务端】

[复制链接]

1288

主题

1514

帖子

90

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22589
发表于 2022-5-6 22:06:46 | 显示全部楼层 |阅读模式
Socket网络通信基础教程
  1.         private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             //Socket
  4.             socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  5.             string str0 = "Hello Client!";
  6.             byte[] bytes0 = System.Text.Encoding.Default.GetBytes(str0);
  7.             socket.Send(bytes0);
  8.             socket.Blocking = false;

  9.             //Connect
  10.             string host = this.textBox1.Text.ToString();
  11.             int port = int.Parse(this.textBox2.Text.ToString());
  12.             socket.Connect(host, port);
  13.             this.clientText.Text = "客户端地址" + socket.LocalEndPoint.ToString();

  14.             int timesNoMsg = 0;
  15.             if (socket.Available == 0)
  16.             {
  17.                 Thread.Sleep(200);
  18.                 timesNoMsg++;
  19.                 if (timesNoMsg / 10 > timeOut)
  20.                 {
  21.                     throw new Exception("连接中断");
  22.                 }
  23.             }
  24.             else
  25.             {
  26.                 timesNoMsg = 0;
  27.             }

  28.             //Send
  29.             string str = "Hello Unity!";
  30.             byte[] bytes = System.Text.Encoding.Default.GetBytes(str);
  31.             socket.Send(bytes);

  32.             bool flag = true;
  33.             //Recv
  34.             while (flag == true)
  35.             {
  36.                 int count = socket.Receive(readBuff);
  37.                 str = System.Text.Encoding.UTF8.GetString(readBuff, 0, count);
  38.                 if(str.Contains("_"))
  39.                 {

  40.                 }
  41.                 this.recvText.Text = str;
  42.             }

  43.             //Close
  44.             socket.Close();
  45.         }
复制代码
服务端接受
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             connfds = new Dictionary<string, Socket>();
  4.             //方法
  5.             Thread t = new Thread(new ThreadStart(listenProc));
  6.             t.Start();
  7.         }
  8.         private void listenProc()
  9.         {
  10.             string ServerIP = this.textBox1.Text.ToString();
  11.             int ServerPort = Convert.ToInt32(this.textBox2.Text.ToString());

  12.             //新建一个套接字Socket即创建Socket
  13.             Socket listenfd = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  14.             IPAddress ipAdr = IPAddress.Any;
  15.             IPEndPoint ipEp = new IPEndPoint(ipAdr, ServerPort);//用IPAddress指定的地址和端口号初始化

  16.             listenfd.Bind(ipEp);
  17.             listenfd.Listen(10);
  18.             //Console.WriteLine("[服务器]启动成功");

  19.             while (listening_)
  20.             {
  21.                 //Accept
  22.                 Socket connfd = listenfd.Accept();
  23.                 string connGuid = Guid.NewGuid().ToString("B");
  24.                 connfds.Add(connGuid, connfd);
  25.                 //Console.WriteLine("[服务器]Accept");
  26.                 printmessage(Color.Blue, "[服务器]Accept");

  27.                 //Recv
  28.                 byte[] readBuff = new byte[1024];
  29.                 int count = connfd.Receive(readBuff);
  30.                 string str = System.Text.Encoding.UTF8.GetString(readBuff, 0, count);
  31.                 //Console.WriteLine("[服务器接收]" + str);

  32.                 //Send

  33.                 byte[] bytes = System.Text.Encoding.Default.GetBytes("\r\n回传" + str);
  34.                 connfd.Send(bytes);

  35.                 // 对str操作
  36.                 string[] strs = str.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
  37.                 bytes = System.Text.Encoding.Default.GetBytes("\r\n回传: " + strs[0]);

  38.                 // 方法
  39.                 new Thread(() => BatchProc(connGuid, strs[0])).Start();

  40.             }
  41.         }

  42.         private void BatchProc(string connGuid, string str)
  43.         {
  44.             //
  45.             // 算法部分
  46.             //

  47.             if (connfds[connGuid] != null && connfds[connGuid].Connected == true)
  48.             {
  49.                 connfds[connGuid].Send(System.Text.Encoding.Default.GetBytes("\r\n回传: " + "[图像检测完成]"));
  50.                 printmessage(Color.Blue, "[图像检测完成]" + str);
  51.             }
  52.         }
复制代码



参考:
【1】C# Socket网络通信基础教程  https://www.pianshen.com/article/4121438054/








算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-3-29 19:38 , Processed in 0.205118 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表