webrequest / socket problem

S

sven22

Hi I'm programming a local proxy which should at first only be able to
pass the request to the webserver and to return the response to the
browser. But i t doesn't work!

I get problems:
1) webrequest & response
when I read in the result into a string and send the encoding in UTF8
or ASCII back to the browser (somehow images are not displayed
because some info gets lost) -using streamreader

when I read the result from the response by using bytes[] b=new
byte[1024]
stream.read(b,0, b.Length)
-> gets me only half of the page - rest is garbage - no images
displayed
that works only if I set b to 1 - which is painfully slow but
works...

2)sockets

proggie hangs at the recieve line send is ok, I get connected but I
don't recieve anything see code below


...

Uri myUri = new Uri(URL);
IPHostEntry IPHost = Dns.Resolve(myUri.Host);

Console.WriteLine("Request resolved:
{0}", IPHost.HostName);
string [] aliases = IPHost.Aliases;
IPAddress[] address = IPHost.AddressList;
Console.WriteLine(address[0]);
IPEndPoint sEndpoint = new IPEndPoint(address[0],
80);

Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
server.Connect(sEndpoint);

if(server.Connected)
Console.WriteLine("Socket connect OK");

int i;
if ((i = server.SendTo(cmsg, sEndpoint)) ==
-1)
System.Console.WriteLine("Send Error!");

byte[] RecvBytes = new byte[1024];
//HANGS HERE ON RECIEVE!
if ((i = server.Receive(RecvBytes)) ==
-1)
System.Console.WriteLine("Recieve Error!");

...

Well if someone has an idea this would be really great - this trouble
me since some days and I would be happy for help.
 
J

Joerg Jooss

sven22 said:
Hi I'm programming a local proxy which should at first only be able to
pass the request to the webserver and to return the response to the
browser. But i t doesn't work!

I get problems:
1) webrequest & response
when I read in the result into a string and send the encoding in UTF8
or ASCII back to the browser (somehow images are not displayed
because some info gets lost) -using streamreader

You cannot use Readers for general purpose data transfer. Readers translate
bytes to characters, which is hardly desirable for any content other than
text.
when I read the result from the response by using bytes[] b=new
byte[1024]
stream.read(b,0, b.Length)
-> gets me only half of the page - rest is garbage - no images
displayed
that works only if I set b to 1 - which is painfully slow but
works...

Let me guess:

Let source and target be the input and output stream. Does your code look
something like that?

byte[] buffer = new byte[0x1000];
int bytes;
while ((bytes = source.Read(buffer, 0, buffer.Length)) > 0) {
// Ouch...
target.Write(buffer, 0, buffer.Length);
}

Well, that's a trivial programming error. You shouldn't write buffer.Length
bytes. You should write only as many bytes as the previous Read() called
returned:

target.Write(buffer, 0, bytes);

That should do it... I'll leave the socket part to somebody else ;-)

Cheers,
 
V

Vadim Chekan

sven22 said:
when I read the result from the response by using bytes[] b=new
byte[1024]
stream.read(b,0, b.Length)
-> gets me only half of the page - rest is garbage - no images
displayed
that works only if I set b to 1 - which is painfully slow but
works...

Do you utilize only received amount of data? Read() returns integer that
shows how much data has been actually read. All behind this limit in
buffer is garbage.
2)sockets

Is this a http proxy? Did you try to debug it by issing the same
commands manually, using telnet? Didn't you forget "\r\n\r\n" at the end
of your sent header?

Vadim Chekan.
 
S

sven22

Well, that's a trivial programming error. You shouldn't write
buffer.Length
bytes. You should write only as many bytes as the previous Read() called
returned:

target.Write(buffer, 0, bytes);
->doesn't work so far. Works great on text only but all my images
are broken - here my code

System.Net.WebRequest Request =
System.Net.WebRequest.Create(URL);

System.Net.WebResponse Response = Request.GetResponse();
int buffSize;

byte [] buffer = new byte[0x1000];

while((buffSize=Response.GetResponseStream().Read(buffer,0,buffer.Length))
0) {
browserStream.Write(buffer,0,buffSize);
}


Is this a http proxy? Did you try to debug it by issing the same
commands manually, using telnet? Didn't you forget "\r\n\r\n" at the end
of your sent header?

I use the browser request directly read in 256 Bytes and forward the
request to the "send" part of my code. Which means what I sent out
is what I get from the browser:

[code:1:fe28cdd20d]
Byte[] cmsg = new Byte[256];
NetworkStream stream = new NetworkStream(socket);
stream.Read(cmsg,0,cmsg.Length);

....

if ((i = server.SendTo(cmsg, sEndpoint)) ==
-1)
System.Console.WriteLine("Send Error!");

byte[] RecvBytes = new byte[1024];

if ((i = server.Receive(RecvBytes)) ==
-1)
System.Console.WriteLine("Recieve Error!");

[/code:1:fe28cdd20d]

Thanks for helping :D
 
J

Joerg Jooss

sven22 said:
Well, that's a trivial programming error. You shouldn't write
buffer.Length bytes. You should write only as many bytes as the
previous Read() called returned:

target.Write(buffer, 0, bytes);
->doesn't work so far. Works great on text only but all my images
are broken - here my code

System.Net.WebRequest Request =
System.Net.WebRequest.Create(URL);

System.Net.WebResponse Response = Request.GetResponse();
int buffSize;

byte [] buffer = new byte[0x1000];

while((buffSize=Response.GetResponseStream().Read(buffer,0,buffer.Length))
{
browserStream.Write(buffer,0,buffSize);
}

Read code looks fine -- but what about "browserStream"? Do you set the
appropriate Content-Type header? And what do you mean by "broken" exactly?

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top