socket/TCP/UDP基础及Unity聊天室的实现

我们平时说的最多的socket是什么呢,实际上socket是对TCP/IP协议的封装,Socket本身并不是协议,而是一个调用接口(API)。

通过Socket,我们才能使用TCP/IP协议。实际上,Socket跟TCP/IP协议没有必然的联系。Socket编程接口在设计的时候,就希望也能适应其他的网络协议。所以说,Socket的出现只是使得程序员更方便地使用TCP/IP协议栈而已,是对TCP/IP协议的抽象,从而形成了我们知道的一些最基本的函数接口,比如create、listen、connect、accept、send、read和write等等。
网络有一段关于socket和TCP/IP协议关系的说法比较容易理解:

“TCP/IP只是一个协议栈,就像操作系统的运行机制一样,必须要具体实现,同时还要提供对外的操作接口。
这个就像操作系统会提供标准的编程接口,比如win32编程接口一样,
TCP/IP也要提供可供程序员做网络开发所用的接口,这就是Socket编程接口。”

1、TCP协议实现原理

TCP数据包主要包括:
1、SYN包:请求建立连接的数据包
2、ACK包:回应数据包,表示接收到了对方的某个数据包
3、PSH包:正常数据包
4、FIN包:通讯结束包
5、RST包:重置连接
6、URG包:紧急指针

一次完成的TCP通讯包括:建立连接、数据传输、关闭连接
建立连接(三次握手):
1、客户端通过向服务器端发送一个SYN来建立一个主动打开,作为三路握手的一部分。
2、服务器端应当为一个合法的SYN回送一个SYN/ACK。
3、最后,客户端再发送一个ACK。这样就完成了三路握手,并进入了连接建立状态。
数据传输:
1、发送数据端传输PSH数据包
2、接收数据端回复ACK数据包
关闭连接(四次分手):
1、一端主动关闭连接。向另一端发送FIN包。
2、接收到FIN包的另一端回应一个ACK数据包。
3、另一端发送一个FIN包。
4、接收到FIN包的原发送方发送ACK对它进行确认。

下面为使用TCP协议实现一个简单服务器端:
[csharp]  view plain  copy
 print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Sockets;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8.   
  9.     class Program {  
  10.         static void Main(string[] args) {  
  11.             // 1,创建socket  
  12.             Socket tcpServer = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);  
  13.             //2,绑定ip跟端口号 222.20.30.68  
  14.             IPAddress ipaddress = new IPAddress(new byte[]{222,20,30,68});  
  15.             EndPoint point = new IPEndPoint(ipaddress,7788);//ipendpoint是对ip+端口做了一层封装的类  
  16.             tcpServer.Bind(point);//向操作系统申请一个可用的ip跟端口号 用来做通信  
  17.             //3,开始监听 (等待客户端连接)  
  18.             tcpServer.Listen(100);//参数是最大连接数  
  19.             Console.WriteLine("开始监听");  
  20.   
  21.            Socket clientSocket = tcpServer.Accept();//暂停当前线程,直到有一个客户端连接过来,之后进行下面的代码  
  22.             Console.WriteLine("一个客户端连接过来了");  
  23.             //使用返回的socket跟客户端做通信  
  24.             string message = "hello 欢迎你";  
  25.             byte[] data = Encoding.UTF8.GetBytes(message);//对字符串做编码,得到一个字符串的字节数组  
  26.             clientSocket.Send(data);  
  27.             Console.WriteLine("向客户端发送了一跳数据");  
  28.   
  29.             byte[] data2 = new byte[1024];//创建一个字节数组用来当做容器,去承接客户端发送过来的数据  
  30.             int length = clientSocket.Receive(data2);  
  31.             string message2 = Encoding.UTF8.GetString(data2, 0, length);//把字节数据转化成 一个字符串  
  32.             Console.WriteLine("接收到了一个从客户端发送过来的消息:"+message2);  
  33.   
  34.             Console.ReadKey();  
  35.         }  
  36.     }  

TcpListener的使用:
[csharp]  view plain  copy
 print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Sockets;  
  6. using System.Runtime.InteropServices;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9.   
  10.     class Program {  
  11.         static void Main(string[] args) {  
  12.             //1,TcpListener对socket进行了一层封装,这个类里面自己会去创建socket对象  
  13.             TcpListener listener = new TcpListener(IPAddress.Parse("222.20.30.68"), 7788);  
  14.   
  15.             //2,开始进行监听  
  16.             listener.Start();  
  17.   
  18.             //3,等待客户端连接过来  
  19.             TcpClient client = listener.AcceptTcpClient();  
  20.   
  21.             //4,取得客户端发送过来的数据  
  22.             NetworkStream stream = client.GetStream();//得到了一个网络流  从这个网络流可以取得客户端发送过来的数据  
  23.   
  24.             byte[] data = new byte[1024];//创建一个数据的容器,用来承接数据  
  25.   
  26.             while (true)  
  27.             {  
  28.                 //0 表示从数组的哪个索引开始存放数据  
  29.                 //1024表示最大读取的字节数  
  30.                 int length = stream.Read(data, 0, 1024);//读取数据  
  31.                 string message = Encoding.UTF8.GetString(data, 0, length);  
  32.                 Console.WriteLine("收到了消息:" + message);  
  33.             }  
  34.   
  35.   
  36.             stream.Close();  
  37.             client.Close();  
  38.             listener.Stop();//停止监听  
  39.             Console.ReadKey();  
  40.         }  
  41.     }  

2、UDP协议实现原理

UDP协议在IP协议上增加了复用、分用和差错检测功能。UDP的特点:
1、是无连接的。相比于TCP协议,UDP协议在传送数据前不需要建立连接,当然也就没有释放连接。
2、是尽最大努力交付的。也就是说UDP协议无法保证数据能够准确的交付到目的主机。也不需要对接收到的UDP报文进行确认。
3、是面向报文的。也就是说UDP协议将应用层传输下来的数据封装在一个UDP包中,不进行拆分或合并。因此,运输层在收到对方的UDP包后,会去掉首部后,将数据原封不动的交给应用进程。
4、没有拥塞控制。因此UDP协议的发送速率不送网络的拥塞度影响。
5、UDP支持一对一、一对多、多对一和多对多的交互通信。
6、UDP的头部占用较小,只占用8个字节。

下面为使用UDP协议实现一个简单服务器端:
[csharp]  view plain  copy
 print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Sockets;  
  6. using System.Text;  
  7. using System.Threading;  
  8. using System.Threading.Tasks;  
  9. class Program  
  10.     {  
  11.         private static Socket udpServer;  
  12.         static void Main(string[] args) {  
  13.             //1,创建socket  
  14.              udpServer = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);  
  15.             //2,绑定ip跟端口号  
  16.             udpServer.Bind( new IPEndPoint( IPAddress.Parse("192.168.0.112"),7788 ) );  
  17.   
  18.             //3,接收数据  
  19.             new Thread(ReceiveMessage){ IsBackground = true}.Start();  
  20.   
  21.             //udpServer.Close();  
  22.             Console.ReadKey();  
  23.         }  
  24.   
  25.         static void ReceiveMessage()  
  26.         {  
  27.             while (true)  
  28.             {  
  29.                 EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);  
  30.                 byte[] data = new byte[1024];  
  31.                 int length = udpServer.ReceiveFrom(data, ref remoteEndPoint);//这个方法会把数据的来源(ip:port)放到第二个参数上  
  32.                 string message = Encoding.UTF8.GetString(data, 0, length);  
  33.                 Console.WriteLine("从ip:" + (remoteEndPoint as IPEndPoint).Address.ToString() + ":" + (remoteEndPoint as IPEndPoint).Port + "收到了数据:" + message);  
  34.             }  
  35.   
  36.         }  
  37.     }  

udpClient的使用:
[csharp]  view plain  copy
 print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Sockets;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8.   
  9.     class Program {  
  10.         static void Main(string[] args) {  
  11.             //创建udpclient 绑定ip跟端口号  
  12.             UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("192.168.0.112"),7788));  
  13.   
  14.             while (true)  
  15.             {  
  16.                 //接收数据  
  17.                 IPEndPoint point = new IPEndPoint(IPAddress.Any, 0);  
  18.                 byte[] data = udpClient.Receive(ref point);//通过point确定数据来自哪个ip的哪个端口号 返回值是一个字节数组,就是我们的数据  
  19.                 string message = Encoding.UTF8.GetString(data);  
  20.                 Console.WriteLine("收到了消息:" + message);  
  21.             }  
  22.   
  23.   
  24.             udpClient.Close();  
  25.             Console.ReadKey();  
  26.         }  
  27.     }  

3、Unity实现聊天室功能

客户端的实现代码:
[csharp]  view plain  copy
 print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Net;  
  4. using System.Net.Sockets;  
  5. using System.Text;  
  6. using System.Threading;  
  7.   
  8. public class ChatManager : MonoBehaviour  
  9. {  
  10.     public string ipaddress = "222.20.30.68";  
  11.     public int port = 7788;  
  12.     public UIInput textInput;  
  13.     public UILabel chatLabel;  
  14.   
  15.     private Socket clientSocket;  
  16.     private Thread t;  
  17.     private byte[] data = new byte[1024];//数据容器  
  18.     private string message = "";//消息容器  
  19.     // Use this for initialization  
  20.     void Start () {  
  21.         ConnectToServer();  
  22.     }  
  23.       
  24.     // Update is called once per frame  
  25.     void Update () {  
  26.         if (message != null && message != "")  
  27.         {  
  28.             chatLabel.text += "\n" + message;  
  29.             message = "";//清空消息  
  30.         }  
  31.     }  
  32.   
  33.     void ConnectToServer()  
  34.     {  
  35.           
  36.         clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);  
  37.         //跟服务器端建立连接  
  38.         clientSocket.Connect(new IPEndPoint(IPAddress.Parse("222.20.30.68"),7788));  
  39.         //创建一个新的线程 用来接收消息  
  40.         t = new Thread(ReceiveMessage);  
  41.         t.Start();  
  42.     }  
  43.     /// <summary>  
  44.     /// 这个线程方法 用来循环接收消息  
  45.     /// </summary>  
  46.     void ReceiveMessage()  
  47.     {  
  48.         while (true)  
  49.         {  
  50.             if (clientSocket.Connected == false)  
  51.                 break;  
  52.   
  53.             int length = clientSocket.Receive(data);  
  54.             message = Encoding.UTF8.GetString(data, 0, length);  
  55.             //chatLabel.text += "\n" + message;  
  56.         }  
  57.     }  
  58.   
  59.     void SendMessage(string message)  
  60.     {  
  61.         byte[] data = Encoding.UTF8.GetBytes(message);  
  62.         clientSocket.Send(data);  
  63.     }  
  64.   
  65.     public void OnSendButtonClick()  
  66.     {  
  67.         string value = textInput.value;  
  68.         SendMessage(value);  
  69.         textInput.value = "";  
  70.     }  
  71.   
  72.     void OnDestroy()  
  73.     {  
  74.         clientSocket.Shutdown(SocketShutdown.Both);  
  75.           
  76.         clientSocket.Close();//关闭连接  
  77.     }  
  78. }  

服务器端的实现代码:
[csharp]  view plain  copy
 print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net.Sockets;  
  5. using System.Text;  
  6. using System.Threading;  
  7. using System.Threading.Tasks;  
  8.   
  9.   
  10. namespace _022_聊天室_socket_tcp服务器端 {  
  11.     /// <summary>  
  12.     /// 用来跟客户端做通信   
  13.     /// </summary>  
  14.     class Client  
  15.     {  
  16.         public string name=null;  
  17.         private Socket clientSocket;  
  18.         private Thread t;  
  19.         private byte[] data = new byte[1024];//这个是一个数据容器  
  20.   
  21.         public Client(Socket s,string i)  
  22.         {  
  23.             clientSocket = s;  
  24.             name = i;  
  25.             //启动一个线程 处理客户端的数据接收  
  26.             t = new Thread(ReceiveMessage);  
  27.             t.Start();  
  28.         }  
  29.   
  30.         private void ReceiveMessage()  
  31.         {  
  32.             //一直接收客户端的数据  
  33.             while (true)  
  34.             {  
  35.                 //在接收数据之前  判断一下socket连接是否断开  
  36.                 if (clientSocket.Poll(10, SelectMode.SelectRead))  
  37.                 {  
  38.                     clientSocket.Close();  
  39.                     break;//跳出循环 终止线程的执行  
  40.                 }  
  41.                   
  42.                 int length = clientSocket.Receive(data);  
  43.                 string message = Encoding.UTF8.GetString(data, 0, length);  
  44.                 //接收到数据的时候 要把这个数据 分发到客户端  
  45.                 //广播这个消息  
  46.                 Program.BroadcastMessage(message,name);  
  47.                 Console.WriteLine("收到 "+name +" 的消息:" + message);  
  48.             }  
  49.         }  
  50.   
  51.         public void SendMessage(string message)  
  52.         {  
  53.             byte[] data = Encoding.UTF8.GetBytes(message);  
  54.             clientSocket.Send(data);  
  55.         }  
  56.   
  57.         public bool Connected  
  58.         {  
  59.             get { return clientSocket.Connected; }  
  60.         }  
  61.     }  
  62. }  

[csharp]  view plain  copy
 print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Sockets;  
  6. using System.Net.WebSockets;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9.   
  10. namespace _022_聊天室_socket_tcp服务器端 {  
  11.     class Program {  
  12.         static  List<Client> clientList = new List<Client>();  
  13.           
  14.         /// <summary>  
  15.         /// 广播消息  
  16.         /// </summary>  
  17.         /// <param name="message"></param>  
  18.         public static void BroadcastMessage(string message,string name)  
  19.         {  
  20.             var notConnectedList = new List<Client>();  
  21.             foreach (var client in clientList)  
  22.             {  
  23.                 if (client.Connected)  
  24.                     client.SendMessage(name+" : "+message);  
  25.                 else  
  26.                 {  
  27.                     notConnectedList.Add(client);  
  28.                 }  
  29.             }  
  30.             foreach (var temp in notConnectedList)  
  31.             {  
  32.                 clientList.Remove(temp);  
  33.             }  
  34.         }  
  35.         static void Main(string[] args) {  
  36.             int i = 0;  
  37.             string name = null;  
  38.             Socket tcpServer = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);//create tcpserver  
  39.             tcpServer.Bind(new IPEndPoint(IPAddress.Parse("222.20.30.68"),7788));//set ip and port  
  40.   
  41.             tcpServer.Listen(100);//listen port  
  42.             Console.WriteLine("server running...");  
  43.   
  44.             while (true)  
  45.             {  
  46.                 i = i + 1;  
  47.                 if (i == 1) { name = "Tom"; }   
  48.                 else if (i == 2) { name = "Alice";}  
  49.                 else { name = "Other"; }  
  50.                 Socket clientSocket = tcpServer.Accept();//accept client request  
  51.                 Console.WriteLine("用户 "+name+" 已连接 !");  
  52.                 Client client = new Client(clientSocket,name);//把与每个客户端通信的逻辑(收发消息)放到client类里面进行处理                  
  53.                 clientList.Add(client);  
  54.             }  
  55.               
  56.         }  
  57.     }  
  58. }  

打开两个客户端。记得代码中的IP地址要改成自己的PC机的IP地址。
实现效果:





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值