예제 #1
0
        /// <summary>
        /// When new client is added
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ClientAdded(object sender, EventArgs e)
        {
            var socket = ((CustomEventArgs) e).ClientSocket;
            var bytes = new byte[1024];
            var bytesRead = socket.Receive(bytes);

            var newUserName = Encoding.Unicode.GetString(bytes, 0, bytesRead);

            if (clients.Any(client => client.UserName == newUserName))
            {
                SendNameAlreadyExist(socket,newUserName);
                return;
            }

            var newClient = new ConnectedClient(newUserName, socket);
            clients.Add(newClient);

            OnClientConnected(socket, newUserName);

            foreach (var client in clients)
                SendUsersList(client.Connection, client.UserName, newUserName, ChatHelper.CONNECTED);

            var state = new ChatHelper.StateObject
            {
                WorkSocket = socket
            };

            socket.BeginReceive(state.Buffer, 0, ChatHelper.StateObject.BUFFER_SIZE, 0,
            OnReceive, state);

            ChatHelper.WriteToEventLog(Log.ClientConnected, EventLogEntryType.Information);
        }
예제 #2
0
 private void RecieveFromServer()
 {
     var state = new ChatHelper.StateObject
     {
         WorkSocket = server.Client
     };
     while (IsConnected)
     {
         if (IsReceivingData)
             continue;
         IsReceivingData = true;
         server.Client.BeginReceive(state.Buffer, 0, ChatHelper.StateObject.BUFFER_SIZE, 0,
             OnReceive, state);
     }
 }
예제 #3
0
 private void ReceiveUdpData()
 {
     try
     {
         var state = new ChatHelper.StateObject { WorkSocket = clientSocket };
         var ep = remoteEndPoint as EndPoint;
         clientSocket.BeginReceiveFrom(state.Buffer, 0, ChatHelper.StateObject.BUFFER_SIZE, SocketFlags.None, ref ep,
             OnUdpRecieve, state);
     }
     catch (Exception)
     {
         udpConnectionActive = false;
     }
 }