|
-
Jan 15th, 2004, 05:41 AM
#1
Thread Starter
Addicted Member
Winsock connections - [RESOLVED]
I am having a problem with winsock, ever 30 seconds or so i am connecting to a device, which allows TCP/IP connection and doing a bit of data transfer. if when i have completed the data transfer i leave the socket open, when i return after 30 seconds i check the connection is ok, if the connection has been closed by the device i reopen it and continue everything works fine. If however if i close the socket after i have completed the data transfer and try to connect after 30 seconds the connection attempt time-out, if left to retry the socket every 30 seconds it will on occasion connect but it is but no means guarantied.
I need to find a way to avoid this problem as this code is contained within a dll and so I need to reopen the socket every time the dll is created.
Any help please.
Last edited by mik706; Jan 16th, 2004 at 05:00 AM.
Mik706
-
Jan 15th, 2004, 05:56 AM
#2
What device are you connecting to?
Woof
-
Jan 15th, 2004, 06:00 AM
#3
Thread Starter
Addicted Member
It is called a CMAC it is a modbus gateway device allowing the connection of multiple modbus devices to Ethernet network, so data from the modbus devices can be interrogated remotely.
-
Jan 15th, 2004, 09:22 AM
#4
Thread Starter
Addicted Member
-
Jan 15th, 2004, 09:43 AM
#5
Sounds like the device isn't disconnecting correctly.
After 30seconds the device disconnects the connection, thus everything is correctly done. But when u manually disconnect it seems like the code in the device isn't working correctly 
Nowt much you can do about it really...
Woka
-
Jan 15th, 2004, 09:48 AM
#6
Thread Starter
Addicted Member
Cheers for the relpy, i thought it was going to be that but i just thought i would ask.
-
Jan 15th, 2004, 09:55 AM
#7
Frenzied Member
Please post the code that does the connect request. The output from netstat after your code closes the connection would also be helpful. If you are not familiar with netstat, it is run from a CMD prompt.
-
Jan 15th, 2004, 10:44 AM
#8
Thread Starter
Addicted Member
Open Socket:
VB Code:
With frmMain
.SockClient.LocalPort = 1
.SockClient.RemotePort = 2700
.SockClient.Protocol = sckTCPProtocol
.SockClient.RemoteHost = "192.6.190.1"
.SockClient.Connect
While .SockClient.State <> sckConnected And .SockClient.State <> sckError And GetTickCount < (Starttime + 2400)
DoEvents: DoEvents: DoEvents
Wend
End With
Close Socket:
VB Code:
If frmMain.SockClient.State = sckConnected Then frmMain.SockClient.Close
netstat results:
TCP MW00873:1 host-192-6-190-1.thebook.hp.com:2700 FIN_WAIT_2
-
Jan 15th, 2004, 11:09 AM
#9
Thread Starter
Addicted Member
After your suggestion of looking at netstat I watched the progress of port1 (local) to see what happened.
About 30 secs after I supposedly close the socket the state of the port changes from:
TCP MW00873:1 host-192-6-190-1.thebook.hp.com:2700 FIN_WAIT_2
To:
TCP MW00873:1 host-192-6-190-1.thebook.hp.com:2700 TIME_WAIT
The state remains for about 4 mins and then I assume becomes inactive, at which time I can reconnect to the device.
-
Jan 15th, 2004, 01:36 PM
#10
Frenzied Member
Originally posted by mik706
After your suggestion of looking at netstat I watched the progress of port1 (local) to see what happened.
About 30 secs after I supposedly close the socket the state of the port changes from:
TCP MW00873:1 host-192-6-190-1.thebook.hp.com:2700 FIN_WAIT_2
To:
TCP MW00873:1 host-192-6-190-1.thebook.hp.com:2700 TIME_WAIT
The state remains for about 4 mins and then I assume becomes inactive, at which time I can reconnect to the device.
This is what I expected we would see. The TIME_WAIT value is set to 2 times the maximum segment lifetime, which is related to the time-to-live (TTL) value of IP datagrams within the network. This allows TCP to ensure that connections are properly closed. The side initiating the close must hang around long enough for the other side to send its final packet.
When you set the local port, you are stuck with this situation. You should either set it to zero or delete the .SockClient.LocalPort = 1 statement. This forces the system to select an unused port number.
-
Jan 16th, 2004, 05:00 AM
#11
Thread Starter
Addicted Member
Cheers for the help, I changed the .SockClient.LocalPort = 1 statement to .SockClient.LocalPort = 0 and everything is working fine. I should have noticed this before as i had this statment in an earlier version of the code.
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
|