예제 #1
0
        static void HandlePacket(ClientClass client, Packet packet)
        {
            ClientClass currentClient = client;

            switch (packet.type)
            {
            case PacketType.CHATMESSAGE:

                string message = ((ChatMessagePacket)packet)._message;

                if (message != "/exit")
                {
                    ChatMessagePacket chatmessagepacket = new ChatMessagePacket(message);

                    currentClient.Send(chatmessagepacket);
                }
                else
                {
                    string exitAnnounce = "[user has disconnected]";

                    ChatMessagePacket chatmessagepacket = new ChatMessagePacket(exitAnnounce);

                    currentClient.Send(chatmessagepacket);
                }

                break;
            }
        }
예제 #2
0
        /*public bool Connect(string ipAddress, int port)
         * {
         *  try
         *  {
         *      tcpListener.Connect(ipAddress, port);
         *
         *  }
         *  catch (Exception e)
         *  {
         *      Console.WriteLine("ExceptionS " + e.Message);
         *      return false;
         *  }
         *
         *  return true;
         * }*/

        public void Start()
        {
            Console.WriteLine("Initialising...");

            tcpListener.Start();

            while (true)
            {
                Socket currentSocket = tcpListener.AcceptSocket();
                //get the int value for the client index
                currClientNumb++;

                ClientClass currentClient = new ClientClass(currentSocket, currClientNumb);

                clientsConnected.Add(currentClient);
                //add to clientsConnected list

                /*if (clientsConnected.Count == 0)
                 * {
                 *  clientsConnected.Add(currentSocket);
                 * }
                 * else
                 * {
                 *  for (int i = 0; i < clientsConnected.Count; i++)
                 *  {
                 *      if (currentSocket == clientsConnected[i])
                 *      {
                 *          //currClientNumb = i;
                 *          break;
                 *      }
                 *      else if (clientsConnected[i] == null)
                 *      {
                 *          clientsConnected[i] = currentSocket;
                 *      }
                 *  }
                 * }*/

                //currClientNumb++; // To make sure client number shown doesn't start at zero


                Thread thread = new Thread(new ParameterizedThreadStart(ClientMethod));

                thread.Start(currentClient);

                Console.WriteLine("Client Connected...");
            }
        }
예제 #3
0
        static void HandlePacket(ClientClass clientReceived, Packet packet)
        {
            ChatMessagePacket chatmessagepacket;
            string            message;

            switch (packet.type)
            {
            case PacketType.NICKNAME:

                clientReceived.nickname = ((NicknamePacket)packet)._nickName;

                //nicknamepacket = new NicknamePacket(client.nickname);

                break;

            case PacketType.CHATMESSAGE:

                for (int i = 0; i < clientsConnected.Count; i++)
                {
                    ClientClass currentClientToSendTo = clientsConnected[i];

                    message = ((ChatMessagePacket)packet)._message;

                    if (message != "/exit")
                    {
                        chatmessagepacket = new ChatMessagePacket(clientReceived.nickname + clientReceived.idNUM + ": " + message);
                    }
                    else
                    {
                        string exitAnnounce = "[user has disconnected]";

                        chatmessagepacket = new ChatMessagePacket(exitAnnounce);
                    }

                    currentClientToSendTo.Send(chatmessagepacket);
                }

                break;
            }
        }
예제 #4
0
        static void ClientMethod(object clientObject)
        {
            ClientClass client = (ClientClass)clientObject;

            string clientCountDisplay = client.thisClient.ToString();

            int noOfIncomingBytes = 0;

            while ((noOfIncomingBytes = client.reader.ReadInt32()) != 0)
            {
                byte[] bites = client.reader.ReadBytes(noOfIncomingBytes);

                MemoryStream byteStream = new MemoryStream(bites);

                Packet packet = formatter.Deserialize(byteStream) as Packet;

                for (int i = 0; i < clientsConnected.Count; i++)
                {
                    ClientClass selectedClient = clientsConnected[i];
                    //if (selectedClient != client)
                    //{
                    HandlePacket(selectedClient, packet);
                    //}

                    /*if (receivedMessage == "/exit")
                     * {
                     *  break;
                     * }*/
                }
            }

            client.Close();

            //Console.WriteLine(reader.ReadLine());

            /*client.writer.WriteLine("message sent...");
             * client.writer.Flush();
             *
             *
             * string clientCountDisplay = client.thisClient.ToString();
             *
             * while ((receivedMessage = client.reader.ReadLine()) != null)
             * {
             *  client.writer.WriteLine("user " + clientCountDisplay + ": " + GetReturnMessage(receivedMessage));
             *  client.writer.Flush();
             *
             *  for (int i = 0; i < clientsConnected.Count; i++)
             *  {
             *      ClientClass selectedClient = clientsConnected[i];
             *      if (selectedClient != client)
             *      {
             *          selectedClient.writer.WriteLine("user " + clientCountDisplay + ": " + GetReturnMessage(receivedMessage));
             *          selectedClient.writer.Flush();
             *      }
             *  }
             *
             *  //if (receivedMessage == "exit")
             *  //{
             *  //    break;
             *  //}
             * }
             *
             * client.Close();*/
        }