?
Solved

TCP socket listen only question

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

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
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…
Suggested Courses

764 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