tcplistener not working

huhhuhhuh

Active member
Joined
Feb 28, 2006
Messages
42
Programming Experience
Beginner
I've implemented a tcplistener listening on port 8080 in my application. It doesn't seem to work. Do browsers send http requests to ports other than 80 or am I doing something wrong?

Based on what I can understand from the sample code here, upon intercepting a request using tcplistener, the data is read into a buffer than into a string and then sent back to some console. Is there some way to allow the http request after intercepting it. How can I send back my own custom content?
 
I've tried manually configuring the browser to direct requests through port 8080 with the application source and localhost as the address on seperate occasions. My listener still does not appear to be responding.

Const portNumber As Integer = 8080
Public MyHost As String = System.Net.Dns.GetHostName()
Public MyIp As IPAddress = System.Net.Dns.GetHostByName(MyHost).AddressList(0)
Public mytcplistener As New TcpListener(MyIp, portNumber)

I'm using the code above to get the address for my local host. Do I have to specify it as 127.0.0.1 instead? Is there any workaround to this because the reason I implemented sockets in the first place was to get the application to run invisibly without the need for user input or manual configuration of the browser. Ty
 
Dim mytcpclient As TcpClient = mytcplistener.AcceptTcpClient
Dim bytes(mytcpclient.ReceiveBufferSize) As [Byte]
Dim msg As [String]
Dim newstreamAs NetworkStream = mytcpclient.GetStream
newstream.Read(bytes, 1024,
CInt(mytcpclient.ReceiveBufferSize))
msg = Encoding.ASCII.GetString(bytes)
Dim sss As String
sss = sss & "<HTML><HEAD><TITLE>sss</TITLE></HEAD><br>" & vbCrLf
sss= sss & "<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
sss = sss & "<b>500</b> sss<br>" & vbCrLf
sss= sss & "</BODY><br>" & vbCrLf
sss = sss & "</HTML><br>" & vbCrLf
bytes = Encoding.Unicode.GetBytes(sss)
newstream.Write(bytes, 0, bytes.Length)

The code above is what I use for testing. I'm returning random content instead of what I read. All the browser shows is the original content, and none of the content I specified. I'm uncertain whether the listener is working or not. What am I doing wrong?
 
VB.NET:
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. 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. Any help will be greatly appreciated.
 
Last edited by a moderator:
I haven't looked at the code yet, but thought i'd give this quick pointer

Ensure that your browser is connecting to the proper port. There is a way to force this in the address line that you type in:

if you wanted your browser to connect to 8080, for instance, at localhost:

http://localhost:8080

at a real website:

http://real.website.com:8080

the :8080 (or whatever port number) forces the browser to connect to that port...
 
Back
Top