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