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.
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.