Solved

How to close Winsock Connection

Posted on 2004-04-09
10
12,956 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now