Solved

TCP socket listen only question

Posted on 2013-10-29
2
377 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
2 Comments
 
LVL 74

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Network ports are the threads that hold network communication together. They are an essential part of networking that can be easily ignore or misunderstood, my goals is to show those who don't have a strong network foundation how network ports opera…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now