I an having issuses with a loop I am running to keep checking for
messages coming across our line.
The goal:
Listen for messages on port 5001 and print the messages into a text
file. The port must stay open at all times and only close if no
activity for 60 seconds.
The problem:
If I do a read on a networkstream and the connection is closed
unexpectedly by the client side, the application hangs.
Other usefuldata for reading the sample code:
The networkstream is delimited by a 4 byte header. the first 4 bytes
tell you the length of the data you are reading for a particular
message (in hex)
Any help would be appreciated. Here is some sample code:
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sock ets
Imports System.Net.Sock ets.SocketExcep tion
Imports System.Text
Imports Microsoft.Visua lBasic
Imports System.Net.DnsP ermissionAttrib ute
Imports System.Security .Permissions
<DnsPermissionA ttribute(Securi tyAction.Demand , Unrestricted:=T rue)>
Public Class WSGateway
Const portNumber As Integer = 5001
Friend checker As New SocketException ()
Friend tcpListener As New TcpListener(por tNumber)
Public Sub Run()
Const portNumber As Integer = 5001
Dim filecounter As Int32 = 0
Dim tcpListener As New
TcpListener(CTy pe(Dns.Resolve( "Localhost").Ad dressList(0), IPAddress),
portNumber)
Console.WriteLi ne("TCP Server is up and waiting for Client
connection...")
Dim Lifetime As New Sockets.LingerO ption(True, 60)
Try
tcpListener.Sta rt()
While True
Dim tcpClient As TcpClient =
tcpListener.Acc eptTcpClient()
Dim networkStream As NetworkStream =
tcpClient.GetSt ream()
Console.WriteLi ne("Connection accepted.")
Do Until networkStream.D ataAvailable = True
'do nothing
Loop
While networkStream.D ataAvailable
Dim bytes(tcpClient .ReceiveBufferS ize) As Byte
networkStream.R ead(bytes, 0, 4)
filecounter = filecounter + 1
Dim ascii As String =
Replace(Encodin g.ASCII.GetStri ng(bytes), Chr(0), "")
If ascii.Length > 0 Then
Dim x As String =
Integer.Parse(A sc(Trim(ascii)) , Globalization.N umberStyles.Hex Number)
networkStream.R ead(bytes, 0, x - 4)
Dim clientdata As String =
Replace(Encodin g.ASCII.GetStri ng(bytes), Chr(0), "")
Microsoft.Visua lBasic.FileOpen (1, "C:\test" &
filecounter & ".txt", OpenMode.Output , OpenAccess.Writ e,
OpenShare.LockW rite)
Microsoft.Visua lBasic.Print(1, clientdata)
Microsoft.Visua lBasic.FileClos e(1)
Dim responseString As String = ""
responseString = Replace(clientd ata, "POLL
AC", "POLL AC ACK")
Dim resplen As Int32 = responseString. Length +
4
Dim sendBytes As [Byte]() =
Encoding.ASCII. GetBytes(respon seString)
Dim headbytes(4) As Byte
headbytes = Encoding.ASCII. GetBytes(Chr(0) &
Chr(0) & Chr(0) & Chr(resplen))
Dim finalbytes(send Bytes.Length + 4) As Byte
headbytes.CopyT o(finalbytes, 0)
sendBytes.CopyT o(finalbytes, 4)
Microsoft.Visua lBasic.FileOpen (1, "C:\testing "
& filecounter & ".txt", OpenMode.Output , OpenAccess.Writ e,
OpenShare.LockW rite)
Microsoft.Visua lBasic.Print(1,
Encoding.ASCII. GetString(final bytes))
Microsoft.Visua lBasic.FileClos e(1)
networkStream.W rite(finalbytes , 0,
finalbytes.Leng th)
Console.WriteLi ne(("Message Sent by TCP Server
/> : " + responseString) )
If clientdata.Ends With("&H2B") Then
networkStream.C lose()
Exit While
End If
clientdata = ""
responseString = ""
bytes.Clear(byt es, 0, bytes.Length)
Else
networkStream.F lush()
Exit While
End If
End While
End While
tcpListener.Sto p()
Console.WriteLi ne("Exit")
Console.ReadLin e()
Catch e As Exception
Console.WriteLi ne(e.ToString() )
Console.ReadLin e()
End Try
End Sub
End Class