Results 1 to 9 of 9

Thread: winsock question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Location
    Philippines
    Posts
    285

    Exclamation winsock question

    hi everyone, i have this really annoying problem. hope you guys could help me out.

    i have a multi client chat program that uses winsock. in the server side, i have an array of winsocks. once a client connects, a new instance of winsock is loaded to accept that connection request. the code looks like the example below:
    VB Code:
    1. Private Sub sckServer_ConnectionRequest(index As Integer, ByVal requestID As Long)
    2.    If index = 0 Then
    3.       intWIndex = intWIndex + 1
    4.      
    5.       Load sckServer(intWIndex)
    6.       sckServer(intWIndex).Accept requestID
    7.      
    8.       AddLog "Socket connection request accepted -> index = " & intWIndex
    9. End If
    now my problem is, in some time while the program is running, some of the winsock in the server gets this error ''Connection is aborted due to timeout or other failure" but in the client side there is no winsock error for that particular connection. i have this code to handle this error :
    VB Code:
    1. Private Sub sckServer_Error(index As Integer, ByVal Number As Integer, _
    2.    Description As String, ByVal sCode As Long, ByVal Source As String, ByVal _
    3.    HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    4.    
    5.    If index = 0 Then Exit Sub
    6.    
    7.    'close connection
    8.    sckServer(index).Close
    9.    
    10.    'unload socket
    11.    Unload sckServer(index)
    12. End Sub
    in the client side, whenever a winsock error occurs, i close the connection and i have a timer enabled so that the client would automatically try to reconnect to the server. but since no winsock error occurred, the client will not try to reconnect.

    do you guys have any idea why no error event fired in the client side? how can i work around this problem? any help would be greatly appreciated.
    Last edited by guyjasper; Jul 18th, 2005 at 04:26 AM.

  2. #2
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727

    Re: winsock question

    generally speaking it is the client who is supposed to close the connection, while servers close the connection only on timeouts.

    therefore: i would suggest to do some check on the client side on the winsock CLOSE event.

    cheers,

    wc.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Location
    Philippines
    Posts
    285

    Re: winsock question

    that was quick

    well, isn't it already handled in my error event in the server? when i close the connection from the server, doesn't the client catch this? what happens if i unload the winsock control from the server? will an error event occur in the client?

    i really need help on this one so i hope you guys can shed some light i have been trying to figure out how to resolve this for weeks already ....

  4. #4
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727

    Re: winsock question

    no errors, but the close event should fire up. as i suggested, try catching that and perform a check to see whether it is necessary or not for the client to re-open a connection.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Location
    Philippines
    Posts
    285

    Re: winsock question

    well, in my client app, i had code for the close and error events but non of them fired when i closed the connection from the server and unloaded the winsock control

    in the server side, is it a good practice to unload a winsock control that have been closed? and when the client tries to reconnect, load a new instance of the winsock control? and when a connection request is received, do i have to specify the localport for the winsock control that would accept that connection?

    thanks for the reply...

  6. #6
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727

    Re: winsock question

    Quote Originally Posted by guyjasper
    in the server side, is it a good practice to unload a winsock control that have been closed?
    yes.
    Quote Originally Posted by guyjasper
    and when the client tries to reconnect, load a new instance of the winsock control?
    yes.
    Quote Originally Posted by guyjasper
    and when a connection request is received, do i have to specify the localport for the winsock control that would accept that connection?
    i would check this out if i were you

    cheers,

    wc.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Location
    Philippines
    Posts
    285

    Re: winsock question

    i tried downloading the source code from the link you posted... well, it doesn't have any error handling routines for the winsock control.

    when a connection has been established between a client and a server, and then an error occurred in the server, what will happen to the connection now? i have searched many web sites but i haven't seen any example on how to properly handle this kind of situation. most of the examples i've found just displays the error message on a message box, and that's it. arrggghh!!!

  8. #8
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727

    Re: winsock question

    guy, as suggested:

    1. if an error occurs on the server, make it close down the connection and unload the winsock control;

    2. on the client, on the ON_ERROR and the CLOSE event make your error routine checks.

    the code was to show you how to handle your port question.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Location
    Philippines
    Posts
    285

    Re: winsock question

    yes, i already have error handling routines on my client.
    i have the ff. codes on my client:
    VB Code:
    1. Private Sub sckClient_Close()
    2.    AddLog "*** frmMain.sckClient_Close()"
    3.    
    4.    'set label
    5.    lblSocketState.Caption = "Socket Last State = 'Closed'"
    6.    lblSocketState.Refresh
    7.    
    8.    'try to reconnect to server
    9.    AddLog "tmrConnect enabled."
    10.    tmrConnect.Enabled = True
    11. End Sub
    12.  
    13. Private Sub sckClient_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)
    14.    AddLog "*** frmMain.sckClient_Error('" & Number & " - " & Description & "')"
    15.    
    16.    'set label
    17.    lblSocketState.Caption = "Socket Last State = 'Error'"
    18.    sckClient.Close
    19.    tmrConnect.Enabled = True
    20. End Sub
    21.  
    22. Private Sub tmrConnect_Timer()
    23.    sckClient.Close
    24.    sckClient.Connect RemoteHostIP, RemotePort
    25. End Sub

    i set tmrConnect interval to 2000 (2 secs). i've tested this code and it works perfectly but that was not the problem. in my program, for some reason, the error event occurred in the server side but in the client side, there was no error event it's really confusing. that is why my client doesn't try to reconnect to the server because maybe it thinks that it is still "connected" to server. while in the server, it already closed and unloaded the winsock control for that connection and is waiting for the client to try to reconnect.

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