Listenning TCP Server

G

Guest

Hi,

I'm trying to make a TCP Server application that continuously listens to all
ASCII data coming through a specific port and save them in a text file. I got
it working for one receive (listenning) TCP session. I added an infinite loop
in my sub-routine (code is below) but it receives the first stream in the
first loop and in the next loop it's stuck at (Dim client As Socket =
myTcpListener.AcceptSocket) . I don't get any exceptions, it just hangs in
there. Does anyone know how we can have a listenning TCP server?


Do
Dim ipAddress As IPAddress =
Dns.Resolve(SystemInformation.ComputerName).AddressList(0)
Dim myTcpListener As New TcpListener(ipAddress, txtPort.Text)
myTcpListener.Start()
Dim client As Socket = myTcpListener.AcceptSocket
If client.Available > 0 Then
Dim bytes(512) As Byte
client.Receive(bytes)
Dim clientData As String = Encoding.ASCII.GetString(bytes)

Dim sw As StreamWriter = File.AppendText("C:\test.txt")
sw.Write(Now & vbTab & clientData.Trim(Chr(0)))
sw.Flush() : sw.Close()
End If
client.Close()
myTcpListener.Stop()
Loop
 
P

Patrick Steele [MVP]

Hi,

I'm trying to make a TCP Server application that continuously listens to all
ASCII data coming through a specific port and save them in a text file. I got
it working for one receive (listenning) TCP session. I added an infinite loop
in my sub-routine (code is below) but it receives the first stream in the
first loop and in the next loop it's stuck at (Dim client As Socket =
myTcpListener.AcceptSocket) . I don't get any exceptions, it just hangs in
there. Does anyone know how we can have a listenning TCP server?


Do
Dim ipAddress As IPAddress =
Dns.Resolve(SystemInformation.ComputerName).AddressList(0)
Dim myTcpListener As New TcpListener(ipAddress, txtPort.Text)
myTcpListener.Start()
Dim client As Socket = myTcpListener.AcceptSocket
If client.Available > 0 Then
Dim bytes(512) As Byte
client.Receive(bytes)
Dim clientData As String = Encoding.ASCII.GetString(bytes)

Dim sw As StreamWriter = File.AppendText("C:\test.txt")
sw.Write(Now & vbTab & clientData.Trim(Chr(0)))
sw.Flush() : sw.Close()
End If
client.Close()
myTcpListener.Stop()
Loop

Just a guess: Getting the ipAddress and starting/stopping the listener
probably don't need to be in the do...loop. I would put the do loop
right before the AcceptSocket and pull the TcpListener.Stop out of the
loop.
 

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