项目 聊天室

1.先创建客户端

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 using UnityEngine.UI;
 5 using System.Net;
 6 using System.Net.Sockets;
 7 using System.Threading;
 8 using System.Text;
 9 
10 public class WeChatSocket : MonoBehaviour
11 {
12     Socket socket;
13     public InputField InputValue;
14     public Text text;
15     byte[] data = new byte[1024];//接收的容器
16     string message = "";//转换后的消息
17                         // Use this for initialization
18     void Start()
19     {
20         ConnetToSever();
21 
22     }
23     /// <summary>
24     /// 创建Socket并连接服务器IP
25     /// </summary>
26     void ConnetToSever()
27     {
28         socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
29         socket.Connect(new IPEndPoint(IPAddress.Parse("10.0.208.54"), 4567));
30         Thread t = new Thread(ReceveMessage);
31         t.Start();
32     }
33     public void ReceveMessage()
34     {
35 
36         while (true)
37         {
38             int length = socket.Receive(data);
39             message = Encoding.UTF8.GetString(data, 0, length);
40         }
41 
42     }
43     public void SendMessage()
44     {
45         string mess = InputValue.text;
46         InputValue.text = "";
47         byte[] datas = Encoding.UTF8.GetBytes(mess);
48         socket.Send(datas);
49     }
50     // Update is called once per frame
51     void Update()
52     {
53         if (!string.IsNullOrEmpty(message))
54         {
55             text.text += message + "\n";
56             message = "";
57         }
58     }
59 }
客户端


接受消息的时候用容器接受 字节容器byte[];

抽象类 与借口的区别?

1.1链接

2.发送(用线程)

2.服务器端  在vs里面写
主函数里的代码:

 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 namespace 聊天室服务器
10 {
11     class Program
12     {
13         static List<Client> listClient = new List<Client>();//用于存储接受到的消息
14         static void Main(string[] args)
15         {
16             //1.创建Socket
17             Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
18             //2.绑定IP和端口
19             socket.Bind(new IPEndPoint(IPAddress.Parse("10.0.208.54"),4567));
20             //3.监听
21             socket.Listen(100);
22             //4.等待链接和接收
23             while (true)
24             {
25                 Socket client = socket.Accept();//如果接收到就是连接成功,如果没有接收下面程序不会运行
26                 Client clientSocket = new Client(client);
27                 listClient.Add(clientSocket);
28             }        
29         }
30         public static void FenFaMessage(string message)
31         {
32             List<Client> clients = new List<Client>();//用于存储无效消息;
33 
34             foreach (var item in listClient)
35             {
36                 if (item.Connected)//如果连接正常,消息有效去发送
37                 {
38                     item.SendMessage(message);
39                 }
40                 else
41                 {
42                     clients.Add(item);
43                    
44                 }
45             }
46             foreach (var item in clients)//移除无效消息
47             {
48                 listClient.Remove(item);
49             }
50 
51 
52         }
53     }
54 }
服务器主函数代码

服务器类里的代码:

 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 namespace 聊天室服务器
10 {
11     /// <summary>
12     /// 消息通讯处理类 接收和发送消息
13     /// </summary>
14     class Client
15     {
16         public Socket socket;
17         public Client(Socket client)
18         {
19 
20             socket = client;
21             Thread thread = new Thread(ReceveMessage);
22             thread.Start();
23         }
24 
25         public void ReceveMessage()
26         {
27             while (true)
28             {
29                 if (socket.Poll(10, SelectMode.SelectRead))//判断连接是否中断
30                 {
31                     socket.Close();
32                     break;
33                 }
34                 byte[] data = new byte[1024];
35                 int length = socket.Receive(data);
36                 string message = Encoding.UTF8.GetString(data, 0, length);
37                 Console.WriteLine("客户端发来消息是" + message);
38                 Program.FenFaMessage(message);
39             }
40 
41 
42         }
43         public void SendMessage(string str)
44         {
45             byte[] datas = Encoding.UTF8.GetBytes(str);
46             socket.Send(datas);
47         }
48         public bool Connected {
49 
50             get { return socket.Connected; }
51         }
52     }
53 }
服务器的分发类的代码


猜你喜欢

转载自www.cnblogs.com/satanj/p/9759090.html