0

I have the following code to receive the response from a Server.

 byte[] responseBuffer=new byte[1];
Socket destServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    destServerSocket.Connect(remoteHost, portno);

                    //State 2: Sending New Request Information to Destination Server and Relay Response to Client
                    destServerSocket.Send(ASCIIEncoding.ASCII.GetBytes(requestPayload));

                    Console.WriteLine("Sent the Request to Server...");
                    Console.WriteLine("Begin Receiving Response...");
                    String Response = "";
                    while (destServerSocket.Receive(responseBuffer) != 0)
                    {
                        Console.Write(ASCIIEncoding.ASCII.GetString(responseBuffer));
                        Response += ASCIIEncoding.ASCII.GetString(responseBuffer);
                        if (this.clientSocket.Connected)
                            this.clientSocket.Send(responseBuffer);
                        else
                        {
                            Console.WriteLine("Socket Disconnected");
                            break;
                        }
                    }
                    Console.WriteLine("Received the Response as {0}", Response);
                    destServerSocket.Disconnect(false);
                    destServerSocket.Dispose();
                    this.clientSocket.Disconnect(false);
                    this.clientSocket.Dispose();

I tried to debug and The Visual Studio just stops and doesn't exit Debugging mode but no clue of where the execution went. Could anyone help me with this weird behavior of C#. I have attached herewith the Output Screen image for your reference.

enter image description here

Thanks,

  • 1
    Just trying to understand the code here. Does this code act as a proxy, reading the response from the destination server and writing it back to a client? – adv12 May 03 '14 at 01:30
  • There is debug-> break all command... And debug->windows->threads + debug->windows->call stack views... – Alexei Levenkov May 03 '14 at 01:31
  • I don't understand this code. You connect to server with 'destServerSocket' then reply with 'this.clientSocket'? What is your app supposed to do? – Martin James May 03 '14 at 01:31
  • @adv12 - quite. I don't understand it either. – Martin James May 03 '14 at 01:32
  • @adv12 Absolutely, this is a proxy code... Alexei Levenkov..could you elaborate? I didn't get what you are saying..And this is not complete Code... Its just a part of the complete code where I have a problem. Do you want me to post the complete code here? – AnanthaPadmanabhan May 03 '14 at 01:33

1 Answers1

1

From the documentation:

If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout.

So, your loop has received all that the server is going to send, but it's blocking waiting for more.

Here's what you'll need to do to properly identify the end of the HTTP response:

Detect end of HTTP request body

(That is, assuming your proxy is specifically an HTTP proxy...)

Community
  • 1
  • 1
adv12
  • 8,443
  • 2
  • 24
  • 48
  • Thanks adv12...My Proxy is a Generic and Basic Proxy where it receives any kind of Data ad relays to client...Your suggestion helped...I also read the receive method documentation but somehow I missed the point..Thanks for pointing it...I am using Socket.Available to check for availability of data and then reading it..now it executes next statements. – AnanthaPadmanabhan May 03 '14 at 02:09
  • I hope you don't mean you're using (Available == 0) to detect the end of data transmission. Available == 0 just means that there's no data available *right now* (possibly because not enough time has passed for more data to come over the network since your last read). You don't want to assume you've received the entire message just because of a lull in data... – adv12 May 03 '14 at 02:14
  • Yeah..I am using Socket.Available!=0 condition and then in that loop I am reading into my Byte Array...Am I doing it right? Here is a small snippet to show what I am doing...while (FromServerSocket.Available != 0) { FromServerSocket.Receive(b); data += ASCIIEncoding.ASCII.GetString(b); } – AnanthaPadmanabhan May 03 '14 at 02:26
  • As I said in my last comment, no, you don't want to interpret Available == 0 as the end of a message. Each protocol will have its own way of identifying message length. So you either need to write specific code for each protocol you want to support or give up on the business of waiting for the "end" and using an infinite loop. You could just say "while(true)" and maybe have a cancel flag that can be set from another thread. But threading is another whole topic. – adv12 May 03 '14 at 02:32
  • ohh I got you now...I will look into that aspect of my messages and see what can be done.. Thanks a lot for your guidance.. – AnanthaPadmanabhan May 03 '14 at 02:43