|
-
Nov 12th, 2004, 05:34 AM
#1
Thread Starter
Addicted Member
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.
-
Nov 12th, 2004, 10:13 AM
#2
Frenzied Member
What's the exception? Guessing something about the object being disposed?
-
Nov 12th, 2004, 12:12 PM
#3
Thread Starter
Addicted Member
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..
-
Nov 12th, 2004, 12:19 PM
#4
Frenzied Member
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?
-
Nov 12th, 2004, 12:28 PM
#5
Thread Starter
Addicted Member
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..
-
Nov 12th, 2004, 12:40 PM
#6
Frenzied Member
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:
Imports System.Net.Sockets
Module Connection
' Get rid of the new keywork
'Dim tcpC As New TcpClient
Dim tcpC as TcpClient
Public Sub Connect()
'Instantiate here
tcpC = New TcpClient
'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
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
-
Nov 12th, 2004, 12:48 PM
#7
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|