Exemplo n.º 1
0
        private void RecieveCallback(IAsyncResult ar)
        {
            RecieveStateObject state   = (RecieveStateObject)ar.AsyncState;
            string             content = string.Empty;

            if (!state.WorkSocket.Connected)
            {
                return;
            }
            int bytesRead = state.WorkSocket.EndReceive(ar);

            if (bytesRead > 0)
            {
                state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead));
                content = state.sb.ToString();
                int indexOfEOM = content.IndexOf("<EOM>");
                while (indexOfEOM > -1)
                {
                    String message = content.Substring(0, indexOfEOM); // Ignores EOM
                    HandleMessage(message);

                    content = content.Substring(indexOfEOM + 5); // Ignores EOM


                    indexOfEOM = content.IndexOf("<EOM>");
                }
                state.sb = new StringBuilder(content);
            }
            DoBeginRecieve(state);
        }
Exemplo n.º 2
0
        private void StartRecieve(Socket s)
        {
            RecieveStateObject so = new RecieveStateObject();

            so.WorkSocket = s;

            DoBeginRecieve(so);
        }
Exemplo n.º 3
0
 private void DoBeginRecieve(RecieveStateObject so)
 {
     if (!so.WorkSocket.Connected)
     {
         return;
     }
     try
     {
         so.WorkSocket.BeginReceive(so.buffer, 0, RecieveStateObject.BufferSize, 0, new AsyncCallback(RecieveCallback), so);
     } catch (Exception ex) when(ex is SocketException || ex is ObjectDisposedException)
     {
         OnDisconnect();
         MessageBox.Show("Other user was disconnected.");
     }
 }