-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.cs
40 lines (36 loc) · 1.28 KB
/
Network.cs
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
using System;
using System.Net;
using System.Net.Sockets;
namespace Server
{
class Network
{
public TcpListener ServerSocket;
public void InitTCP()
{
ServerSocket = new TcpListener(IPAddress.Any, 5500);
ServerSocket.Start();
ServerSocket.BeginAcceptTcpClient(OnClientConnect, null);
Console.WriteLine("Server has successfully started.");
}
void OnClientConnect(IAsyncResult result)
{
TcpClient client = ServerSocket.EndAcceptTcpClient(result);
client.NoDelay = false;
ServerSocket.BeginAcceptTcpClient(OnClientConnect, null);
for (int i = 1; i < Constants.MAX_PLAYERS; i++)
{
if (Globals.Clients[i].Socket == null)
{
Globals.Clients[i].Socket = client;
Globals.Clients[i].Index = i;
Globals.Clients[i].IP = client.Client.RemoteEndPoint.ToString();
Globals.Clients[i].Start();
Console.WriteLine("Incoming Connection from " + Globals.Clients[i].IP + " Index: " + i);
Globals.networkSendData.SendJoinGame(i);
break;
}
}
}
}
}