socoket–使用c#的socket搭建简易通信以及粘包和分包:异步方式
1:同步方式
Accept Connect
Receive
2:异步方式
BeginAccept EndAccept //Connect连接
BegiinReceive EndReceive
3:粘包和分包问题:粘包和分包是利用socket在TCP协议下的内部优化机制
3.1粘包:多条消息粘包,导致这种情况是发送内容比较频繁,内容较小,进行粘合,节约资源
3.2分包:对发送的数据比较大的时候,进行分开发送,类似于分段发送,进行拆分的发送
4:先运行服务端,再运行客户端,
记得修改这客户端和服务端里面的ip地址,不知道的可以使用cmd里面使用ipconfig命令查看ip地址,这里采用的是IPv4地址,端口随便,不要低于4600以下,大概七八千左右差不多了,这里客户端输入“c”结束客户端与服务端的链接
5:关闭先关闭客户端,再关闭服务端
(代码如下———–如果本篇博文对你有帮助点赞加搜藏)
server.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Net.Sockets;using System.Net;namespace TCP //服务端{class Program{static void Main(string[] args){StartServerAsync();Console.ReadKey();}static void StartServerAsync(){// 创建套接字 ipv4 tcp传输流 协议类型:tcpSocket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//绑定IP 本地IP 192.168.1.100IPAddress ipAddress = IPAddress.Parse(\"192.168.1.100\");//申请端口IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 8888);//并绑定ip和端口serverSocket.Bind(ipEndPoint);//开启监听端口号 最大连接量为10serverSocket.Listen(10);//接受客户端链接--接受一个客户端// Socket clientSocket = serverSocket.Accept();//接受多个客户端--开启异步接受serverSocket.BeginAccept(AcceptCallBack,serverSocket);}//接受多个连接的回调函数static void AcceptCallBack(IAsyncResult ar){Socket serverSocket = ar.AsyncState as Socket;Socket clientSocket = serverSocket.EndAccept(ar);//向客户端发送消息string msg = \"hello你好\";byte[] data = System.Text.Encoding.UTF8.GetBytes(msg);//把字符串转换成字节流,再转成数组clientSocket.Send(data);//开始接收消息clientSocket.BeginReceive(dataBuffer, 0, 1024, SocketFlags.None, ReceiveCallBack, clientSocket);//继续调用回调函数,使得循环serverSocket.BeginAccept(AcceptCallBack, serverSocket);}//类外定义静态数组static byte[] dataBuffer = new byte[1024];//从客户端接受数据回调函数static void ReceiveCallBack(IAsyncResult ar){Socket clientSocket = null;try{clientSocket = ar.AsyncState as Socket;int count = clientSocket.EndReceive(ar);if(count==0){clientSocket.Close();return;}string msg = Encoding.UTF8.GetString(dataBuffer, 0, count);Console.WriteLine(\"从客户端接收数据:\" + msg);clientSocket.BeginReceive(dataBuffer, 0, 1024, SocketFlags.None, ReceiveCallBack, clientSocket);}catch(Exception e){Console.WriteLine(e);if (clientSocket != null){clientSocket.Close();}}finally{}}}}
client.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Net.Sockets;using System.Net;namespace TCP客户端{class Program{static void Main(string[] args){//创建套接字Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//建立链接地址和端口号clientSocket.Connect(new IPEndPoint(IPAddress.Parse(\"192.168.1.100\"), 8888));//接受从服务端来的数据byte[] data = new byte[1024];int count = clientSocket.Receive(data);string msg = System.Text.Encoding.UTF8.GetString(data, 0, count);Console.WriteLine(msg);while (true){//发送消息给服务端string s = Console.ReadLine();//从控制台输入// Console.Write(s);//输入C进行关闭客户端if (s == \"c\"){clientSocket.Close();return;}clientSocket.Send(Encoding.UTF8.GetBytes(s));}for (int i=0;i<100;i++){clientSocket.Send(Encoding.UTF8.GetBytes(i.ToString()));}Console.ReadKey();//关闭客户端clientSocket.Close();}}}
6:运行结果如下:我先运行了一个服务端,两个客户端,两个客户端发送给服务端消息