PDA

Click to See Complete Forum and Search --> : Listener help


huhhuh
Mar 15th, 2006, 12:36 AM
Public myip2 As IPAddress = IPAddress.Parse("127.0.0.1")
Const portNumber As Integer = 8889
Public MyHost As String = System.Net.Dns.GetHostName()
Public MyIp As IPAddress = System.Net.Dns.GetHostByName(MyHost).AddressList(0)
Public mytcplistener As New TcpListener(myip2, portNumber)
Public listeningthread As Thread

Public Sub startserver()
listeningthread = New Thread(AddressOf listening)
listeningthread.Start()
End Sub
Public Sub listening()
mytcplistener.Start()
Dim mytcpclient As TcpClient = mytcplistener.AcceptTcpClient
Dim bytes(mytcpclient.ReceiveBufferSize) As [Byte]
Dim msg As [String]
Dim readstream As NetworkStream = mytcpclient.GetStream
readstream.Read(bytes, 1024, CInt(mytcpclient.ReceiveBufferSize))
msg = Encoding.ASCII.GetString(bytes)

Dim sURL As String = " "

Dim index1 As Integer = msg.IndexOf(" "c)
Dim index2 As Integer = msg.IndexOf(" "c, index1 + 1)

Dim part1 As String = msg.Substring(index1 + 1, index2 - index1)
Dim index3 As Integer = part1.IndexOf("/"c, index1 + 8)
Dim index4 As Integer = part1.IndexOf(" "c, index1 + 8)
Dim index5 As Integer = index4 - index3
sURL = part1.Substring(index1 + 4, part1.Length - index5 - 8)

Dim sTMP As String
sTMP = sTMP & "<HTML><HEAD><TITLE>sss</TITLE></HEAD><br>" & vbCrLf
sTMP = sTMP & "<BODY BGCOLOR=" & Chr(34) & "#FFFFFF" & Chr(34) & " Text=" & Chr(34) & "#000000" & Chr(34) & " LINK=" & Chr(34) & "#0000FF" & Chr(34) & " VLINK=" & Chr(34) & "#000080" & Chr(34) & " ALINK=" & Chr(34) & "#008000" & Chr(34) & "><br>" & vbCrLf
sTMP = sTMP & "<b>500</b> ssss<br>" & vbCrLf
sTMP = sTMP & "</BODY><br>" & vbCrLf
sTMP = sTMP & "</HTML><br>" & vbCrLf
bytes = Encoding.Unicode.GetBytes(sTMP)
readstream.Write(bytes, 1, bytes.Length)

End Sub

Public Sub stopserver()
mytcplistener.Stop()
End Sub

This is a portion of a class I've developed for the purpose of filtering browser requests. It has been heavily modified from bits and pieces of sample code I've obtained. For testing purposes, I'm sending back data other than the request. My tcplistener doesn't seem to be working as browser requests get handled normally even when I specify it to direct requests through the local host and port 8889. What am I doing wrong?

I'm new to network and Internet programming and from what I can understand of the sample codes I've obtained so far, the code above starts a new thread which starts a listener at the localhost listening to port 8889. It then intercepts a client request to that port and converts it into ASCII string. The test string I'm sending back is encoded in Unicode. Additionally, a string has been created to extract the url from the message. Can someone inform me if I've misunderstood the code?

My final aim is to listen on port 80 for http requests, obtain the requests and filter them, retrieve the requested item, parse and modify the requested item, and return it to the user. Do personal firewalls or enabled filtering programs interfere with my listener? Any help will be greatly appreciated.

huhhuh
Mar 15th, 2006, 04:09 AM
Please help, this is urgent. It's the only part of my application I can't get to work. If I'm doing something wrong, or if I should go about this another way, please tell me. Thx

the182guy
Mar 15th, 2006, 09:18 AM
if you tell internet explorer to go through a proxy, it will see your program is a real proxy. So when IE requests pages, it will send a chunk of header information telling your app where to connect to so you have to parse it and connect so you can send data. so you need 2 sockets, one for listening for IE connection, and the other for connecting to the actual web server to get the info, i dont see a second socket there but I have no idea about vb.net sockets, it would be a hell of a lot easier in vb6

huhhuh
Mar 15th, 2006, 10:08 PM
What happens when you create a tcpclient with tcplistener.accepttcpclient? Does that mean the tcpclient establishes a connection with the listener as the end point or the remote host the user is trying to connect to? Are you saying that the listener accepts the tcpclient, they have a connection, and then the listener should parse the header and open a new socket to the endpoint based on the header before returning the request to the client?