By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,548 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

Problem With System.Net.Sockets

P: n/a
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.Sockets
Imports System.Net.Sockets.SocketException
Imports System.Text
Imports Microsoft.VisualBasic
Imports System.Net.DnsPermissionAttribute
Imports System.Security.Permissions

<DnsPermissionAttribute(SecurityAction.Demand, Unrestricted:=True)>
Public Class WSGateway
Const portNumber As Integer = 5001
Friend checker As New SocketException()
Friend tcpListener As New TcpListener(portNumber)

Public Sub Run()
Const portNumber As Integer = 5001
Dim filecounter As Int32 = 0
Dim tcpListener As New
TcpListener(CType(Dns.Resolve("Localhost").Address List(0), IPAddress),
portNumber)
Console.WriteLine("TCP Server is up and waiting for Client
connection...")
Dim Lifetime As New Sockets.LingerOption(True, 60)
Try
tcpListener.Start()
While True
Dim tcpClient As TcpClient =
tcpListener.AcceptTcpClient()
Dim networkStream As NetworkStream =
tcpClient.GetStream()
Console.WriteLine("Connection accepted.")
Do Until networkStream.DataAvailable = True
'do nothing
Loop
While networkStream.DataAvailable
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, 4)
filecounter = filecounter + 1
Dim ascii As String =
Replace(Encoding.ASCII.GetString(bytes), Chr(0), "")
If ascii.Length > 0 Then
Dim x As String =
Integer.Parse(Asc(Trim(ascii)), Globalization.NumberStyles.HexNumber)
networkStream.Read(bytes, 0, x - 4)
Dim clientdata As String =
Replace(Encoding.ASCII.GetString(bytes), Chr(0), "")
Microsoft.VisualBasic.FileOpen(1, "C:\test" &
filecounter & ".txt", OpenMode.Output, OpenAccess.Write,
OpenShare.LockWrite)
Microsoft.VisualBasic.Print(1, clientdata)
Microsoft.VisualBasic.FileClose(1)
Dim responseString As String = ""
responseString = Replace(clientdata, "POLL
AC", "POLL AC ACK")
Dim resplen As Int32 = responseString.Length +
4
Dim sendBytes As [Byte]() =
Encoding.ASCII.GetBytes(responseString)
Dim headbytes(4) As Byte
headbytes = Encoding.ASCII.GetBytes(Chr(0) &
Chr(0) & Chr(0) & Chr(resplen))
Dim finalbytes(sendBytes.Length + 4) As Byte
headbytes.CopyTo(finalbytes, 0)
sendBytes.CopyTo(finalbytes, 4)
Microsoft.VisualBasic.FileOpen(1, "C:\testing"
& filecounter & ".txt", OpenMode.Output, OpenAccess.Write,
OpenShare.LockWrite)
Microsoft.VisualBasic.Print(1,
Encoding.ASCII.GetString(finalbytes))
Microsoft.VisualBasic.FileClose(1)
networkStream.Write(finalbytes, 0,
finalbytes.Length)
Console.WriteLine(("Message Sent by TCP Server
/> : " + responseString))
If clientdata.EndsWith("&H2B") Then
networkStream.Close()
Exit While
End If
clientdata = ""
responseString = ""
bytes.Clear(bytes, 0, bytes.Length)
Else
networkStream.Flush()
Exit While
End If
End While
End While

tcpListener.Stop()
Console.WriteLine("Exit")
Console.ReadLine()
Catch e As Exception

Console.WriteLine(e.ToString())
Console.ReadLine()
End Try
End Sub
End Class
Nov 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.