Results 1 to 13 of 13

Thread: [Resolved] Problem with code please help (System.Net.Sockets) Related

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    37

    [Resolved] Problem with code please help (System.Net.Sockets) Related

    Ok, I have the following code working except my form locks up until someone connects, then after they connect it locks up until data is sent once data is received and a msg box pops up the form locks up again... Can someone tell me what I am doing wrong? This does exactly what I need it to do but what I dont need it to do is lock up the form.

    VB Code:
    1. Private Sub cmdListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdListen.Click
    2.         Dim ipadd As IPAddress = IPAddress.Parse("192.168.1.100")
    3.         Dim port As String = "9909"
    4.         Dim bytes(1024) As [Byte]
    5.         Dim data As [String] = Nothing
    6.  
    7.         Dim Listen As New TcpListener(ipadd, port)
    8.         Listen.Start()
    9.  
    10.         Dim client As TcpClient = Listen.AcceptTcpClient()
    11.  
    12.  
    13.         data = Nothing
    14.  
    15.  
    16.         Dim stream As NetworkStream = client.GetStream()
    17.  
    18.         Dim i As Int32
    19.  
    20.  
    21.         i = stream.Read(bytes, 0, bytes.Length)
    22.  
    23.         data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
    24.         MsgBox([String].Format("Received: {0}", data))
    25.  
    26.         i = stream.Read(bytes, 0, bytes.Length)
    27.         cmdListen.Enabled = False
    28.  
    29.     End Sub


    Basically the code is setup to listen on an IP/Port. When someone connects it will wait for that persons input then display a msg box when they type something. I am wanting to grab what they type and depending on what they type do a specific thing. I already know how to do this as long as the form doesnt lock up!

    Thanks for any help.


    P.S. Hope I am not confusing
    Last edited by dsc_chris; Jul 26th, 2004 at 03:04 PM.

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    The reason you are locking up is because you are using methods that block, that is, they block the thread until that method completes. I assume all your code is in one thread, so when you call a blocking method, it will hang the GUI.

    Both the TcpListener.Start and the NetworkStream.Read methods are blocking methods.

    I've taken a different route with my socket programming, using multiple threads and asynchronous methods. But, looking at MSDN, you can avoid blocking with TcpListener.Start by using the Pending method, and avoid blocking with NetworkStream.Read by using the DataAvailable property.

    HTH,
    Mike

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    37
    Thats what I was thinking it was doing, Would you be able to maybe show me some example code?

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    37
    Ignore my last request, I think I have found exactly what I needed when you refered to the MSDN. Thank You I will let you know if it works.

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    37
    Ok, That has stopped the lockup issue. But it isnt processing anything after Else. Like on an incoming connection... (I am a n00b to .NET it is totally different from vb6 in terms of sockets.)


    New Code:

    VB Code:
    1. Private Sub cmdListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdListen.Click
    2.  
    3.         Dim ipadd As IPAddress = IPAddress.Parse("192.168.1.100")
    4.         Dim port As String = "9909"
    5.         Dim bytes(1024) As [Byte]
    6.         Dim data As [String] = Nothing
    7.  
    8.         Dim Listen As New TcpListener(ipadd, port)
    9.         Listen.Start()
    10.         If Not Listen.Pending Then
    11.  
    12.             MsgBox("No incoming connections")
    13.         Else
    14.             Dim client As TcpClient = Listen.AcceptTcpClient()
    15.  
    16.  
    17.             data = Nothing
    18.  
    19.  
    20.             Dim stream As NetworkStream = client.GetStream()
    21.  
    22.             Dim i As Int32
    23.  
    24.  
    25.             i = stream.Read(bytes, 0, bytes.Length)
    26.  
    27.             data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
    28.             MsgBox([String].Format("Received: {0}", data))
    29.  
    30.             i = stream.Read(bytes, 0, bytes.Length)
    31.             cmdListen.Enabled = False
    32.         End If
    33.  
    34.  
    35.     End Sub

  6. #6
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Can try - but example code for what? For the Pending and DataAvailable stuff? I've actually never used that, so I don't have any, although I'd search MSDN for an example.

    If you're interested in asynchronous socket programming, look up in MSDN the topics Using an Asynchronous Server Socket, Using an Asynchronous Client Socket, Asynchronous Server Socket Example and Asynchronous Client Socket Example.

    If you have played with it yet, asynchronous programming can take a little while to get your head around - although it's pretty cool. Depending on your application, you may not even need it. I've read, but don't know for sure myself, that you can handle up to 2000 connections without having to go asynchronous.

    Of course, your mileage may vary

  7. #7
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Originally posted by dsc_chris
    Ignore my last request...
    Too late I did not try to run your code, but where exactly does the code block? Looks like you handled the TCPListener with the Pending method, but how about your NetworkStream.Read? It will block there. Aren't you going to try the DataAvailable property?

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    37
    What I am looking for is a solution to listen on a specific ip/port and on any incoming data to that connection it process's the data and depending on what the data is it does certain things (things i would specify in the code itself) I dont need to handle 2000 connections :P just a single connection hehe

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    37
    This is the new code. Still no go, Am I putting the code in the right place? Right now I have it so when you click the Listen button it process's the code.

    VB Code:
    1. Private Sub cmdListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdListen.Click
    2.  
    3.         Dim ipadd As IPAddress = IPAddress.Parse("192.168.1.100")
    4.         Dim port As String = "9909"
    5.         Dim bytes(1024) As [Byte]
    6.         Dim data As [String] = Nothing
    7.  
    8.         Dim Listen As New TcpListener(ipadd, port)
    9.         Listen.Start()
    10.         If Not Listen.Pending Then
    11.  
    12.             MsgBox("No incoming connections")
    13.         Else
    14.             Dim client As TcpClient = Listen.AcceptTcpClient()
    15.  
    16.  
    17.             data = Nothing
    18.  
    19.  
    20.             Dim stream As NetworkStream = client.GetStream()
    21.  
    22.             Dim i As Int32
    23.             If stream.CanRead Then
    24.                 Do
    25.                     i = stream.Read(bytes, 0, bytes.Length)
    26.                     data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
    27.                     MsgBox([String].Format("Received: {0}", data))
    28.                 Loop While stream.DataAvailable
    29.  
    30.                 cmdListen.Enabled = False
    31.             End If
    32.         End If
    33.  
    34.     End Sub

  10. #10

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    37
    Ok, I have fixed it simply by adding a loop. BUT when it gets to actually processing the incoming data it locks up...



    VB Code:
    1. Private Sub cmdListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdListen.Click
    2.  
    3.         Dim ipadd As IPAddress = IPAddress.Parse("192.168.1.100")
    4.         Dim port As String = "9909"
    5.         Dim bytes(1024) As [Byte]
    6.         Dim data As [String] = Nothing
    7.  
    8.         Dim Listen As New TcpListener(ipadd, port)
    9.         Listen.Start()
    10.         MsgBox(Listen.Pending)
    11.  
    12.         Do
    13.             If Not Listen.Pending Then
    14.  
    15.                 MsgBox("No incoming connections")
    16.                 lblSocket.Text = "No Incoming Connections"
    17.             Else
    18.                 Dim client As TcpClient = Listen.AcceptTcpClient()
    19.  
    20.                 lblSocket.Text = "Client Connected"
    21.                 data = Nothing
    22.  
    23.  
    24.                 Dim stream As NetworkStream = client.GetStream()
    25.  
    26.                 Dim i As Int32
    27.                 If stream.CanRead Then
    28.                     Do
    29.                         i = stream.Read(bytes, 0, bytes.Length)
    30.                         data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
    31.                         'MsgBox([String].Format("Received: {0}", data))
    32.                         lblSocket.Text = ([String].Format("Received: {0}", data))
    33.                     Loop While stream.DataAvailable
    34.  
    35.                     cmdListen.Enabled = False
    36.                 End If
    37.             End If
    38.         Loop
    39.     End Sub

  11. #11
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Try changing your NetworkStream.CanRead to NetworkSteam.DataAvailable. CanRead just tells you that the stream supports reading. I think you're still blocking on the NetworkStream.Read.

  12. #12

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    37
    I tried that. Actually what was hogging the form down was that stupid loop. I threw it all into a timer and moved the Dim's to the Pubilc Declarations area and just simply put in the command button

    VB Code:
    1. Listen.Start()
    2. tmrSockets.Interval = 1
    3. tmrSockets.Enabled = True

    This seems to have fixed the small issue i had. Now if I can figure out how to prevent it from disconnecting right after it receives data i am good to go :P

  13. #13

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    37
    Sweet. I did a little bit of testing and it works exactly how I needed it to... Thanks a WHOLE BUNCH Mike. You were an awsome help! I hope this thread is useful to others looking at simple ways around the winsock controls :P


    Regards

    Chris Childers
    DarkStar Communications

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width