lyh916

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1.客户端

 1 using System.Net.Sockets;
 2 using System.Text;
 3 using UnityEngine;
 4 using UnityEngine.UI;
 5 using System;
 6 
 7 public class TestSocket2 : MonoBehaviour
 8 {
 9     private Socket socket;
10     public InputField inputField;
11     public Text text;
12 
13     private byte[] readBytes = new byte[1024];
14     private string readStr = "";
15 
16     void Start()
17     {
18         socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
19     }
20 
21     void Update()
22     {
23         text.text = readStr;
24     }
25 
26     public void TestConnect()
27     {
28         //Connect
29         socket.BeginConnect("127.0.0.1", 8888, ConnectCallBack, socket);
30     }
31 
32     public void TestSend()
33     {
34         //Send
35         byte[] writeBytes = Encoding.Default.GetBytes(inputField.text);
36         socket.BeginSend(writeBytes, 0, writeBytes.Length, 0, SendCallBack, socket);
37     }
38 
39     private void ConnectCallBack(IAsyncResult asyncResult)
40     {
41         try
42         {
43             Socket socket = (Socket)asyncResult.AsyncState;
44             socket.EndConnect(asyncResult);
45 
46             //Receive
47             socket.BeginReceive(readBytes, 0, 1024, 0, ReceiveCallBack, socket);
48         }
49         catch (Exception exception)
50         {
51             Debug.LogError(exception.ToString());
52         }
53     }
54 
55     private void ReceiveCallBack(IAsyncResult asyncResult)
56     {
57         try
58         {
59             Socket socket = (Socket)asyncResult.AsyncState;
60             int count = socket.EndReceive(asyncResult);
61 
62             //在unity中,只有主线程才可以操作UI组件。因此不能直接对text.text进行赋值
63             readStr = Encoding.Default.GetString(readBytes, 0, count);
64             //text.text = readStr;
65 
66             //Receive
67             socket.BeginReceive(readBytes, 0, 1024, 0, ReceiveCallBack, socket);
68         }
69         catch (Exception exception)
70         {
71             Debug.LogError(exception.ToString());
72         }
73     }
74 
75     private void SendCallBack(IAsyncResult asyncResult)
76     {
77         try
78         {
79             Socket socket = (Socket)asyncResult.AsyncState;
80             int count = socket.EndSend(asyncResult);
81         }
82         catch (Exception exception)
83         {
84             Debug.LogError(exception.ToString());
85         }
86     }
87 }

 

2.服务端

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Net;
 4 using System.Net.Sockets;
 5 using System.Text;
 6 
 7 namespace TestSocket2
 8 {
 9     class ClientState
10     {
11         public Socket socket;
12         public byte[] receiveBytes = new byte[1024];
13     }
14 
15     class Program
16     {
17         static Dictionary<Socket, ClientState> clientDic = new Dictionary<Socket, ClientState>();
18 
19         static void Main(string[] args)
20         {
21             Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
22 
23             //Bind
24             IPAddress iPAddress = IPAddress.Parse("127.0.0.1");
25             IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, 8888);
26             socket.Bind(iPEndPoint);
27 
28             //Listen
29             socket.Listen(0);
30             Console.WriteLine("Listen");
31 
32             //Accept
33             socket.BeginAccept(AcceptCallBack, socket);
34 
35             Console.ReadLine();
36         }
37 
38         private static void AcceptCallBack(IAsyncResult asyncResult)
39         {
40             try
41             {
42                 Console.WriteLine("Accept");
43                 Socket serverSocket = (Socket)asyncResult.AsyncState;
44                 Socket clientSocket = serverSocket.EndAccept(asyncResult);
45                 ClientState clientState = new ClientState();
46                 clientState.socket = clientSocket;
47                 clientDic.Add(clientSocket, clientState);
48 
49                 //Receive
50                 clientSocket.BeginReceive(clientState.receiveBytes, 0, 1024, 0, ReceiveCallBack, clientState);
51 
52                 //Accept
53                 serverSocket.BeginAccept(AcceptCallBack, serverSocket);
54             }
55             catch (Exception exception)
56             {
57                 Console.WriteLine(exception.ToString());
58             }
59         }
60 
61         private static void ReceiveCallBack(IAsyncResult asyncResult)
62         {
63             try
64             {
65                 ClientState clientState = (ClientState)asyncResult.AsyncState;
66                 Socket clientSocket = clientState.socket;
67                 int count = clientSocket.EndReceive(asyncResult);
68 
69                 //客户端关闭
70                 if (count == 0)
71                 {
72                     clientSocket.Close();
73                     clientDic.Remove(clientSocket);
74                     return;
75                 }
76 
77                 string str = Encoding.Default.GetString(clientState.receiveBytes, 0, count);
78                 clientSocket.Send(Encoding.Default.GetBytes(str));
79                 clientSocket.BeginReceive(clientState.receiveBytes, 0, 1024, 0, ReceiveCallBack, clientState);
80                 Console.WriteLine("Receive:" + str);
81             }
82             catch (Exception exception)
83             {
84                 Console.WriteLine(exception.ToString());
85             }
86         }
87     }
88 }

 

posted on 2021-10-24 16:10  艰苦奋斗中  阅读(99)  评论(0编辑  收藏  举报