Proper way to accept data from sockets?

  • Thread starter Thread starter derSchweiz
  • Start date Start date
D

derSchweiz

Hi,

Experimenting with sockets, and I think I got it half working. I have the
following code:

Private Sub socket_receive(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Activated
Dim bytesRec As Integer
Dim i As Integer = 0
Dim data1 As Net.Sockets.Socket = cidServer.Accept() 'this line is
causing program to hang

bytesRec = data1.Receive(bytes)

dataString = System.Text.Encoding.Default.GetString(bytes, 0,
bytesRec)

If dataString = "f" Then
MsgBox("Du hast f gedruckt")
data1.Disconnect(True)
Else
MsgBox("Du hast f nicht gedruckt")
data1.Disconnect(True)
End If
End Sub

Basically, what I have discovered is that once this section of code has
loaded, the rest of the program will halt because it is expecting data from
the line:
Dim data1 As Net.Sockets.Socket = cidServer.Accept()

I have sockets working properly, so if I go and telnet localhost <port#> and
then just enter the key 'f' a message box will pop up telling me that I hit
the letter 'f', or not respectively. After this event the program will
resume working, since it is finished with "Dim data1 As Net.Sockets.Socket =
cidServer.Accept()". This is sort of like scanf() in C where the program
stops and waits for input.

My question is how would I allow the program to run while listen to the
socket at the same time? I.e. I can use the program and I can telnet
localhost <port#> without having a hung program? I was thinking if there
might be an event handler that could activate my socket_receive() method if
a user telnets in and enters data.

-Thanks
 
You can listen to the socket on a different thread... What do is spawn
thread from the UI application and then in that thread you can create and
listen to sockets.. Everytime you want to notify the main UI thread, you can
send a notification using events...

How you can do the above really depends whether you are with VS.NET 2003
(1.1) or VS.NET 2005(2.0)?

VJ
 
Back
Top