Results 1 to 11 of 11

Thread: Winsock connections - [RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147

    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

  2. #2

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147
    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.
    Mik706

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147
    *BUMP*
    Mik706

  5. #5
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147
    Cheers for the relpy, i thought it was going to be that but i just thought i would ask.
    Mik706

  7. #7
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    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.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147
    Open Socket:
    VB Code:
    1. With frmMain
    2.         .SockClient.LocalPort = 1
    3.         .SockClient.RemotePort = 2700
    4.         .SockClient.Protocol = sckTCPProtocol
    5.         .SockClient.RemoteHost = "192.6.190.1"
    6.         .SockClient.Connect
    7.        
    8.         While .SockClient.State <> sckConnected And .SockClient.State <> sckError And GetTickCount < (Starttime + 2400)
    9.             DoEvents: DoEvents: DoEvents
    10.         Wend
    11.     End With

    Close Socket:
    VB Code:
    1. 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
    Mik706

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147
    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.
    Mik706

  10. #10
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    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.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147
    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.
    Mik706

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width