Beispiel #1
0
 // Invoke the ResponseReceived event
 protected void OnResponseReceived(ResponseReceivedEventArgs e)
 {
     if (ResponseReceived != null)
     {
         ResponseReceived(this, e);
     }
 }
Beispiel #2
0
        // Called when a SendAsync operation completes
        private void ProcessSend(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                //Read data sent from the server
                Socket sock = e.UserToken as Socket;

                sock.ReceiveAsync(e);
            }
            else
            {
                ResponseReceivedEventArgs args = new ResponseReceivedEventArgs();
                args.response = e.SocketError.ToString();
                args.isError  = true;
                OnResponseReceived(args);
            }
        }
Beispiel #3
0
 // Called when a ConnectAsync operation completes
 private void ProcessConnect(SocketAsyncEventArgs e)
 {
     if (e.SocketError == SocketError.Success)
     {
         // Successfully connected to the server
         // Send data to the server
         //byte[] buffer = Encoding.UTF8.GetBytes(dataIn + "<EOF>");
         byte[] buffer = Encoding.UTF8.GetBytes(dataIn);
         e.SetBuffer(buffer, 0, buffer.Length);
         Socket sock = e.UserToken as Socket;
         sock.SendAsync(e);
     }
     else
     {
         ResponseReceivedEventArgs args = new ResponseReceivedEventArgs();
         args.response = e.SocketError.ToString();
         args.isError  = true;
         OnResponseReceived(args);
     }
 }
Beispiel #4
0
        // Called when a ReceiveAsync operation completes
        private void ProcessReceive(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                // Received data from server
                string dataFromServer = Encoding.UTF8.GetString(e.Buffer, 0, e.BytesTransferred);

                Socket sock = e.UserToken as Socket;
                sock.Shutdown(SocketShutdown.Send);
                sock.Close();

                // Respond to the client in the UI thread to tell him that data was received
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    ResponseReceivedEventArgs args = new ResponseReceivedEventArgs();
                    args.response = dataFromServer;
                    OnResponseReceived(args);
                });
            }
            else
            {
                throw new SocketException((int)e.SocketError);
            }
        }
Beispiel #5
0
        // Called when a SendAsync operation completes
        private void ProcessSend(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                //Read data sent from the server
                Socket sock = e.UserToken as Socket;

                sock.ReceiveAsync(e);
            }
            else
            {
                ResponseReceivedEventArgs args = new ResponseReceivedEventArgs();
                args.response = e.SocketError.ToString();
                args.isError = true;
                OnResponseReceived(args);
            }
        }
Beispiel #6
0
        // Called when a ReceiveAsync operation completes
        private void ProcessReceive(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                // Received data from server
                string dataFromServer = Encoding.UTF8.GetString(e.Buffer, 0, e.BytesTransferred);

                Socket sock = e.UserToken as Socket;
                sock.Shutdown(SocketShutdown.Send);
                sock.Close();

                // Respond to the client in the UI thread to tell him that data was received
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    ResponseReceivedEventArgs args = new ResponseReceivedEventArgs();
                    args.response = dataFromServer;
                    OnResponseReceived(args);
                });

            }
            else
            {
                throw new SocketException((int)e.SocketError);
            }
        }
Beispiel #7
0
        // Called when a ConnectAsync operation completes
        private void ProcessConnect(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                // Successfully connected to the server
                // Send data to the server
                //byte[] buffer = Encoding.UTF8.GetBytes(dataIn + "<EOF>");
                byte[] buffer = Encoding.UTF8.GetBytes(dataIn);
                e.SetBuffer(buffer, 0, buffer.Length);
                Socket sock = e.UserToken as Socket;
                sock.SendAsync(e);
            }
            else
            {
                ResponseReceivedEventArgs args = new ResponseReceivedEventArgs();
                args.response = e.SocketError.ToString();
                args.isError = true;
                OnResponseReceived(args);

            }
        }
Beispiel #8
0
 // Invoke the ResponseReceived event
 protected void OnResponseReceived(ResponseReceivedEventArgs e)
 {
     if (ResponseReceived != null)
         ResponseReceived(this, e);
 }