Results 1 to 28 of 28

Thread: [RESOLVED] Winsock wont connect (Resolved)

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [RESOLVED] Winsock wont connect (Resolved)

    I am having a problem with winsock in my app, I can connect to the sever when I test it using my demo but when I use the actual application it wont connect. Below is the code I am using to connect to the server:

    VB Code:
    1. Private Sub Command2_Click()
    2. messcon.Connect txtConnect, 7000
    3. If messcon.State = sckConnected Then
    4. Command2.Enabled = False
    5. Else
    6. MsgBox (LoadResString(68 + i))
    7. End If
    8. End Sub

    I keep getting the please connect message showing up. It worked find before I added this code:

    VB Code:
    1. Private Sub Command3_Click()
    2. If Not txtAttachment.Text = "" Then
    3. NF = FreeFile
    4. Open cmdAttach.FileName For Input As #NF
    5. Input #NF, data
    6. fname = cmdAttach.FileTitle
    7. messcon.SendData "|DATA|" & data & fname
    8. Close #NF
    9. End If
    10. End Sub
    11.  
    12. Private Sub Messlis_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    13. If InStr(1, str, "|DATA|") <> 0 Then
    14.  NF = FreeFile
    15.  MkDir (App.Path & "\Received Files\")
    16.  Open App.Path & "\Received Files\" & fname For Output As #NF
    17.  Print #NF, data
    18.  Close #NF
    19.  End If
    20. End Sub

    TIA

    Nightwalker83
    Last edited by Nightwalker83; Mar 13th, 2003 at 06:30 PM.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  2. #2
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    What protocol u r using? and what is the description of error.....?
    please elaborate!

  3. #3

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    1. TCP Protocol

    2. It's not an error message just a message thats part of my application.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    Still not enough information to reach to any conclusion...
    well.. if using TCP, have u made another winsock listening to your connect request? and also.. the winsock which is in listening mode cannot accept the connection request..

  5. #5

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    VB Code:
    1. Private Sub Form_Load()
    2. On Error Resume Next
    3. intmax = 0
    4. Messlis(intmax).LocalPort = 7000
    5. Messlis(intmax).Listen
    6. End Sub

    This is the code I am using for the connection request:

    VB Code:
    1. Private Sub Messlis_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    2. 'Allow the chat app to accept more than one connection
    3. If Index = 0 Then
    4.       intmax = intmax + 1
    5.     Load Messlis(intmax)
    6. Messlis(intmax).Accept requestID
    7. End If
    8. If Messlis(intmax).State = sckError Then
    9. MsgBox (LoadResString(67 + i))
    10. End If
    11. End Sub

    This code was working before so I don't think this is the problem since I didn't change it.
    Last edited by Nightwalker83; Mar 11th, 2003 at 06:16 AM.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    and the code in ConnectionRequest event...Where u are accepting the request...????

  7. #7

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    What do you mean?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  8. #8
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176
    The connection request is in the post above yours you missed it


    Post the whole project if you can
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  9. #9
    Junior Member Guile.NET's Avatar
    Join Date
    Mar 2003
    Location
    www.voodoochat.com
    Posts
    31
    What is the error message?
    The arrow shot by the archer may, or may not, kill a single person. However, stratagems devised by a wise man, can kill even babes in the womb.

  10. #10
    Addicted Member
    Join Date
    Jul 2001
    Location
    Maine
    Posts
    214
    He has one winsock control listening (index = 0) and then he creates a new winsock control for every new user. -Note: You don't have to use something like intMax, just use messlis.UBound when creating new winsock controls-

    However, as described on winsockvb.com, you are going to have to create a boolean array along with your winsock controls, and replace your code with this under connection_request.

    Code:
    for i = messlis.LBound to messlis.ubound
    if Connection(i) = False then 'There is an empty winsock control..
    messliss(i).accept requestID
    connection(i) = true 'Set the boolean to true, it is taken
    exit sub
    end if
    next i
    
    If Index = 0 Then
          intmax = intmax + 1
        Load Messlis(intmax)
    Messlis(intmax).Accept requestID 
    End If

  11. #11

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    Originally posted by jeb2022
    Code:
    for i = messlis.LBound to messlis.ubound
    if Connection(i) = False then 'There is an empty winsock control..
    messliss(i).accept requestID
    connection(i) = true 'Set the boolean to true, it is taken
    exit sub
    end if
    next i
    I tried this code but it up with a Sub or Function not defined error on this line:

    VB Code:
    1. if Connection(i) = False then
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  12. #12
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176
    Private Connection(200) as boolean

    This will just inform your app as to wether your winsock is in use.

    you need to check through all currently loaded winsocks to see if they have a connection established, if not then the user on that winsock has left and it is free for use. The boolean indicates which are in use and which are not.
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  13. #13
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176
    NOOOOOOOOOOOOO


    in command2_click event you cant have

    Code:
    If messcon.State = sckConnected Then
    Command2.Enabled = False
    Else
    MsgBox (LoadResString(68 + i))
    End If
    Wait for the connect event to fire on the winsock control. That will indicate when you are connected.

    The winsock is still trying to connect when you display your message but hasnt actually completed connecting
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  14. #14
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    The statements after connect were probably immediately fired before the connection negotiation finished. I had the same problem and I also return a value (boolean in my case), you can also try something like this.

    VB Code:
    1. 'blah blah
    2. Attempt_Connect:
    3.     frmCommon.sckTCP(SocketIndex).Close
    4.     frmCommon.sckTCP(SocketIndex).LocalPort = 0
    5. On Error GoTo Error_Handler
    6.     frmCommon.sckTCP(SocketIndex).Connect Trim(strIPAddress)    'Connect attempt
    7. On Error GoTo 0
    8.    
    9.     Count_Pause = 0
    10.     Do Until Count_Pause >= MAX_Retry
    11.         If frmCommon.sckTCP(SocketIndex).State = sckConnected Then
    12.             Count_Pause = MAX_Retry
    13.         Else
    14.             Call Pause(15)
    15.             Count_Pause = Count_Pause + 1
    16.         End If
    17.     Loop
    18.    
    19.     If frmCommon.sckTCP(SocketIndex).State <> sckConnected Then
    20.         GoTo Error_Handler  'Retry connecting while Count_Connect < 3
    21.     End If
    22.    
    23.     ConnectSocket_sckTCP = True
    24.     GoTo Exit_Proc
    25.  
    26. Error_Handler:
    27.  
    28.     Count_Connect = Count_Connect + 1
    29.     If Count_Connect >= MAX_Retry Then
    30.         'blah blah
    31.          GoTo Exit_Proc
    32.     Else
    33.         GoTo Attempt_Connect    'Retry connect.
    34.     End If
    35.  
    36. Exit_Proc:
    37.  
    38. End Function

    BTW, I used Exit_Proc because is still had some statements which I did not include here. Essentially, you do a loop, up to three times, to check the state of the socket.

    Pause() is a user defined pause using GetTickCount with DoEvents in it. I noticed that when I set pause to 10 milliseconds the loop occurs twice before the socket state becomes connected. When I changed it to 15 milliseconds, I passed through the loop only once.
    Last edited by leinad31; Mar 12th, 2003 at 03:30 AM.

  15. #15
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176
    This will not work for slow connections (post Above)

    you are both making it more complicated than neccassary

    When using winsock ensure you have Service Pack 5 Installed for VB
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  16. #16
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Actually, the Connect event fires first. But I went to the trouble of the loop so that my Connect_sckTCP() procedure will return a correct value. Here's an excerpt from the procedure trace I did:


    16:31:57 - 0000000014 START ConnectSocket_sckTCP(1, 202.81.179.144).[/B]
    16:31:57 - 0000000015 START CheckIfExist_sckTCP(1, ).
    16:31:57 - 0000000016 RETURNED CheckIfExist_sckTCP(1, ) = 1
    16:31:57 - 0000000017 CONNECT frmCommon.sckTCP(1) TO 202.81.179.144 'This is right before I use connect method.
    16:31:57 - 0000000018 START Pause(15).
    16:31:57 - 0000000019 START frmCommon.sckTCP_Connect(1). 'This is the Winsock event.
    .
    .
    frmCommon.sckTCP_ConnectionRequest(0, 1040). 'Because I did a loop connect.
    .
    .
    16:31:57 - 0000000041 RETURNED ConnectSocket_sckTCP = True
    Last edited by leinad31; Mar 12th, 2003 at 03:45 AM.

  17. #17

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    Originally posted by BodwadUK
    NOOOOOOOOOOOOO


    in command2_click event you cant have

    Code:
    If messcon.State = sckConnected Then
    Command2.Enabled = False
    Else
    MsgBox (LoadResString(68 + i))
    End If
    Wait for the connect event to fire on the winsock control. That will indicate when you are connected.

    The winsock is still trying to connect when you display your message but hasnt actually completed connecting
    I replaced the code you mentioned with:

    VB Code:
    1. Private Sub Command2_Click()
    2. messcon.Connect txtConnect, 7000
    3. If messcon.State = sckConnected Then
    4. Command2.Enabled = False
    5. Else
    6. Exit Sub
    7. End If
    8. End Sub

    My prog seem to work fine now.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  18. #18
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Originally posted by BodwadUK
    This will not work for slow connections (post Above)

    you are both making it more complicated than neccassary

    When using winsock ensure you have Service Pack 5 Installed for VB
    True, but I'll be using it only on a LAN. Besides, my app needs a fast connection so if didn't connect because of the lag then its not worth connecting anyway. The slow connect will defeat the purpose.

  19. #19
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Originally posted by Nightwalker83
    I replaced the code you mentioned with:

    VB Code:
    1. Private Sub Command2_Click()
    2. messcon.Connect txtConnect, 7000
    3. If messcon.State = sckConnected Then
    4. Command2.Enabled = False
    5. Else
    6. Exit Sub
    7. End If
    8. End Sub

    My prog seem to work fine now.
    You can just as well replace it with:
    VB Code:
    1. Private Sub Command2_Click()
    2.     messcon.Connect txtConnect, 7000
    3. End Sub
    because there is no way the socket connects that fast that you can check for it in the next line of code. Not even in a LAN.
    If you want to disable Command1 after the connection succeeded use something like:

    VB Code:
    1. Private Sub messcon_Connect()
    2.     Command1.Enabled = False
    3. End Sub
    Frans

  20. #20

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    Originally posted by Frans C
    You can just as well replace it with:
    VB Code:
    1. Private Sub Command2_Click()
    2.     messcon.Connect txtConnect, 7000
    3. End Sub
    because there is no way the socket connects that fast that you can check for it in the next line of code. Not even in a LAN.
    If you want to disable Command1 after the connection succeeded use something like:

    VB Code:
    1. Private Sub messcon_Connect()
    2.     Command1.Enabled = False
    3. End Sub
    That code isn't working.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  21. #21
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176
    it is actually connecting isnt it
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  22. #22

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    Nope
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  23. #23
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176
    You have got a socket listening as a server havent you????
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  24. #24

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    Yes as I said it was working before prefectly alright before I made the changes. I didn't not change anything in the Messcon or Messlis connection events so I don't know what the problem could be.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  25. #25
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    Doevents maybe?

    You'd be surprised how many times that can solve a problem.
    Don't Rate my posts.

  26. #26
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Originally posted by Pc_Madness
    Doevents maybe?

    You'd be surprised how many times that can solve a problem.
    You'de be surprised how many times it can CAUSE a problem as well, especially if you use it in winsock events (DataArrival for instance).
    Frans

  27. #27
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    I tried your app and the connection works fine. Only change I made was remove the code that disabled the connect button if the entered IP address is equal to the localIP. That way I could do a local connect and it worked fine except for the popping message.

    The message pops up because you immediately did a compare on the socket state though it hasn't changed yet. But when I checked the state of the socket it was already connected and I could send messages.

  28. #28

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    Originally posted by leinad31
    I tried your app and the connection works fine. Only change I made was remove the code that disabled the connect button if the entered IP address is equal to the localIP. That way I could do a local connect and it worked fine except for the popping message.


    I removed the following code and it seems to be working.

    VB Code:
    1. If txtConnect.Text = messcon.LocalIP Then
    2. Command2.Enabled = False
    3. Command4.Enabled = False
    4. Else
    5. Command2.Enabled = True
    6. Command4.Enabled = True
    7. End If

    The message pops up because you immediately did a compare on the socket state though it hasn't changed yet. But when I checked the state of the socket it was already connected and I could send messages.
    Yeah I used this code to check for the connection state:

    VB Code:
    1. Private Sub Timer2_Timer()
    2. Select Case messcon.State
    3. Case "7"
    4. Me.Caption = Me.Caption & " - Connected"
    5. Case "8"
    6. Me.Caption = Me.Caption & " - Connection was closed"
    7. End Select
    8. End Sub

    I had all the connection states but Messcon proberly wouldn't have encounted them.
    Last edited by Nightwalker83; Nov 16th, 2003 at 07:24 PM.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

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