Question TCP Listener and WinForms

Guardian

Active member
Joined
Jan 19, 2005
Messages
44
Location
England
Programming Experience
5-10
Hi all, if i put this piece of code into a shared sub main in a WinForms App, it works fine, i click on my http:/127.0.0.1:988/test and get Hello World back in the browser.

VB.NET:
Public Shared Sub main()

	Dim tcpl As New Net.Sockets.TcpListener(Net.IPAddress.Any, 988)
	tcpl.Start()

	While True
		Dim newSocket As Socket = tcpl.AcceptSocket()
		If newSocket.Connected Then



			Dim outBuf As Byte() = System.Text.Encoding.ASCII.GetBytes("<html><head></head><body>Hello World</body></html>")

			newSocket.Send(outBuf, outBuf.Length, SocketFlags.None)

			'Dim ns As New NetworkStream(newSocket)
			'Dim inBuf(4000) As Byte

			'ns.Read(inBuf, 0, inBuf.Length)

			'Dim s As String = System.Text.Encoding.ASCII.GetString(inBuf)


			'Dim outBuf As Byte() = System.Text.Encoding.ASCII.GetBytes("<html><head></head><body>Test</body></html>")

			'ns.Write(outBuf, 0, outBuf.Length)
			'ns.Flush()
			'ns.Close()

		End If

		Exit While
	End While

End Sub

If however i put it into a button click event or anything (at this point i don't care about blocking/non-blocking, neither work) i get the request back from the web page, but it will not send the response back.

I have tried using blocking and non-blocking, separate threads, background workers, modules etc, and none work.

I need this to be in a winforms application, and i cannot use the HttpListener class (which works great, but we use windows 2000 at work, and HttpListener needs to be on XP SP2 or greater to work).

Any help on this would be great, its getting quite frustrating!
 
Back
Top