Hi all,
i am trying to create a small app which can record the requests from
and responses to my browser.
To achieve this i am using two tcpclients and a listener.
The first 1 is listening to a port on my localhost to intercept the
browsers request. The second 1 uses the stream of tcpclient1 and
forwards it to the target.
The response-stream from the target is read from tcpclient2 and
written to tcpclient1.
Seems workable. And when i step through the code using <shift><f8>, it
works fine indeed.
However, when i don't use breakpoints, the requests and responses seem
somewhat confused. The rendering in my browser is not what one would
expect :-).
I tried application.doevents, setting the sendtimeout on the
tcpclient, but both offers no solution.
2nd strange thing is that i can only go to one destination. After
that, the tcp-listener seems to ignore most requests.
I'll post my code (which of course is only for testing purposes and
doesnt deserve a price for efficient coding (yet)) below.
Thanks for your remarks.
grtz,
mark
<code>
Dim ipAddress As IPAddress = ipAddress.Parse("127.0.0.1")
_listener = New TcpListener(ipAddress, 7090)
_listener.Start()
While True
Dim client As TcpClient
client = _listener.AcceptTcpClient
Dim stream As NetworkStream = client.GetStream()
Dim buffer(4096) As Byte
stream.Read(buffer, 0, buffer.Length)
Dim data As String =
System.Text.Encoding.ASCII.GetString(buffer, 0, buffer.Length)
Dim url As String = GetUrl(data)
Dim clientForward As TcpClient = New TcpClient(url, 80)
clientForward.SendTimeout = 3000
Dim streamOut As NetworkStream = clientForward.GetStream
streamOut.Write(buffer, 0, buffer.Length)
Application.DoEvents()
streamOut.Read(buffer, 0, buffer.Length)
stream.Write(buffer, 0, buffer.Length)
stream.Close()
client.Close()
If Not stream.CanRead Then
streamOut.Close()
clientForward.Close()
End If
End While
<end code>