How do I keep the TCP/IP listener running?

C

Chris

I took some samples from the net to help me create a TCP/IP socket
listener. I stripped out most of the code and put it below. Bottom
line is when I receive the ***END*** line from the client, the client
is done sending me data. Of course when this happens, my server
stops. I would like to know how to keep the server listening after
the client is done communicating. I'm sure most of the reason it stops
is that everything is in Sub Main. I did see some samples on
threading connections but I don't need that. Just one connection at a
time. This works perfectly otherwise.

Thanks for help,

Chris

Imports System.Net.Sockets
Imports System.Net
Imports System.Net.WebProxy
Imports System.IO
Imports System
Imports System.Text.RegularExpressions
Class TCPSrv
Shared Sub Main()
Dim servermessage As String
Dim clientmessage As String
Const portNumber As Integer = 8100
Dim tcpListener As New TcpListener(portNumber)
tcpListener.Start()
Console.WriteLine("Waiting for connection...")
Dim socketforClient As Socket
socketforClient = tcpListener.AcceptSocket()
Dim NetworkStream As NetworkStream
networkStream = New NetworkStream(socketforClient)
Dim streamWriter As StreamWriter
streamWriter = New StreamWriter(NetworkStream)
Dim streamReader As StreamReader
streamReader = New StreamReader(NetworkStream)
Dim strPage As String

Do While (Status)
If socketforClient.Connected Then
servermessage = streamReader.ReadLine()
If servermessage = "***END***" Then
Status = False
Exit Do
End If
clientmessage = "Response to the client"
streamWriter.WriteLine(clientmessage)
streamWriter.Flush()
End If
Console.WriteLine(servermessage)
streamWriter.Flush()

Loop
End Sub
End Class
 
C

Chris

Thanks alot for your help. I picked the Synchronous sample which
worked for me very well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top