Results 1 to 5 of 5

Thread: Winsock forcefully close connection

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    353

    Winsock forcefully close connection

    Hi,

    I have a problem.

    When i connect to a online server , i recieve data under DATA ARRIVAL...but sometimes the server takes a looooong time to close the connection and to trigger my second command.

    What im thinking is of a way to forcefully close the connection after i get some data in DATA ARRIVAL.

    I tried to trigger Winsock.close in data arrival but it did not help...and another problem is that dara arrival triggers many times, so it would do the winsock.close triggering manytimes, invoking my command many times (the command that is under winsock.close)

    cheers!
    Thanks for helping me out.

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Winsock forcefully close connection

    When one end does a Close on an open TCP connection it means "I am done sending." However the other end can keep its stream open and send all day long until it decides to Close from its end.

    Maybe the real question here is why the other end is not doing a Close when you expect it to. Or maybe why you care about the Close at all.

    I think what you are asking for is a Reset, and the Winsock control does not provide that option. It really isn't the best way to go about things anyhow. Usually a server keeps the connection open for a good reason. Are you trying to do something unusual here?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    353

    Re: Winsock forcefully close connection

    No, it's that i noticed that some server mosty 1% of them, even less, have that thing to keep open connections for a long long long time.

    but i think it's something that is not configured on purpose hmmm

    so basically i can't do anyhting about it but to take all the data recieved in data arrival and proceed...
    Thanks for helping me out.

  4. #4
    Member
    Join Date
    Jul 2011
    Posts
    59

    Re: Winsock forcefully close connection

    odd.. i have done a few server / client based apps. i never had a winsock connection error.


    vb Code:
    1. Option Explicit
    2. Private Type tClient
    3.     FileName As String
    4.     FileSize As Long
    5.     BytesReceived As Long
    6.         FileNum As Integer
    7. End Type
    8.  
    9. Private Clients() As tClient
    10.  
    11. Private Sub cmdConnect_Click()
    12.     If cmdConnect.Caption = "Start Listening" Then
    13.         SckReceiveFile(0).LocalPort = Val(Me.txtListenPort.Text)
    14.         SckReceiveFile(0).Listen
    15.        
    16.         cmdConnect.Caption = "Stop Connections"
    17.     Else
    18.         SckReceiveFile(0).Close
    19.         cmdConnect.Caption = "Start Listening"
    20.     End If
    21. End Sub
    22.  
    23. Private Sub Form_Load()
    24.     lstConnections.ListItems.Add , , "0"
    25. End Sub
    26.  
    27. Private Sub lstConnections_BeforeLabelEdit(Cancel As Integer)
    28.     Cancel = 1
    29. End Sub
    30.  
    31. Private Sub SckReceiveFile_Close(Index As Integer)
    32.     On Error Resume Next
    33.    
    34.     SckReceiveFile(Index).Close
    35.         Close Clients(Index).FileNum
    36.    
    37.     If Clients(Index).BytesReceived < Clients(Index).FileSize Then
    38.         Kill App.Path & "\" & Clients(Index).FileName
    39.        
    40.         Me.lstConnections.ListItems(Index + 1).SubItems(4) = "Incomplete, File Deleted"
    41.     Else
    42.         Me.lstConnections.ListItems(Index + 1).SubItems(4) = "Transfer Complete"
    43.     End If
    44.     FitTextInListView Me.lstConnections, 4, , Index + 1
    45.    
    46.     Clients(Index).FileNum = 0
    47.     Clients(Index).BytesReceived = 0
    48.     Clients(Index).FileSize = 0
    49.     Clients(Index).FileName = ""
    50. End Sub
    51.  
    52. Private Sub FitTextInListView(LV As ListView, ByVal Column As Integer, Optional ByVal Text As String, Optional ByVal ItemIndex As Long = -1)
    53.     Dim TLen As Single, CapLen As Single
    54.    
    55.     CapLen = Me.TextWidth(LV.ColumnHeaders(Column + 1).Text) + 195
    56.    
    57.     If ItemIndex >= 0 Then
    58.         If ItemIndex = 0 Then
    59.             TLen = Me.TextWidth(LV.ListItems(ItemIndex).Text)
    60.         Else
    61.             TLen = Me.TextWidth(LV.ListItems(ItemIndex).SubItems(Column))
    62.         End If
    63.     Else
    64.         TLen = Me.TextWidth(Text)
    65.     End If
    66.    
    67.     TLen = TLen + 195
    68.    
    69.     If CapLen > TLen Then TLen = CapLen
    70.    
    71.     If LV.ColumnHeaders(Column + 1).Width < TLen Then LV.ColumnHeaders(Column + 1).Width = TLen
    72. End Sub
    73.  
    74. Private Sub SckReceiveFile_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    75.     Dim K As Integer, LI As ListItem
    76.    
    77.     For K = 1 To SckReceiveFile.UBound
    78.         If SckReceiveFile(K).State = sckClosed Then Exit For
    79.     Next K
    80.    
    81.     If K = SckReceiveFile.UBound + 1 Then
    82.         Load SckReceiveFile(SckReceiveFile.UBound + 1)
    83.         ReDim Preserve Clients(SckReceiveFile.UBound)
    84.        
    85.         K = SckReceiveFile.UBound
    86.         lstConnections.ListItems.Add , , CStr(K)
    87.     End If
    88.    
    89.     SckReceiveFile(K).Accept requestID
    90.    
    91.     If Len(SckReceiveFile(K).RemoteHost) = 0 Then
    92.         Me.lstConnections.ListItems(K + 1).SubItems(2) = SckReceiveFile(K).RemoteHostIP
    93.     Else
    94.         Me.lstConnections.ListItems(K + 1).SubItems(2) = SckReceiveFile(K).RemoteHost
    95.     End If
    96.    
    97.     FitTextInListView Me.lstConnections, 2, , K + 1
    98. End Sub
    99.  
    100. Private Sub SckReceiveFile_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    101.     Dim sData As String, Pos As Long, Pos2 As Long
    102.    
    103.     SckReceiveFile(Index).GetData sData, vbString
    104.    
    105.     If Clients(Index).FileSize = 0 And InStr(1, sData, ":") > 0 Then
    106.         Pos = InStr(1, sData, ",")
    107.        
    108.         Clients(Index).FileSize = Val(Left(sData, Pos - 1))
    109.         Pos2 = InStr(Pos, sData, ":")
    110.         Clients(Index).FileName = Mid(sData, Pos + 1, (Pos2 - Pos) - 1)
    111.        
    112.         Clients(Index).FileNum = FreeFile
    113.         Open App.Path & "\" & Clients(Index).FileName For Binary Access Write Lock Write As Clients(Index).FileNum
    114.        
    115.         sData = Mid(sData, Pos2 + 1)
    116.        
    117.         Me.lstConnections.ListItems(Index + 1).SubItems(3) = Clients(Index).FileName
    118.         FitTextInListView Me.lstConnections, 3, , Index + 1
    119.     End If
    120.    
    121.     If Len(sData) > 0 Then
    122.         Clients(Index).BytesReceived = Clients(Index).BytesReceived + Len(sData)
    123.         Put Clients(Index).FileNum, , sData
    124.        
    125.         Me.lstConnections.ListItems(Index + 1).SubItems(4) = Format(Clients(Index).BytesReceived / Clients(Index).FileSize * 100#, "#0.00") & " %"
    126.         FitTextInListView Me.lstConnections, 4, , Index + 1
    127.        
    128.         If Clients(Index).BytesReceived >= Clients(Index).FileSize Then
    129.             SckReceiveFile_Close Index
    130.         End If
    131.     End If
    132. End Sub
    133.  
    134. Private Sub tmrStatus_Timer()
    135.     Dim K As Long, TmpStr As String
    136.    
    137.     For K = 0 To SckReceiveFile.UBound
    138.         TmpStr = Choose(SckReceiveFile(K).State + 1, "Closed", "Open", "Listening", "Connection pending", "Resolving host", "Host resolved", "Connecting", "Connected", "Server is disconnecting", "Error")
    139.        
    140.         If Me.lstConnections.ListItems(K + 1).SubItems(1) <> TmpStr Then
    141.             Me.lstConnections.ListItems(K + 1).SubItems(1) = TmpStr
    142.             FitTextInListView Me.lstConnections, 1, , K + 1
    143.         End If
    144.     Next K
    145. End Sub
    146.  
    147. Private Sub txtListenPort_Validate(Cancel As Boolean)
    148.     txtListenPort.Text = Val(txtListenPort.Text)
    149. End Sub

    *this is my server based code. :P hope it helps

    this is done with a timer, a textbox, listview, command button, and a winsock control

    *oh this app, can handle a ton of connections :P

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Winsock forcefully close connection

    Quote Originally Posted by batori View Post
    No, it's that i noticed that some server mosty 1% of them, even less, have that thing to keep open connections for a long long long time.

    but i think it's something that is not configured on purpose hmmm

    so basically i can't do anyhting about it but to take all the data recieved in data arrival and proceed...
    Off top of my head, but maybe something like this

    1. Attempt to close your connection and set a flag identifying that winsock's Index so that if the DataArrival occurs afterwards, you simply exit the event

    2. After attempted close, dynamically load another WinSock control and try to reconnect with that. Obviously we are talking about a control array here.

    3. Since the "closed" winsock is still loaded, you'll want to code for tracking that its slot in the control array is available for a new winsock instance down the road.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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