bartdsp
asked on
TCP socket listen only question
Hello,
I am attempting to receive data via a socket. The data is sent maybe once a second but there can also be long (as in hours) delays between messages. My code "works" but it sometimes hangs on the .Receive method. Any suggestions on how I can listen to this connection without a hangup? See code below....
Thanks,
bartj
Imports System.IO
Imports System
Imports System.Drawing
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bytes(1024) As Byte
Dim bytesrec As Integer
Dim ipaddress As IPAddress = ipaddress.Parse("192.168.1 0.20")
Dim remoteEP As New IPEndPoint(ipaddress, "23")
Dim sendreq As New Socket(AddressFamily.Inter Network, SocketType.Stream, ProtocolType.Tcp)
sendreq.Connect(remoteEP)
Do While True
Application.DoEvents()
bytesrec = sendreq.Receive(bytes) ' Here is where it hangs sometimes
If bytesrec > 0 Then
'do something here.......
End If
Loop
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
I am attempting to receive data via a socket. The data is sent maybe once a second but there can also be long (as in hours) delays between messages. My code "works" but it sometimes hangs on the .Receive method. Any suggestions on how I can listen to this connection without a hangup? See code below....
Thanks,
bartj
Imports System.IO
Imports System
Imports System.Drawing
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bytes(1024) As Byte
Dim bytesrec As Integer
Dim ipaddress As IPAddress = ipaddress.Parse("192.168.1
Dim remoteEP As New IPEndPoint(ipaddress, "23")
Dim sendreq As New Socket(AddressFamily.Inter
sendreq.Connect(remoteEP)
Do While True
Application.DoEvents()
bytesrec = sendreq.Receive(bytes) ' Here is where it hangs sometimes
If bytesrec > 0 Then
'do something here.......
End If
Loop
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I will look into the BeginReceive method.
I am using a "button" just for ease of testing/debug. It will be in a backgroundworker when implemented(without the doevents).
Thanks for the response.
Bartj