|
-
Oct 1st, 2001, 12:19 PM
#1
Thread Starter
Member
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?
-
Oct 1st, 2001, 03:00 PM
#2
Fanatic Member
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:
'Coded by [Digital-X-Treme]
Option Explicit
Private Type WSADataType
wVersion As Integer
wHighVersion As Integer
szDescription As String * 256
szSystemStatus As String * 128
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type
Private Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVR As Long, lpWSAD As WSADataType) As Long
Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
Private Declare Function closesocket Lib "wsock32.dll" (ByVal s As Long) As Long
Private mlngID As Long
Private Sub Command1_Click()
Call CloseSockManually
End Sub
Private Sub Form_Load()
Dim StartupData As WSADataType
Dim lngRet As Long
lngRet = WSAStartup(&H101, StartupData)
Debug.Print StartupData.szDescription
Winsock1.LocalPort = 80
Winsock1.Listen
Shell "C:\Program Files\Internet Explorer\IEXPLORE.EXE [url]http://127.0.0.1[/url]", vbNormalFocus
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim lngRet As Long
lngRet = WSACleanup()
End Sub
Private Sub Winsock1_Close()
MsgBox "Closed"
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
DoEvents
mlngID = requestID
Winsock1.Accept requestID
End Sub
Private Sub CloseSockManually()
Dim lngRet As Long
lngRet = closesocket(mlngID)
Call Winsock1_Close
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData
Debug.Print strData
End Sub
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)
Debug.Print Number & ": " & Description
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]
-
Oct 1st, 2001, 03:12 PM
#3
Hyperactive Member
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.)
-
Oct 3rd, 2001, 07:06 AM
#4
Thread Starter
Member
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?
-
Oct 6th, 2001, 07:10 AM
#5
Hyperactive Member
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.)
-
Oct 6th, 2001, 07:51 AM
#6
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|