Click here to Skip to main content
15,875,017 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I am writing tcp proxy server and i have trouble. After i caught request from client, i use such code:

C#
var remoteStream = myRerouting.GetStream();
using (var clientStream = myClient.GetStream())
{
    remoteStream.Write(httpRequest, 0, httpRequest.Length);
    ReadSend(myRerouting.Client, clientStream);
}


ReadSend method code:
C#
internal static void ReadSend(Socket mySocket, NetworkStream stream)
{
    byte[] b = new byte[mySocket.ReceiveBufferSize];
    int len = 0;
    while (mySocket.Poll(1000000, SelectMode.SelectRead) && (len = mySocket.Receive(b, mySocket.ReceiveBufferSize, SocketFlags.None)) > 0)
    {
        stream.Write(b, 0, len);
    }
}


And all is fine.

But when i want to process response(make some optimizations) and only then send response to client, something is going wrong and client don't receive an answer(i see something like "you tried to access the address which is currently unavailable"), though if i debug the code, i.e. move step by step, all is fine as earlier, buffer is filled properly and response is sent to client properly. Here is modified code:

C#
var remoteStream = myRerouting.GetStream();
using (var clientStream = myClient.GetStream())
{
    remoteStream.Write(httpRequest, 0, httpRequest.Length);
    var buffer = ReadToEnd(myRerouting.Client);
    clientStream.Write(buffer, 0, buffer.Length);
}


ReadToEnd method code:
C#
private static byte[] ReadToEnd(Socket mySocket)
{
    byte[] b = new byte[mySocket.ReceiveBufferSize];
    int len = 0;
    using (MemoryStream m = new MemoryStream())
    {
        while (mySocket.Poll(1000000, SelectMode.SelectRead) &&
               (len = mySocket.Receive(b, mySocket.ReceiveBufferSize, SocketFlags.None)) > 0)
        {
            m.Write(b, 0, len);
        }
        return m.ToArray();
    }
}

This is the only change. I don't understand why this is happening.
Posted
Comments
Vedat Ozan Oner 10-Feb-14 14:53pm    
I am not sure, but can you try to send the received data by splitting it into smaller packets where each packet has the size of socket.receivebuffersize? I mean:
instead of 'client.write(all_data)'
you can send it like
while(idx < all_size) {
client.write(buff, idx, idx+socket.recbuffsize);
idx+=socket.recbuffsize; }

I hope it is clear what I tried to say.
[no name] 10-Feb-14 15:23pm    
It works! Thank you very much. It have already become a headache for me.
Vedat Ozan Oner 10-Feb-14 15:33pm    
you are welcome :)
[no name] 11-Feb-14 12:30pm    
And..maybe you could suggest a better way to read http response from remote resource? I mean, on this level, i.e. without httprequest/webrequest classes.
Vedat Ozan Oner 11-Feb-14 13:53pm    
I think that is good. do you have a problem with those streams?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900