Exemplo n.º 1
0
        /*
         * function called by the begin receive async callback
         * ar is the socket wrapper
         */
        public static void startReceive(IAsyncResult ar)
        {
            var socketWrap = (SocketWrapper)ar.AsyncState;
            var socket     = socketWrap.sock;


            //Console.WriteLine("yea");

            try
            {
                var requestFromStart = socket.EndReceive(ar);

                //we unwrap the respons from the bytes recevied and append them to the content container
                socketWrap.responseContent.Append(Encoding.ASCII.GetString(socketWrap.buffer, 0, requestFromStart));
                //Console.WriteLine(Encoding.ASCII.GetString(socketWrap.buffer, 0, requestFromStart));

                //we try to get the respone header
                if (!HTTPParser.gotResponseHeader(socketWrap.responseContent.ToString()))
                {
                    socket.BeginReceive(socketWrap.buffer, 0, 512, 0, startReceive, socketWrap);
                    //Console.WriteLine("yea");
                }


                //Console.WriteLine("yea");
                var responseBody  = HTTPParser.getResponseBody(socketWrap.responseContent.ToString());
                var contentLenght = HTTPParser.getContentLen(socketWrap.responseContent.ToString());


                //also we keep receiving if we didn't finish reading
                if (responseBody.Length < contentLenght)
                {
                    socket.BeginReceive(socketWrap.buffer, 0, 512, 0, startReceive, socketWrap);
                }
                //we print everything since there is nothing more to receive
                else
                {
                    foreach (var i in socketWrap.responseContent.ToString().Split('\r', '\n'))
                    {
                        System.Console.WriteLine(i);
                    }

                    System.Console.WriteLine("length is " + contentLenght);

                    socket.Shutdown(SocketShutdown.Both);
                    socket.Close();
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.ToString());
            }
        }
Exemplo n.º 2
0
        public static async void start(string link, int id)
        {
            var linkDns        = Dns.GetHostEntry(link.Split('/')[0]);
            var ipAddrs        = linkDns.AddressList[0];
            var remoteEndPoint = new IPEndPoint(ipAddrs, HTTPParser.PORT);

            var socket = new Socket(ipAddrs.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            //if there is an endpoint we set it
            var endpoint = String.Empty;

            if (link.Contains("/"))
            {
                endpoint = link.Substring(link.IndexOf("/"));
            }
            else
            {
                endpoint = "/";
            }

            //create the socket wrapper since we needed more info than just the socket
            var socketWrap = new SocketWrapper()
            {
                sock           = socket,
                hostname       = link.Split('/')[0],
                endpoint       = endpoint,
                remoteEndPoint = remoteEndPoint,
                id             = id
            };

            //we start each task and we make them wait until they are signaled
            await connect(socketWrap);

            await send(socketWrap, HTTPParser.getRequest(socketWrap.hostname, socketWrap.endpoint));

            await receive(socketWrap);

            //after response from the server is received we print it
            Console.WriteLine(
                "async: response {0} got : contentLenght = {1}",
                id, HTTPParser.getContentLen(socketWrap.responseContent.ToString()));

            //close the socket
            socket.Shutdown(SocketShutdown.Both);
            socket.Close();
        }
Exemplo n.º 3
0
        /*
         * function called by the begin connect async callback
         * ar is the socket wrapper
         */
        public static void startConnect(IAsyncResult ar)
        {
            //we deserialize the wrapper and get the info
            var socketWrap = (SocketWrapper)ar.AsyncState;
            var socket     = socketWrap.sock;
            var id         = socketWrap.id;
            var hostname   = socketWrap.hostname;

            socket.EndConnect(ar);

            System.Console.WriteLine("Socket {0} connected to {1} (ip:{2})", id, hostname, socket.RemoteEndPoint);

            //get the request that we want to make as a string and
            //convert it to bytes so we can send it with BeginSend

            var requestToBytes = Encoding.ASCII.GetBytes(HTTPParser.getRequest(hostname, socketWrap.endpoint));

            //start sending the request as bytes
            socket.BeginSend(requestToBytes, 0, requestToBytes.Length, 0, startSend, socketWrap);
        }
Exemplo n.º 4
0
        private static void receiveCallback(IAsyncResult ar)
        {
            var socketWrap = (SocketWrapper)ar.AsyncState;
            var socket     = socketWrap.sock;

            try
            {
                var requestFromStart = socket.EndReceive(ar);

                //we unwrap the response from the bytes received and append them to the content container
                socketWrap.responseContent.Append(Encoding.ASCII.GetString(socketWrap.buffer, 0, requestFromStart));
                //Console.WriteLine(Encoding.ASCII.GetString(socketWrap.buffer, 0, requestFromStart));

                //we re-try if we didn't get a response
                if (!HTTPParser.gotResponseHeader(socketWrap.responseContent.ToString()))
                {
                    socket.BeginReceive(socketWrap.buffer, 0, 512, 0, receiveCallback, socketWrap);
                }


                var responseBody  = HTTPParser.getResponseBody(socketWrap.responseContent.ToString());
                var contentLenght = HTTPParser.getContentLen(socketWrap.responseContent.ToString());


                //also we keep receiving if we didn't finish reading
                if (responseBody.Length < contentLenght)
                {
                    socket.BeginReceive(socketWrap.buffer, 0, 512, 0, receiveCallback, socketWrap);
                }
                //we print everything since there is nothing more to receive
                else
                {
                    socketWrap.receiveFinished.Set();
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.ToString());
            }
        }