Solved

TCP socket listen only question

Posted on 2013-10-29
2
390 Views
Last Modified: 2013-10-30
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.10.20")
        Dim remoteEP As New IPEndPoint(ipaddress, "23")
        Dim sendreq As New Socket(AddressFamily.InterNetwork, 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
0
Comment
Question by:bartdsp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39610165
From the documentation of the Receive method:

If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout.

Look into using one of the asynchronous overloads of BeginRecieve to listen without blocking.

Off-topic:  Why are you doing all of this within a button click handler? It is more preferable to spawn a new thread or use a BackgroundWorker for logic that is long-running, and which can potentially lock the UI thread. Application.DoEvents is often used as a hack/shortcut for such results.
0
 

Author Comment

by:bartdsp
ID: 39610195
Hi,

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
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question