How to close Winsock Connection

Dear Experts,

I have difficulties in Closing WInsock connection.

I can establish a connection to my server (which is a vision controller) via Ethernet. But when I close the application using close method, and open again, it fail to reconnect. It always give connection state to be either sckConnecting or sckError.

I cannot close a TCP/IP connection via close method? Even if I use close method, it does not invoke any _close event.

How can I establish my connection everytime I launch my application?

cwh
cwhAsked:
Who is Participating?
 
ennixoConnect With a Mentor Commented:
if you want to close a winsock you must do
Winsock.Close
Winsock.LocalPort = 0

if the other side closes the connection the Winsock_Close() event occurs, in this sub you have to do this to close properly the connection :
Do While Winsock.State <> sckClosed
    Winsock.Close
Loop

now it should work.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Can you post the revelant code?

Idle_Mind
0
 
zzzzzoocCommented:
>> But when I close the application using close method

You mean when you close the app. and reopen it, the connection will fail? If so, don't use .LocalPort unless it's absolutely required. Upon connection, if your instance of Winsock has LocalPort=0, it'll automatically assign you an available port to use. If it's a static port, you'll have to wait until the local port's state is finalized which isn't immediately done using the Close() method.

About that.. if you go into Command Prompt and type "netstat", you'll notice listings with "TIME_WAIT". This means the socket has been closed (the port) and is waiting a period of x # of seconds until it's reusable by other applications. This is to ensure no other application uses the same port and receives data it's not meant to. IE: It's to ensure the connection is absolutely closed on both ends.

All-in-all, don't use .LocalPort unless it's a must.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
cwhAuthor Commented:
The problem is the device I am communicating to at the other end is a vision controller. I cannot check whether it will close the connection.  According to ennixo, the close event only be invoked if the other end close. In my case, I think my controller will not feedback anything to me.

Also, I do not use any localport because my port number is to fixed at 8500. Do you mean I have to change the port number to 0 for everytime I close connection?

Whenever I keep getting the state = sckConnecting (6), or (9), what I do is to open my hyper-terminal, and connect and disconnect it, and sometimes my application connection will be established.

If I still fail to establish connection with connect and disconnect via hyperterminal, then I have to unplug my network cable to the vision controller, and connect it back after a while, and the connection will be established!

I do not know why. I will try also: (What is the different between Do While or Do Until???)

Do While Winsock.State <> sckClosed
    Winsock.Close
Loop

0
 
zzzzzoocConnect With a Mentor Commented:
Hmm.. using the Close() method will force the connection to close between client and server but the server (vision controller?) still needs to close it's socket and set it to listen for connections again.

1.) Client connects to server successfully. Client state = 7 (connected), Server state = 7
2.) Client uses .Close(). Client state = 0 (closed), Server state = 8 (closing)
3.) Server must close socket and set it to listen again. <-----

If the "vision controller" must receive a certain packet to "close" the socket, you need to send that. Aside from that, the server should automatically handle clients forcing a .Close().

0
 
cwhAuthor Commented:
Dear experts:

I can now establish my connection! I refer to a telnet VB program and found that ennixo is right about putting
Winsock.LocalPort = 0

I did that and it works! Thank you!!!

However, the problem now is I always get "Connection is forcefully rejected" error. After I close the connection, if I want to connect it again, it will show this error for some time and after a while, maybe a minute or so, the connection will back to normal, (i.e. I can establish connectioin again). Even my previous Hyperterminal and the telnet VB give the same error.

I have searched through msdn and the web, none of the website explain in detail what Connection is forcefully rejected mean and the root cause. Anybody can explain or provide any links? And how can I prevent that?

Please help. Thank you.

cwh
0
 
cwhAuthor Commented:
Dear experts:

I can now establish my connection! I refer to a telnet VB program and found that ennixo is right about putting
Winsock.LocalPort = 0

I did that and it works! Thank you!!!

However, the problem now is I always get "Connection is forcefully rejected" error. After I close the connection, if I want to connect it again, it will show this error for some time and after a while, maybe a minute or so, the connection will back to normal, (i.e. I can establish connectioin again). Even my previous Hyperterminal and the telnet VB give the same error.

I have searched through msdn and the web, none of the website explain in detail what Connection is forcefully rejected mean and the root cause. Anybody can explain or provide any links? And how can I prevent that?

Please help. Thank you.

cwh
0
 
ennixoConnect With a Mentor Commented:
the microsoft winsock error 10061 specifications :
Connection refused. The connection could not be established because the target machine refused it. This error usually occurs because no application on the remote machine is servicing connections on that address.
0
 
ennixoCommented:
perhaps vision controller needs some time (more than winsock) to reinit... i think that's why you get this error.
0
 
cwhAuthor Commented:
thank you experts! I will try to explore more into winsocks and share to you if I find anymore problems. Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.