Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to close Winsock Connection

Posted on 2004-04-09
10
13,056 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:cwh
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 10791064
Can you post the revelant code?

Idle_Mind
0
 
LVL 5

Accepted Solution

by:
ennixo earned 300 total points
ID: 10792287
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
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 10798177
>> 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:cwh
ID: 10803725
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
 
LVL 17

Assisted Solution

by:zzzzzooc
zzzzzooc earned 50 total points
ID: 10808054
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
 

Author Comment

by:cwh
ID: 10819638
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
 

Author Comment

by:cwh
ID: 10819640
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
 
LVL 5

Assisted Solution

by:ennixo
ennixo earned 300 total points
ID: 10820684
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
 
LVL 5

Expert Comment

by:ennixo
ID: 10820690
perhaps vision controller needs some time (more than winsock) to reinit... i think that's why you get this error.
0
 

Author Comment

by:cwh
ID: 10831161
thank you experts! I will try to explore more into winsocks and share to you if I find anymore problems. Thank you!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question