Results 1 to 6 of 6

Thread: Winsock - Problem closing ports

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 1999
    Posts
    51

    Winsock - Problem closing ports

    I'm making a simple chat application using a Winsock TCP connection. It's working fine however when I close the two peer-peer clients and then restart them again, I get a "10048 Address in Use" message. If I run the "netstat" command from the command prompt, I can see that the port I am using is still open. I use the winsock.close method in my attempts to close the connection, but it always leaves one of the peers still listening on a port, even though I call the close method in both peers. After a few minutes, the TCP/IP stack automatically removes that port, but this is an unacceptable waiting period. Right now I'm running both peers on the same computer, using the loopback 127.0.0.1 address. Any ideas on how I can properly close the ports of both clients?

  2. #2
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    Smile Code...

    The following information may not work Basically, i tried to implement closing the Winsock control's connection via the API using the requestID and the closesocket Winsock API function. I thought this might be a more direct method of closing the connection... and the socket might not linger for so long.

    On my system, it succeeded in closing the connection, but i don't know if it will solve your problem of the socket lingering... Give it a try. If it works, nice one, if not, sorry i couldn't be of help.
    VB Code:
    1. 'Coded by [Digital-X-Treme]
    2. Option Explicit
    3.  
    4. Private Type WSADataType
    5.     wVersion As Integer
    6.     wHighVersion As Integer
    7.     szDescription As String * 256
    8.     szSystemStatus As String * 128
    9.     iMaxSockets As Integer
    10.     iMaxUdpDg As Integer
    11.     lpVendorInfo As Long
    12. End Type
    13.  
    14. Private Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVR As Long, lpWSAD As WSADataType) As Long
    15. Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
    16. Private Declare Function closesocket Lib "wsock32.dll" (ByVal s As Long) As Long
    17.  
    18. Private mlngID As Long
    19.  
    20. Private Sub Command1_Click()
    21. Call CloseSockManually
    22. End Sub
    23.  
    24. Private Sub Form_Load()
    25.  
    26.     Dim StartupData As WSADataType
    27.     Dim lngRet As Long
    28.    
    29.     lngRet = WSAStartup(&H101, StartupData)
    30.     Debug.Print StartupData.szDescription
    31.    
    32.     Winsock1.LocalPort = 80
    33.     Winsock1.Listen
    34.     Shell "C:\Program Files\Internet Explorer\IEXPLORE.EXE [url]http://127.0.0.1[/url]", vbNormalFocus
    35. End Sub
    36.  
    37. Private Sub Form_Unload(Cancel As Integer)
    38.     Dim lngRet As Long
    39.     lngRet = WSACleanup()
    40. End Sub
    41.  
    42. Private Sub Winsock1_Close()
    43.     MsgBox "Closed"
    44. End Sub
    45.  
    46. Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    47.     Winsock1.Close
    48.     DoEvents
    49.     mlngID = requestID
    50.     Winsock1.Accept requestID
    51. End Sub
    52.  
    53. Private Sub CloseSockManually()
    54.     Dim lngRet As Long
    55.     lngRet = closesocket(mlngID)
    56.     Call Winsock1_Close
    57. End Sub
    58.  
    59. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    60.     Dim strData As String
    61.     Winsock1.GetData strData
    62.     Debug.Print strData
    63. End Sub
    64.  
    65. Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    66.     Debug.Print Number & ":    " & Description
    67. End Sub

    Laterz
    Digital-X-Treme
    Contact me on MSN Messenger: [email protected]

    [VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
    / (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]

  3. #3
    Hyperactive Member TiPeRa's Avatar
    Join Date
    Apr 2001
    Location
    In between
    Posts
    464
    When you close the handle to a socket, some additional negotiation goes on between the client and the server. The socket will wait for up to two times the maximum time that windows would wait to receive an acknowledgement from the other end of the socket that closed the port. By default, this option is set to two minutes. Therefore, Windows may wait up to four minutes before the port is actually released.

    This makes that specific port unavailable until it is actually released.

    RESOLUTION:
    The only workaround is to not use a specific local port. If you set the LocalPort property to Zero, Winsock will pick a random local port for you and use it until a Close method is called on the WinSock Control.
    From MSDN Library
    W#Ć€V€® W¦|| ߀ W¦|| ߀, ÄÑÐ †#€®€ ¦§ ÑÖ†#¦Ñ6 ¥Öµ ©ÄÑ ÐÖ ÄßÖµ† ¦†, §Ö §¦† ßÄ©K, ®€|ÄX ÄÑÐ |€† ¦† #ÄÞÞ€Ñ.
    (Whatever will be will be, and there is nothing you can do about it, so sit back, relax and let it happen.)

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 1999
    Posts
    51
    Well, selecting no local port worked for me but there is a problem with doing this when designing a two-way chat communication program. Each peer always needs to know the remote port of the other peer before communication is even established, correct? Therefore I NEED to specify the localport setting on both of the peers?

  5. #5
    Hyperactive Member TiPeRa's Avatar
    Join Date
    Apr 2001
    Location
    In between
    Posts
    464
    That is what I thought, but my chat program still worked, which REALLY suprised me!!!
    W#Ć€V€® W¦|| ߀ W¦|| ߀, ÄÑÐ †#€®€ ¦§ ÑÖ†#¦Ñ6 ¥Öµ ©ÄÑ ÐÖ ÄßÖµ† ¦†, §Ö §¦† ßÄ©K, ®€|ÄX ÄÑÐ |€† ¦† #ÄÞÞ€Ñ.
    (Whatever will be will be, and there is nothing you can do about it, so sit back, relax and let it happen.)

  6. #6
    Lively Member
    Join Date
    May 2001
    Location
    Jordan\Amman
    Posts
    84
    I have a perfectly working Server \Client Application ,Adn the server can force the Client to close

    I agree that The close method needs time ..but this is usually less than 5 seconds

    When I want to close connection to a client ,I send a command to the client "A Code" ,and the client understands that I want it to close and will start performing the "Close" method ,and on the server sid I use the following Mechanism to make sure that the Client is closed before declaring him as Close




    TCPServer(Index).Close 'Index is the pointer to the client that
    I want to close (I am usign an array
    of Winsock Control

    DoEvents
    While TCPServer(Index).State <> sckClosed
    DoEvents
    Wend
    ListAdd "Closing Remote connection." ' Adding a promot in a
    list about closure
    Palestine will be free again ,and someday all the unjustice will be from the history.

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