Ejemplo n.º 1
0
        private static async Task <Socket> ConnectHttpAsync(string host, int port)
        {
            //TODO: need testing
            Socket socket = null;

            try
            {
                socket = await SocketHelper.ConnectAsync(host, port);

                if (socket == null)
                {
                    return(null);
                }

                await Task.Run(() => { socket.Send(Encoding.UTF8.GetBytes("CONNECT " + host + ":" + port + " HTTP/1.0\r\n\r\n")); });

                byte[] recvBuffer = new byte[39];
                string respond = string.Empty;
                int    received, tbytes = 0;

                await Task.Run(() =>
                {
                    do
                    {
                        received = 0;
                        received = socket.Receive(recvBuffer);
                        if (received == 0)
                        {
                            break;
                        }

                        tbytes  += received;
                        respond += Encoding.ASCII.GetString(recvBuffer, 0, recvBuffer.Length);
                        //if (respond.Contains("\r\n\r\n")) break;
                    }while ((tbytes > 0) && (tbytes < 39));
                });

                if (respond.Contains("200"))
                {
                    return(socket);
                }
            }
            catch (Exception exp) { ExceptionHelper.Log(exp); }

            if (socket != null)
            {
                socket.Close();
            }

            return(null);
        }
Ejemplo n.º 2
0
        protected async Task <Socket> createStreamAsync(string host, int port)
        {
            if (IsCanceled)
            {
                return(null);
            }
            Socket s = null;

            if ((ProxyClient.Proxy != null))
            {
                s = await ProxyClient.ConnectAsync(host, port);

                if (s != null)
                {
                    InfoMessage("Proxy connection established.", MessageType.Info);
                }
                else
                {
                    InfoMessage("unable to Connect to the proxy", MessageType.Error);
                }
            }
            else
            {
                Socket socket = await SocketHelper.ConnectAsync(host, port);

                if (socket != null)
                {
                    s = socket;
                    if (!isIPV6.HasValue)
                    {
                        isIPV6 = SocketHelper.IsIPV6();
                    }
                }
            }

            return(s);
        }