forked from XINCGer/Unity3DTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
85 lines (74 loc) · 2.78 KB
/
Copy pathProgram.cs
File metadata and controls
85 lines (74 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace SocketServer
{
class Program
{
//缓冲区
private static byte[] dataBuff = new byte[1024];
//端口号
private static int port = 6666;
//socket
private static Socket socketServer;
static void Main(string[] args)
{
//设置监听的IP地址和端口,这里使用回环地址
IPAddress ip = IPAddress.Loopback;
//实例化一个socket对象,确定网络类型,socket类型,协议类型
socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//绑定端口与IP
socketServer.Bind(new IPEndPoint(ip, port));
//挂起连接队列的最大长度为15,并启动监听
socketServer.Listen(15);
Console.WriteLine("启动监听{0}成功!", socketServer.LocalEndPoint);
//创建一个线程用来处理客户端的连接
Thread thread = new Thread(ListenClientConnect);
thread.Start();
}
/// <summary>
/// 处理客户端的连接
/// </summary>
private static void ListenClientConnect()
{
while (true)
{
//运行到Accept()方法会阻塞程序(同步Socket)
//一收到客户端的请求,就新建一个线程来处理数据
Socket socketClient = socketServer.Accept();
socketClient.Send(Encoding.UTF8.GetBytes("Server:你好!客户端!"));
Thread thread = new Thread(ReciveMessage);
thread.Start(socketClient);
}
}
/// <summary>
/// 接收消息
/// </summary>
/// <param name="socketClient"></param>
private static void ReciveMessage(object socketClient)
{
if (null != socketClient)
{
Socket mySocketClient = socketClient as Socket;
while (true)
{
try
{
//通过socketClient来接收数据
int reciveNum = mySocketClient.Receive(dataBuff);
Console.WriteLine("接收客户端:{0}消息{1}", mySocketClient.RemoteEndPoint, Encoding.UTF8.GetString(dataBuff, 0, reciveNum));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
mySocketClient.Shutdown(SocketShutdown.Both);
mySocketClient.Close();
break;
}
}
}
}
}
}