Example #1
0
        static void Main(string[] args)
        {
            if (args.Length > 0) {
                ipAddress = IPAddress.Parse(args[0]);
            }
            localEndPoint = new IPEndPoint(ipAddress, 8000);

            var protocol = new Protocol(new UTF8Encoding(), 500);
            var serverList = new List<string>();
            Socket listeningSocket = null;
            Socket[] sockets = new Socket[15];
            int i = 0;
            listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            listeningSocket.Bind(localEndPoint);
            Console.WriteLine("MasterServer initialized");
            while (true) {
                listeningSocket.Listen(100);
                sockets[i] = listeningSocket.Accept();
                protocol.Socket = sockets[i];
                var r = protocol.Receive();
                if (r.Data == "im a server i promise") {
                    Console.WriteLine($"Server {i} connected");
                    string idAndPort = i.ToString() + " " + clientPort;
                    protocol.Send(new Response(ResponseStatus.OK, DataType.TEXT, clientPort.ToString()));
                    serverList.Add(idAndPort);
                    i++;
                    clientPort++;
                    foreach (var server in serverList) {
                        var info = server.Split(' ');
                        Console.WriteLine($"ServerID: {info[0]}     Port: {info[1]}");
                    }
                } else if (r.Data == "list") {
                    Console.WriteLine("Client connected");
                    string data = "";
                    foreach (var server in serverList) {
                        data += " " + server;
                    }
                    protocol.Send(new Response(ResponseStatus.OK, DataType.TEXT, data));
                    i++;
                }
            }
        }
Example #2
0
        static void GetServerList()
        {
            var masterServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            masterServer.Connect(masterServerEndPoint);
            Protocol protocol = new Protocol(new UTF8Encoding(), 2000, masterServer);

            protocol.Send(new Request(RequestStatus.NONE, DataType.TEXT, "list"));
            var r = protocol.Receive() as Response;
            var response = r.Data.TrimStart(' ').Split(' ');
            Console.WriteLine("List of servers:");
            foreach (var server in response) {
                if (server.Length > 1)
                    Console.WriteLine();
                else
                    Console.WriteLine($"ServerID: {server}");
            }
            Console.Write("Enter server id: ");
            int input = int.Parse(Console.ReadLine());
            int serverPort = int.Parse(response[input + 1]);
            masterServer.Close();
            masterServerEndPoint = new IPEndPoint(ipAddress, serverPort);
            Console.Clear();
        }
Example #3
0
 static void ConnectToServer()
 {
     client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     client.Connect(masterServerEndPoint);
     protocol = new Protocol(new UTF8Encoding(), 20000, client);
 }
Example #4
0
        static void ConnectToMasterServer()
        {
            masterServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            masterServer.Connect(masterServerEndPoint);
            LogEntry("Connected to MasterServer");
            Protocol protocol = new Protocol(new UTF8Encoding(), 100, masterServer);

            protocol.Send(new Request(RequestStatus.NONE, DataType.TEXT, "im a server i promise"));

            var response = (Response)protocol.Receive();
            if (response.Status == ResponseStatus.OK) {
                serverEndPoint = new IPEndPoint(ipAddress, int.Parse(response.Data));
            }
        }
Example #5
0
 public Client(Protocol.Protocol protocol)
 {
     Protocol = protocol;
 }