socks5.TCP.Client.Receive C# (CSharp) Method

Receive() public method

public Receive ( byte data, int offset, int count ) : int
data byte
offset int
count int
return int
        public int Receive(byte[] data, int offset, int count)
        {
            try
            {
                int received = this.Sock.Receive(data, offset, count, SocketFlags.None);
                if (received <= 0)
                {
                    this.Disconnect();
                    return -1;
                }
                DataEventArgs dargs = new DataEventArgs(this, data, received);
                //this.onDataReceived(this, dargs);
                return received;
            }
            catch (Exception ex)
            {
                #if DEBUG
  Console.WriteLine(ex.ToString()); 
#endif 
                this.Disconnect();
                return -1;
            }
        }

Usage Example

示例#1
0
文件: Socks.cs 项目: hymersm/Socks5
        public static SocksEncryption RequestSpecialMode(List<AuthTypes> auth, Client client)
        {
            //select mode, do key exchange if encryption, or start compression.
            if (auth.Contains(AuthTypes.SocksBoth))
            {
                //tell client that we chose socksboth.
                client.Send(new byte[] { (byte)HeaderTypes.Socks5, (byte)AuthTypes.SocksBoth });
                //wait for public key.
                SocksEncryption ph = new SocksEncryption();
                ph.GenerateKeys();
                //wait for public key.
                byte[] buffer = new byte[4096];
                int keysize = client.Receive(buffer, 0, buffer.Length);
                //store key in our encryption class.
                ph.SetKey(buffer, 0, keysize);
                //send key.
                client.Send(ph.GetPublicKey());
                //now we give them our key.
                client.Send(ph.ShareEncryptionKey());
                //send more.
                int enckeysize = client.Receive(buffer, 0, buffer.Length);
                //decrypt with our public key.
                byte[] newkey = new byte[enckeysize];
                Buffer.BlockCopy(buffer, 0, newkey, 0, enckeysize);
                ph.SetEncKey(ph.key.Decrypt(newkey, false));

                ph.SetType(AuthTypes.SocksBoth);
                //ready up our client.
                return ph;
            }
            else if (auth.Contains(AuthTypes.SocksEncrypt))
            {
                //tell client that we chose socksboth.
                client.Send(new byte[] { (byte)HeaderTypes.Socks5, (byte)AuthTypes.SocksEncrypt });
                //wait for public key.
                SocksEncryption ph = new SocksEncryption();
                ph.GenerateKeys();
                //wait for public key.
                byte[] buffer = new byte[4096];
                int keysize = client.Receive(buffer, 0, buffer.Length);
                //store key in our encryption class.
                ph.SetKey(buffer, 0, keysize);
                ph.SetType(AuthTypes.SocksBoth);
                //ready up our client.
                return ph;
            }
            else if (auth.Contains(AuthTypes.SocksCompress))
            {
                //start compression.
                client.Send(new byte[] { (byte)HeaderTypes.Socks5, (byte)AuthTypes.SocksCompress });
                SocksEncryption ph = new SocksEncryption();
                ph.SetType(AuthTypes.SocksCompress);
                //ready
            }
            else if (auth.Contains(AuthTypes.Login))
            {
                SocksEncryption ph = new SocksEncryption();
                ph.SetType(AuthTypes.Login);
                return ph;
            }
            return null;
        }
All Usage Examples Of socks5.TCP.Client::Receive