
Socket(套接字、插口)
TCP和UCP的区别:
1、基于连接和无连接
2、对系统资源的要求(TCP较多,UCP少)
3、UDP程序结构简单
4、流模式和数据报模式
5、TCP保证数据正确性和数据先后顺序,UDP可能丢包,且不保证数据到达先后顺序
// 进行前先寻得本机IPv4地址
win + R ——>cmd——>输入ipconfig


// TCP类
- class TCP
- {
- // 服务器端套接字(指定的寻址方案的一个实例.IPv4,套接字类型.字节流,支持的协议.TCP)
- Socket tcpServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- // 客户端套接字
- Socket tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
-
- //Internet 协议 (IP) 地址(IP 地址的字节数组值)
- IPAddress ipAddress = new IPAddress(new byte[] { 192, 168, 77, 1 });
-
-
- public void TcpServer()
- {
- //IP + Port(端口) 地址和端口号,端口号由自己指定,用于链接
- IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 0001);
- tcpServer.Bind(ipEndPoint); /*将相关联 tcpServer 与本地终结点*/
-
- // 侦听tcpServer(挂起的连接队列的最大长度)
- tcpServer.Listen(20);
- Console.WriteLine("正在连接客户端……");
-
- // 新创建的连接
- Socket client = tcpServer.Accept();
- Console.WriteLine("一个客户端链接过来了");
-
- // 接收信息
- byte[] data = new byte[1024];
- // 将tcpServer数据的按字节存入接收缓冲区(接收到的数据的存储位置)
- int length = client.Receive(data);
-
- //字符编码.获取 UTF-8 格式的编码.将指定字节数组中的一个字节序列解码为一个字符串(包含要解码的字节序列的字节数组,第一个要解码的字节的索引,要解码的字节数)
- string message = Encoding.UTF8.GetString(data, 0, length);
-
- Console.WriteLine("接收到客户端的消息:" + message);
-
- // 发送消息,将指定字符串中的所有字符编码为一个字节序列
- client.Send(Encoding.UTF8.GetBytes("欢迎你的到来"));
-
- // 关闭Socket连接和释放所有关联资源
- client.Close();
- tcpServer.Close();
- }
- public void TcpClient()
- {
- //IP + Port(端口),与客户端一致,保证接收
- IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 0001);
- //建立与远程主机的连接(远程设备)
- tcpClient.Connect(ipEndPoint);
-
- Console.WriteLine("链接上了服务器端!");
-
- string message = "我上线了";
- tcpClient.Send(Encoding.UTF8.GetBytes(message));
- byte[] data = new byte[1024];
-
- int length = tcpClient.Receive(data);
- Console.WriteLine("接收到服务器的信号:" + Encoding.UTF8.GetString(data));
-
- tcpClient.Close();
- }
- }
// UDP类
- class UDP
- {
- Socket udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- Socket udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
-
- IPAddress ipAddress = new IPAddress(new byte[] { 192, 168, 77, 1 });
-
- public void UdpSer()
- {
- IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 1111);
-
- // udpServer连接节点ipEndPint
- udpServer.Bind(ipEndPoint);
-
- // 提供了一个指示服务器必须侦听的所有网络接口上的客户端活动的 IP 地址
- IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);
-
-
- // 标识网络地址,abstrct类
- EndPoint ep = ipep;
- byte[] data = new byte[1024];
- // 接收到数据缓冲区,将存储终结点(数据的存储位置,引用传递(远程服务器))
- int length = udpServer.ReceiveFrom(data, ref ep);
-
- Console.WriteLine("接收到来着UDP客户端的数据:" + Encoding.UTF8.GetString(data, 0, length));
-
- udpServer.Close();
- }
- public void UdpCnt()
- {
- byte[] data = Encoding.UTF8.GetBytes("你好,udp客户端上线了");
-
- IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 1111);
-
- udpClient.SendTo(data, ipEndPoint);
-
- udpClient.Close();
- }
- }
// 主程
- static void Main(string[] args)
- {
- // TCP
- TCP server = new TCP();
- Thread sr = new Thread(server.TcpServer);
- Thread ct = new Thread(server.TcpClient);
-
- sr.Start();
- ct.Start();
-
- Thread.Sleep(1000);
-
- sr.Abort();
- ct.Abort();
-
- // UDP
- UDP udp = new UDP();
- Thread sUdp = new Thread(udp.UdpSer);
- Thread cUdp = new Thread(udp.UdpCnt);
-
- sUdp.Start();
- cUdp.Start();
-
- Thread.Sleep(1000);
-
- cUdp.Abort();
- sUdp.Abort();
- }
// 结果
- 正在连接客户端……
- 链接上了服务器端!
- 一个客户端链接过来了
- 接收到客户端的消息:我上线了
- 接收到服务器的信号:欢迎你的到来
-
-
-
-
- 接收到来着UDP客户端的数据:你好,udp客户端上线了
- 请按任意键继续. . .