示例#1
0
        private void BeginReceive(IAsyncResult ar)
        {
            Socket client = (Socket)ar.AsyncState;

            if (this._ID < 0)
            {
                return;
            }

            try
            {
                int nBytesRec = client.EndReceive(ar);
                if (nBytesRec > 0)
                {
                    string sReceived = Encoding.ASCII.GetString(this._Buffer, 0, nBytesRec);
                    this._Data += sReceived;

                    while (this._Data.IndexOf("\n") > 0)
                    {
                        if (BeginRead != null)
                        {
                            BeginRead(this, new TCPEventArgs(this._Data.Substring(0, this._Data.IndexOf("\n")), this._ID, client.RemoteEndPoint));
                        }
                        this._Data = this._Data.Substring(this._Data.IndexOf("\n") + 1);
                    }

                    while (this._Data.Length > this._BufferLength)
                    {
                        if (BeginRead != null)
                        {
                            BeginRead(this, new TCPEventArgs(this._Data.Substring(0, this._BufferLength), this._ID, client.RemoteEndPoint));
                        }
                        this._Data = this._Data.Substring(this._BufferLength + 1);
                    }

                    if (client.Connected)
                    {
                        client.BeginReceive(this._Buffer, 0, this._Buffer.Length, SocketFlags.None, new AsyncCallback(BeginReceive), client);
                    }
                }
                else
                {
                    DisconnectSocket();
                }
            }
            catch (Exception ex)
            {
                SocketHelper.ShowError(this, ex);
            }
        }
示例#2
0
 public void Write(string value)
 {
     try
     {
         if ((this._ClientSocket != null) && (this._ClientSocket.Connected))
         {
             Byte[] byte_data = Encoding.ASCII.GetBytes(value.ToCharArray());
             this._ClientSocket.Send(byte_data, byte_data.Length, 0);
         }
     }
     catch (Exception ex)
     {
         SocketHelper.ShowError(this, ex);
     }
 }
示例#3
0
        /// <summary>
        /// Connect Event
        /// </summary>
        /// <param name="ar"></param>
        private void BeginAccept(IAsyncResult ar)
        {
            Socket listener = (Socket)ar.AsyncState;
            Socket client;

            if ((listener != null) && (listener.Handle.ToInt32() != -1))
            {
                try
                {
                    for (int i = 0; i < this.MaxConnections; i++)
                    {
                        if (this.TCPClients[i] == null)
                        {
                            ++this.ConnectionCount;
                            this.TCPClients[i]                  = new TCPClient(listener.EndAccept(ar), i);
                            this.TCPClients[i].BeginRead       += new TCPClient.ReadEventHandler(this.ReadSocket);
                            this.TCPClients[i].BeginDisconnect += new TCPClient.DisconnectEventHandler(this.DisconnectSocket);

                            if (BeginConnect != null)
                            {
                                BeginConnect(this.TCPClients[i], new TCPEventArgs(null, i, this.TCPClients[i].RemoteEndPoint));
                            }
                            break;
                        }
                        else if (i == this.MaxConnections - 1)
                        {
                            client = listener.EndAccept(ar);

                            Console.WriteLine("Socket connection from IP: {0} was rejected. Maximum Telnet connections reached", client.RemoteEndPoint);

                            client.Send(System.Text.Encoding.ASCII.GetBytes("Maximum Telnet connections reached"));

                            client.Shutdown(SocketShutdown.Both);
                            System.Threading.Thread.Sleep(10);
                            client.Close();
                            //Disconnect(client);
                        }
                    }

                    listener.BeginAccept(new AsyncCallback(BeginAccept), listener);
                }

                catch (Exception ex)
                {
                    SocketHelper.ShowError(this, ex);
                }
            }
        }
示例#4
0
 /// <summary>
 /// Write data to a socket
 /// </summary>
 /// <param name="data">data</param>
 public void Write(int socketid, string data)
 {
     try
     {
         if ((socketid < this.MaxConnections) &&
             (this.TCPClients[socketid] != null) &&
             (this.TCPClients[socketid].Connected))
         {
             this.TCPClients[socketid].Write(data);
         }
     }
     catch (Exception ex)
     {
         SocketHelper.ShowError(this, ex);
     }
 }
示例#5
0
 /// <summary>
 /// Eplicit Socket disconnect
 /// </summary>
 /// <param name="socket"></param>
 public void Disconnect(Socket socket)
 {
     try
     {
         if (socket.Connected)
         {
             socket.Shutdown(SocketShutdown.Both);
             System.Threading.Thread.Sleep(10);
             socket.Close();
         }
     }
     catch (Exception ex)
     {
         SocketHelper.ShowError(this, ex);
     }
 }
示例#6
0
 /// <summary>
 /// Disconnect client
 /// </summary>
 /// <param name="socketid">SocketID of the client connection</param>
 public void Disconnect(int socketid)
 {
     try
     {
         if ((socketid < this.MaxConnections) &&
             (this.TCPClients[socketid] != null) &&
             (this.TCPClients[socketid].Connected))
         {
             this.TCPClients[socketid].DisconnectSocket();
         }
     }
     catch (Exception ex)
     {
         SocketHelper.ShowError(this, ex);
     }
 }
示例#7
0
 /// <summary>
 /// Disconnect all clients and stop listener
 /// </summary>
 public void Stop()
 {
     try
     {
         if (listener != null)
         {
             Disconnect();
             listener.Close();
             listener = null;
         }
     }
     catch (Exception ex)
     {
         SocketHelper.ShowError(this, ex);
     }
 }
示例#8
0
        /// <summary>
        /// Start TCP listener
        /// </summary>
        public void Start()
        {
            if (listener != null)
            {
                Console.WriteLine("Listener exists. Stop it first.");
                return;
            }

            try
            {
                TCPClients = new TCPClient[this.MaxConnections];
                listener   = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                listener.Bind(new IPEndPoint(this.IPAddress, this.Port));
                listener.Listen(this.MaxConnections);

                listener.BeginAccept(new AsyncCallback(BeginAccept), listener);
            }
            catch (Exception ex)
            {
                SocketHelper.ShowError(this, ex);
            }
        }