Created
October 28, 2017 21:59
-
-
Save HurricanKai/297272afadffce07eb7f856964a8a2eb to your computer and use it in GitHub Desktop.
Server Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Server | |
{ | |
public TcpListener listener; | |
Task listenerTask; | |
private bool online; | |
private Dictionary<TcpClient, Task> clienttasks; | |
public Server(int port) | |
{ | |
online = true; | |
listener = new TcpListener(IPAddress.Any, port); | |
listenerTask = new Task(() => | |
{ | |
while (online) | |
{ | |
if (listener.Pending()) | |
{ | |
var client = listener.AcceptTcpClient(); | |
Task clienttask = new Task(() => | |
{ | |
var s = client.GetStream(); | |
var mcs = new MinecraftStream(s); | |
while (client.Connected) | |
{ | |
if (client.Available > 0) | |
{ | |
int i = mcs.ReadInt32(); | |
Console.WriteLine($"Got an {i}"); | |
} | |
} | |
}); | |
clienttasks.Add(client, clienttask); | |
clienttask.Start(); | |
} | |
foreach (var c in clienttasks) | |
if (!c.Key.Connected) | |
clienttasks.Remove(c.Key); | |
} | |
}); | |
listenerTask.Start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment