Solved

TCP socket listen only question

Posted on 2013-10-29
2
378 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

911 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

20 Experts available now in Live!

Get 1:1 Help Now