Beispiel #1
0
        void ss_DataReceived(object sender, AsyncSocketUserTokenEventArgs e)
        {
            this.Invoke((MethodInvoker) delegate
            {
                if (richTextBox1.TextLength > 536870912)
                {
                    richTextBox1.Clear();
                }
            });

            string data = e.ReceivedRawData.ToHexString();

            this.Invoke((MethodInvoker) delegate
            {
                try
                {
                    richTextBox1.AppendText(string.Format("----ClientId: {0}, ip: {1}, port: {2}, received: {3}", e.ConnectionId.ToString(), e.EndPoint.Address.ToString(), e.EndPoint.Port.ToString(), data));
                    richTextBox1.AppendText(Environment.NewLine);
                    richTextBox1.ScrollToCaret();
                }
                catch
                {
                }
            });

            ss.Send(e.ConnectionId, e.ReceivedRawData);
        }
Beispiel #2
0
 void client_DataSent(object sender, AsyncSocketUserTokenEventArgs e)
 {
     this.Invoke((MethodInvoker) delegate
     {
         richTextBox1.AppendText("client_DataSent" + e.ConnectionId.ToString() + string.Format(e.ReceivedRawData.ToHexString()));
         richTextBox1.AppendText(Environment.NewLine);
         richTextBox1.ScrollToCaret();
     });
 }
Beispiel #3
0
 void ss_Disconnected(object sender, AsyncSocketUserTokenEventArgs e)
 {
     this.Invoke((MethodInvoker) delegate
     {
         try
         {
             richTextBox1.AppendText(string.Format("->Left clients: {0}, Disconnected ClientId: {1}, ip: {2}, port: {3}", ss.NumConnectedSockets.ToString(), e.ConnectionId.ToString(), e.EndPoint.Address.ToString(), e.EndPoint.Port));
             richTextBox1.AppendText(Environment.NewLine);
             richTextBox1.ScrollToCaret();
         }
         catch
         {
         }
     });
 }
Beispiel #4
0
        void ss_Connected(object sender, AsyncSocketUserTokenEventArgs e)
        {
            this.Invoke((MethodInvoker) delegate
            {
                try
                {
                    richTextBox1.AppendText(string.Format("-> Total client: {3}, Connected ClientId: {0}, ip: {1}, port: {2}", e.ConnectionId.ToString(), e.EndPoint.Address.ToString(), e.EndPoint.Port.ToString(), ss.NumConnectedSockets));
                    richTextBox1.AppendText(Environment.NewLine);
                    richTextBox1.ScrollToCaret();
                }
                catch
                {
                }

                if (ss.NumConnectedSockets >= this.maxClient)
                {
                    this.maxClient = ss.NumConnectedSockets;
                }

                label4.Text = this.maxClient.ToString();
            });
        }
        /// <summary>
        /// Connect to remote endpoint.
        /// </summary>
        /// <param name="remoteEndPoint">Remote IPEndPoint.</param>
        /// <param name="useIOCP">Specifies whether the socket should only use Overlapped I/O mode.</param>
        public void Connect(IPEndPoint remoteEndPoint, bool useIOCP = true)
        {
            this.CheckDisposed();

            this._clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                this._token = new AsyncSocketUserTokenEventArgs(this._clientSocket);
                this._token.ConnectionId = Guid.NewGuid();
                this._token.SetBuffer(this._token.ReadEventArgs.Buffer, this._token.ReadEventArgs.Offset);

                this._clientSocket.UseOnlyOverlappedIO = useIOCP;
                this._clientSocket.BeginConnect(remoteEndPoint, new AsyncCallback(this.ProcessConnect), this._clientSocket);

                Debug.WriteLine(AsyncSocketClientConstants.ClientConnectSuccessfully);
            }
            catch (ObjectDisposedException e)
            {
                Debug.WriteLine(string.Format(AsyncSocketClientConstants.ExceptionStringFormat, "DevLib.Net.AsyncSocket.AsyncSocketClient.Connect", e.Source, e.Message, e.StackTrace, e.ToString()));
                this.RaiseEvent(this.Disconnected, new AsyncSocketUserTokenEventArgs(this._clientSocket));
            }
            catch (SocketException e)
            {
                Debug.WriteLine(string.Format(AsyncSocketClientConstants.ExceptionStringFormat, "DevLib.Net.AsyncSocket.AsyncSocketClient.Connect", e.Source, e.Message, e.StackTrace, e.ToString()));
                if (e.ErrorCode == (int)SocketError.ConnectionReset)
                {
                    this.RaiseEvent(this.Disconnected, this._token);
                }

                this.OnErrorOccurred(this._token.Socket, new AsyncSocketErrorEventArgs(e.Message, e, AsyncSocketErrorCodeEnum.ClientConnectException));
            }
            catch (Exception e)
            {
                Debug.WriteLine(string.Format(AsyncSocketClientConstants.ExceptionStringFormat, "DevLib.Net.AsyncSocket.AsyncSocketClient.Connect", e.Source, e.Message, e.StackTrace, e.ToString()));
                this.RaiseEvent(this.Disconnected, this._token);
                this.OnErrorOccurred(this._token.Socket, new AsyncSocketErrorEventArgs(e.Message, e, AsyncSocketErrorCodeEnum.ClientConnectException));
            }
        }
        /// <summary>
        /// Method RaiseEvent.
        /// </summary>
        /// <param name="eventHandler">Instance of EventHandler.</param>
        /// <param name="eventArgs">Instance of AsyncSocketUserTokenEventArgs.</param>
        private void RaiseEvent(EventHandler <AsyncSocketUserTokenEventArgs> eventHandler, AsyncSocketUserTokenEventArgs eventArgs)
        {
            // Copy a reference to the delegate field now into a temporary field for thread safety
            EventHandler <AsyncSocketUserTokenEventArgs> temp = Interlocked.CompareExchange(ref eventHandler, null, null);

            if (temp != null)
            {
                temp(this, eventArgs);
            }
        }
Beispiel #7
0
 void ss_DataSent(object sender, AsyncSocketUserTokenEventArgs e)
 {
     //throw new NotImplementedException();
 }