Results 1 to 7 of 7

Thread: VB.NET: Simple Connect and Disconnect using System.Net.Sockets...[Resolved]

  1. #1

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230

    VB.NET: Simple Connect and Disconnect using System.Net.Sockets...[Resolved]

    Say i have declare a module for connection and two buttons...

    one button has the method connect() and
    the other has the method disconnect()....

    how do i actually connect after i had disconnect my tcpC...
    I could not connect after i disconnect my connection...

    Code:
    Imports System.Net.Sockets 
    
    Module Connection 
    
        Dim tcpC As New TcpClient 
      
         Public Sub Connect() 
    
                 'Connect from this to the server at Port 6970 
                  Try 
                       tcpC.Connect("152.138.40.200", 6970) 
                  Catch e As Exception 
                       Console.WriteLine(e.ToString()) 
                  End Try 
        End Sub 
    
        Public Sub Disconnect() 
                tcpC.Close() 
        End Sub 
    
    End Module
    Thanks
    Last edited by toytoy; Dec 3rd, 2004 at 07:47 AM.

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    What's the exception? Guessing something about the object being disposed?

  3. #3

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    there is no exception...

    the connection just break out after i disconnect..

    therefore i can't connect again using the "Connect button"until i re-start the whole program..

  4. #4
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    I don't understand. What do you mean by "the connection just break out"? If you step through your code when you try to connect again, what happens?

  5. #5

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    that means:

    1 step: when i start the program, it can connect to the server through the "Connect" Buton...

    2 steps: when i click the "Disconnect" button, the connection is then closed...

    3 steps: However the connection could not connect again when i click the "Connect" button the second time..

    the connection just break out with no exception...
    my string that i sent over, cannot get cross it the second time...
    that means it could only get connected once..

  6. #6
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    ok, I'm suprised no exception is happening. Maybe you don't see it because the exception is writing to the console? You could try not catching the exception and then see what's happening.

    Regardless, I believe your problem is that you cannot close a socket and then reopen it, because the close method calls dispose. What you should do is create a new TcpClient every time you need to connect. Something like
    VB Code:
    1. Imports System.Net.Sockets
    2.  
    3. Module Connection
    4.     ' Get rid of the new keywork
    5.     'Dim tcpC As New TcpClient
    6.      Dim tcpC as TcpClient
    7.  
    8.      Public Sub Connect()
    9.              'Instantiate here
    10.               tcpC = New TcpClient
    11.              'Connect from this to the server at Port 6970
    12.               Try
    13.                    tcpC.Connect("152.138.40.200", 6970)
    14.               Catch e As Exception
    15.                    Console.WriteLine(e.ToString())
    16.               End Try
    17.     End Sub     Public Sub Disconnect()
    18.             tcpC.Close()
    19.     End Sub End Module

    Notice how tcpC gets declared in a way that both methods can use it, but it gets instantiated every time you attempt to connect.

    HTH,
    Mike

  7. #7

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    Mike Hildner thanks...

    I get it..

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