Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

TCP socket listen only question

Posted on 2013-10-29
2
Medium Priority
?
401 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

610 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