Home All Groups Group Topic Archive Search About

Proper way to accept data from sockets?

Author
15 Apr 2006 11:25 PM
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

Author
16 Apr 2006 2:41 AM
VJ
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

"derSchweiz" <ke***@keine.de> wrote in message
news:Rhf0g.26241$P01.12050@pd7tw3no...
Show quoteHide quote
> 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
>
Author
16 Apr 2006 6:29 AM
derSchweiz
Great!
Found a good article here- straight and to the point:
http://searchvb.techtarget.com/vsnetTip/1,293823,sid8_gci881357_tax293037,00.html

Its working now.

Show quoteHide quote
"VJ" <some***@ms.com> schrieb im Newsbeitrag
news:eA1aI9PYGHA.1352@TK2MSFTNGP05.phx.gbl...
> 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
>
> "derSchweiz" <ke***@keine.de> wrote in message
> news:Rhf0g.26241$P01.12050@pd7tw3no...
>> 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
>>
>
>
Author
17 Apr 2006 2:37 PM
VJ
Great!!

VJ

"derSchweiz" <ke***@keine.de> wrote in message
news:Wul0g.26813$P01.3099@pd7tw3no...
Show quoteHide quote
> Great!
> Found a good article here- straight and to the point:
> http://searchvb.techtarget.com/vsnetTip/1,293823,sid8_gci881357_tax293037,00.html
>
> Its working now.
>
> "VJ" <some***@ms.com> schrieb im Newsbeitrag
> news:eA1aI9PYGHA.1352@TK2MSFTNGP05.phx.gbl...
>> 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
>>
>> "derSchweiz" <ke***@keine.de> wrote in message
>> news:Rhf0g.26241$P01.12050@pd7tw3no...
>>> 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
>>>
>>
>>
>
>