473,493 Members | 4,319 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem With System.Net.Sockets

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3753
by: Dmitry Akselrod | last post by:
Hello everyone, I have a vb.net application that wraps the TCPListener object in a class. The server connects to the local interface and establishes itself on port 9900. It then polls for...
0
1783
by: mrpolitics | last post by:
So I'm working with PureIRCD (http://sourceforge.net/projects/pure-ircd) and everything was fine untill yesterday when the server crashed. So I did a cold restart and staretd the server back up...
15
2160
by: mrpolitics | last post by:
So I'm working with PureIRCD (http://sourceforge.net/projects/pure-ircd) and everything was fine untill yesterday when the server crashed. So I did a cold restart and staretd the server back up...
2
1983
by: Michael Turner | last post by:
Hi Guys I have just taken this article from msdn and it is coming up with an error any ideas why? If any one wants to see where I found it...
0
1291
by: Ben | last post by:
I modified the logmonitor sdk example so it would work over a network. It works great when the client and server are running on the same PC and have administrator privileges. So I have two...
0
3896
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
5
23975
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
1
17525
by: Mr. Beck | last post by:
Hello, Please Help..... I have been working with some tcp/ip socket communication within a C# program recently. Basicly, I have a program (myProblemProgram) that has a socket connected to...
1
20543
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but...
0
6980
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7192
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6862
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5452
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.