예제 #1
0
        static void Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(IPAddress.Any, 20000);
            int         counter      = 0;

            serverSocket.Start();
            Console.WriteLine(" >> Server Started");

            while (true)
            {
                counter += 1;
                var clientSocket = serverSocket.AcceptTcpClient();
                Console.WriteLine(" >> Client No: " + counter + " Started!");
                HandleClient client = new HandleClient();
                client.StartClient(clientSocket);
            }
        }
예제 #2
0
파일: Server.cs 프로젝트: Waszok/SnakeGame
 /// <summary>
 /// Glowna funkcja.
 /// </summary>
 /// <param name="args"></param>
 static void Main(string[] args)
 {
     try
     {
         _localAddr = IPAddress.Parse(GetIpAddress());
         //_localAddr = IPAddress.Parse("127.0.0.1");
         _port           = 8888;
         _serverListener = new TcpListener(_localAddr, _port);
         _serverListener.Start();
         Console.WriteLine(" >> " + "Server Started");
         Console.WriteLine("The local End point is: " + _serverListener.LocalEndpoint);
         while (true)
         {
             if (_clientSockets.Count < 2)
             {
                 TcpClient _clientSocketNew = _serverListener.AcceptTcpClient(); //zwarca obiekt klasy TcpClient
                                                                                 //odpowiada za gniazdo klienckie
                 SearchAvailableId();
                 DataClients data = new DataClients(_currentId, _clientSocketNew);
                 _clientSockets.Add(data);
                 int lastId = _currentId;
                 _currentId = 1;
                 HandleClient client = new HandleClient();
                 client.StartClient(_clientSocketNew, lastId);
             }
             else
             {
                 Console.WriteLine("Server is full!!!");
                 throw new SocketException();
             }
         }
     }
     catch (SocketException e)
     {
         Console.WriteLine("SocketException: {0}", e);
     }
     finally
     {
         _serverListener.Stop();
     }
 }
예제 #3
0
        static void Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(10_000);
            TcpClient   clientSocket = default(TcpClient);
            int         counter      = 0;

            serverSocket.Start();
            Console.WriteLine("Chat Server started .....");
            counter = 0;

            while ((true))
            {
                counter     += 1;
                clientSocket = serverSocket.AcceptTcpClient();

                //byte[] bytesFrom = new byte[10025];
                string dataFromClient = null;

                NetworkStream networkStream = clientSocket.GetStream();

                int    test      = clientSocket.ReceiveBufferSize;
                byte[] bytesFrom = new byte[test];

                networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

                clientList.Add(dataFromClient, clientSocket);

                Broadcast(dataFromClient + " Joined ", dataFromClient, false);

                Console.WriteLine(dataFromClient + " Joined chat room ");
                HandleClient client = new HandleClient();
                client.StartClient(clientSocket, dataFromClient, clientList);
            }
            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine("exit");
            Console.ReadKey();
        }
예제 #4
0
        public static void ListenToNewClients()
        {
            //IPHostEntry host = Dns.GetHostEntry("localhost");
            //IPAddress ipAddress = host.AddressList[0];

            TcpListener serverSocket = new TcpListener(IPAddress.Parse("127.0.0.1"), 11000);

            serverSocket.Start();
            Console.WriteLine(" >> Server Started");

            int counter = 0;

            while (true)
            {
                counter += 1;
                TcpClient clientSocket = serverSocket.AcceptTcpClient();
                Console.WriteLine(" >> Client No:" + Convert.ToString(counter) + " started!");
                HandleClient handleClient = new HandleClient();
                handleClientList.Add(handleClient);
                handleClient.StartClient(clientSocket, Convert.ToString(counter));
            }
        }
예제 #5
0
        /// <summary>
        /// Entry Point
        /// Opens a server, starts the admin thread, and handles client connections
        /// </summary>
        private static void Main()
        {
            //Connection information
            var ip           = System.Net.IPAddress.Loopback;
            var serverSocket = new TcpListener(ip, 8888);
            var clientSocket = default(TcpClient);

            serverSocket.Start();
            Console.WriteLine(" >> " + "Server Started");

            var adminThread = new Thread(AdminLoop);

            adminThread.Start();
            while (!Office.Open)
            {
                //Wait for sales to open
            }

            //Continually loop and wait for client connections
            while (Office.Open)
            {
                if (!serverSocket.Pending())
                {
                    continue;
                }
                clientSocket = serverSocket.AcceptTcpClient();
                var client = new HandleClient();
                client.StartClient(clientSocket);
            }

            //Manage resources and wrap up
            Console.WriteLine("Waiting for all clients to disconnect...");
            adminThread.Join();
            clientSocket?.Close();
            serverSocket.Stop();
            Console.WriteLine(" >> " + "exit");
        }
예제 #6
0
        static void Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(IPAddress.Any, 9000);
            TcpClient   clientSocket = default(TcpClient);
            int         counter      = 0;

            try
            {
                serverSocket.Start();
                Console.WriteLine("GPS Tracker Server Started ....");
                while (true)
                {
                    counter     += 1;
                    clientSocket = serverSocket.AcceptTcpClient();
                    Console.WriteLine("Connected");
                    byte[] bytesFrom      = new byte[10025];
                    string dataFromClient = null;

                    NetworkStream networkStream = clientSocket.GetStream();
                    networkStream.Read(bytesFrom, 0, 10025);
                    dataFromClient = Encoding.ASCII.GetString(bytesFrom);
                    //Console.WriteLine("GPS data is: " + dataFromClient);
                    //System.IO.File.WriteAllText(@"C:\_djivanovic\trackerdata.txt", dataFromClient);
                    int      contentParseLength = dataFromClient.Split(',').Length;
                    String[] contentParse       = dataFromClient.Split(',');
                    String   prepare            = contentParse.ElementAtOrDefault(1);
                    String   imei = null;

                    if (prepare != null)
                    {
                        String[] prepareImei = prepare.Split(':');
                        imei = prepareImei.ElementAtOrDefault(1);
                    }

                    if (imei != null && !clientsList.Contains(imei))
                    {
                        clientsList.Add(imei, clientSocket);
                    }
                    else
                    {
                        Console.WriteLine("Assign new socket for client");
                        clientsList[imei] = clientSocket;
                        continue;
                    }

                    if (contentParseLength == 3 && imei != null)
                    {
                        Broadcast.BroadcastClientData("LOAD", imei, false, clientsList);
                    }
                    //else
                    //{
                    //    continue;
                    //}
                    //broadcast(dataFromClient + " Joined ", dataFromClient, false);

                    Console.WriteLine(imei + " Joined GPS tracker ");
                    HandleClient client = new HandleClient();
                    client.StartClient(clientSocket, imei, clientsList, dataFromClient);
                }
            }

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

            finally
            {
                clientSocket.Close();
                serverSocket.Stop();
                Console.WriteLine("exit");
                Console.ReadLine();
            }
        }