Beispiel #1
0
        public bool getNewUser(int status, string user_id, TcpClient clientSocket)
        {
            try
            {
                User user = new User(status, user_id);
                UserWithSocket UserWSocket = new UserWithSocket(user, clientSocket);

                this.userlistwithoutsocket.Add(user);
                this.userlist.Add(UserWSocket);

                return true;
            }
            catch
            {
                return false;
            }
        }
Beispiel #2
0
        public void test()
        {
            TcpListener serverSocket = new TcpListener(8001);
            TcpClient clientSocket = default(TcpClient);
            serverSocket.Start();
            Console.WriteLine("Lobby Server Started..");

            while (true)
            {
                clientSocket = serverSocket.AcceptTcpClient();
                Console.WriteLine("Lobby Success");

                byte[] bytesFrom = new byte[LENGTH.MAX_PACKET_LEN];
                byte[] bytesFrom2 = new byte[LENGTH.MAX_PACKET_LEN];

                Byte[] bytesTo = null;
                string dataFromClient = null;

                NetworkStream networkStream = clientSocket.GetStream();
                networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);

                User user = new User();
                user = JsonConvert.DeserializeObject<User>(dataFromClient);

                UserWithSocket userWithSocket = new UserWithSocket(user, clientSocket);

                userinfo.myInformation = user;

                userinfo.userlist.Add(userWithSocket);
                userinfo.userlistwithoutsocket.Add(user);

                string output = JsonConvert.SerializeObject(userinfo.userlistwithoutsocket);
                bytesFrom = System.Text.Encoding.ASCII.GetBytes(output);

                networkStream.Write(bytesFrom, 0, bytesFrom.Length);
                networkStream.Flush();

                foreach (UserWithSocket uws in userinfo.userlist)
                {
                    NetworkStream nws = uws.clientSocket.GetStream();
                    nws.Write(bytesFrom, 0, bytesFrom.Length);
                    nws.Flush();
                }

                Console.WriteLine("Joined ID: " + userinfo.myInformation.user_id);

                handleClinet client = new handleClinet();
                client.startClient(userWithSocket.clientSocket, dataFromClient, userinfo.userlist);
            }

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