Наследование: System.EventArgs
Пример #1
0
        public void stopServer()
        {
            if (tcpListener != null && serverThread != null && lobby != null)
            {
                serverThread.Abort();

                foreach (ServerClient client in lobby.getClientlist()) lobby.remove(client);

                idCounter = 0;
                tcpListener.Stop();
            }

            ServerInfoEventArgs args = new ServerInfoEventArgs();
            args.consoleinfo = "Server Closed Successfully!\n";
            OnNeedLog(args);
        }
Пример #2
0
        private void acceptConnection(Int32 _inputsize)
        {
            int clientid = clientsockets.Count;
            Socket clinetsocket = clientsockets[clientid-1];

                ServerInfoEventArgs args = new ServerInfoEventArgs();
                args.consoleinfo = "Connection accepted from " + clinetsocket.RemoteEndPoint + "\n";
                OnNeedLog(args);

                ServerClient client = new ServerClient();
                client.id = clientid;
                client.port = ((IPEndPoint)clinetsocket.RemoteEndPoint).Port.ToString();
                client.ip = ((IPEndPoint)clinetsocket.RemoteEndPoint).Address.ToString();
                client.name = recieveData(clinetsocket,_inputsize);   // itt várakozik az inputra
                client.gametype = "";

                lobby.add(client);

                clinetsocket.Send(new ASCIIEncoding().GetBytes("Users in the lobby.\n" + lobby.getClientsName()));
        }
Пример #3
0
 protected virtual void OnNeedLog(ServerInfoEventArgs e)
 {
     EventHandler<ServerInfoEventArgs> handler = needlog;
     if (handler != null)
     {
         handler(this, e);
     }
 }
Пример #4
0
        public void stratserver(String _ip, Int32 _port)
        {
            try
            {
                serverthread = new Thread(() =>
                {
                    ipAddress = IPAddress.Parse(_ip);
                    tcpListener = new TcpListener(ipAddress, _port);
                    tcpListener.Start();

                    ServerInfoEventArgs args = new ServerInfoEventArgs();
                    args.consoleinfo = "The server is running at port " + _port + " ...\n";
                    OnNeedLog(args);
                    args.consoleinfo = "The local End point is: " + tcpListener.LocalEndpoint + "\n";
                    OnNeedLog(args);
                    args.consoleinfo = "Waiting for a connection.....\n";
                    OnNeedLog(args);

                    clientsockets = new List<Socket>();
                    gamelist = new List<Game>();
                    lobby = new Lobby();

                    while (true)
                    {
                        if (tcpListener.Pending())
                        {
                            clientsockets.Add(tcpListener.AcceptSocket());
                            acceptConnection(100);
                        }
                    }

                });

                serverthread.Start();

            }
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.Message);
            }
        }
Пример #5
0
        public void stopserver()
        {
            if (tcpListener != null && serverthread != null)
            {
                foreach(Socket socket in clientsockets)
                {
                   socket.Close();
                }
                tcpListener.Stop();

                serverthread.Abort();
            }

            ServerInfoEventArgs args = new ServerInfoEventArgs();
            args.consoleinfo = "Server Closed Successfully!\n";
            OnNeedLog(args);
        }
Пример #6
0
        public void stratServer(String _ip, Int32 _port)
        {
            try
            {
                serverThread = new Thread(() =>
                {
                    ipAddress = IPAddress.Parse(_ip);
                    tcpListener = new TcpListener(ipAddress, _port);
                    tcpListener.Start();

                    ServerInfoEventArgs args = new ServerInfoEventArgs();
                    args.consoleinfo = "The server is running at port " + _port + " ...\n";
                    OnNeedLog(args);
                    args.consoleinfo = "The local End point is: " + tcpListener.LocalEndpoint + "\n";
                    OnNeedLog(args);
                    args.consoleinfo = "Waiting for a connection.....\n";
                    OnNeedLog(args);

                    while (true)
                    {
                        if (tcpListener.Pending())
                        {
                            Console.WriteLine("Adding ID: " + Convert.ToString(idCounter));
                            Socket socket = tcpListener.AcceptSocket();
                            lobby.add(new ServerClient(
                                idCounter,
                                ((IPEndPoint)socket.RemoteEndPoint).Port.ToString(),
                                ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(),
                                recieveData(socket, 100),
                                recieveData(socket, 100),
                                socket,
                                false
                            ));

                            idCounter++;
                            string sendString = ((IPEndPoint)socket.RemoteEndPoint).Address.ToString();
                            sendString += ";" + ((IPEndPoint)socket.RemoteEndPoint).Port.ToString();

                            socket.Send(new ASCIIEncoding().GetBytes(sendString));
                        }

                        List<ServerClient>clList = lobby.getClientlist();

                        foreach (ServerClient client in clList)
                        {
                            if (!client.startedrecive)
                            {
                                client.startedrecive = true;

                                Thread recieve = new Thread(() =>
                                {
                                    while (true)
                                    {
                                        recieveDone.Reset();
                                        StateObject state = new StateObject();
                                        state.workSocket = client.clientsocket;

                                        client.clientsocket.BeginReceive(state.buffer, 0, StateObject.BufferSize,
                                                                         0, new AsyncCallback(receiveCompleted), state);
                                        recieveDone.WaitOne();
                                    }

                                });

                                recieve.Start();
                            }

                        }

                    }

                });

                serverThread.Start();

            }
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.Message);
            }
        }