Пример #1
0
        private void NewConnection(Socket sockClient)
        {
            // Program blocks on Accept() until a client connects.
            //SocketChatClient client = new SocketChatClient( listener.AcceptSocket() );
            SocketSessionClient client = new SocketSessionClient(sockClient);

            //client.Sock.SendTimeout = 1000;
            //client.Sock.ReceiveTimeout = 1000;

            m_aryClients.Add(client);

            if (OnSocketConnect != null)
            {
                OnSocketConnect(string.Format("Client {0}, joined...", client.Sock.RemoteEndPoint), client.Sock.RemoteEndPoint.ToString());
            }

            //Console.WriteLine(string.Format("Client {0}, joined\r\n", client.Sock.RemoteEndPoint));

            //// Get current date and time.
            //DateTime now = DateTime.Now;
            //String strDateLine = "Welcome " + now.ToString("G") + "\n\r";
            //// Convert to byte array and send.
            //Byte[] byteDateLine = System.Text.Encoding.ASCII.GetBytes(strDateLine.ToCharArray());
            //client.Sock.Send(byteDateLine, byteDateLine.Length, 0);

            client.SetupRecieveCallback(this);
        }
Пример #2
0
        public void OnRecievedData(IAsyncResult ar)
        {
            try
            {
                SocketSessionClient client = (SocketSessionClient)ar.AsyncState;
                byte[] aryRet = client.GetRecievedData(ar);

                // If no data was recieved then the connection is probably dead
                if (aryRet.Length < 1)
                {
                    if (OnSocketDisconnect != null)
                    {
                        OnSocketDisconnect(string.Format("Client {0}, disconnected\r\n", client.Sock.RemoteEndPoint), client.Sock.RemoteEndPoint.ToString());
                    }

                    //Console.WriteLine(string.Format("Client {0}, disconnected\r\n", client.Sock.RemoteEndPoint));

                    client.Sock.Close();
                    m_aryClients.Remove(client);
                    return;
                }

                //Console.WriteLine(aryRet.Length.ToString());


                if (OnGetRecMSG != null)
                {
                    OnGetRecMSG(aryRet, client.Sock.RemoteEndPoint.ToString());
                }
                // Send the recieved data to all clients (including sender for echo)
                //foreach (SocketChatClient clientSend in m_aryClients)
                //{
                //    try
                //    {
                //        clientSend.Sock.Send(aryRet);
                //    }
                //    catch
                //    {
                //        // If the send fails the close the connection
                //        Console.WriteLine(string.Format("Send to client {0} failed\r\n", client.Sock.RemoteEndPoint));

                //        if (OnSocketDisconnect != null)
                //        {
                //            OnSocketDisconnect(string.Format("Send to client {0} failed! Disconnected!\r\n", client.Sock.RemoteEndPoint));
                //        }

                //        clientSend.Sock.Close();
                //        m_aryClients.Remove(client);
                //        return;
                //    }
                //}
                client.SetupRecieveCallback(this);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }