Results 1 to 20 of 20

Thread: Winsock issue - Client state stuck at 6=sckConnecting

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    9

    Winsock issue - Client state stuck at 6=sckConnecting

    Hi,

    I have a single machine with 2 network adapters. I started a VB application. On my main form, I have 2 WINSOCK controls - sock1 and sock2. I have bound each control to different network adapters.

    I am trying to emulate a client-server architecture on this machine. I am new to this and followed an online tutorial. Below is the code. The problem I am having is that the connection state for my sever socket (sock1) is 7, but the connection state for my client socket (sock2) is always 6=sckConnecting.

    Please HELP !!!!

    Thanks.

    ------------------------------------------------------------------------

    Private Sub Form_Load()

    ' Close sockets is not closed
    If sock1.State <> sckClosed Then
    sock1.Close
    End If
    If sock2.State <> sckClosed Then
    sock2.Close
    End If

    ' Set protocol used for communication
    sock1.Protocol = sckTCPProtocol
    sock2.Protocol = sckTCPProtocol

    ' Set Local and Remote host properties for sock1 = server
    sock1.Bind 5014, "11.12.13.14"

    ' Set Local and Remote host properties for sock2 = client
    sock2.RemotePort = 5014
    sock2.RemoteHost = "11.12.13.14"
    sock2.Bind 5015, "11.12.13.15"

    End Sub

    --------------------------------------------------------------------

    Private Sub cmdCancel_Click()

    If sock1.State <> sckClosed Then
    sock1.Close
    End If
    If sock2.State <> sckClosed Then
    sock2.Close
    End If

    End

    End Sub

    --------------------------------------------------------------------

    Private Sub cmdStartClient_Click()

    If sock2.State <> sckClosed Then
    sock2.Close
    End If

    ' Set the client in connect mode
    sock2.Connect

    End Sub

    --------------------------------------------------------------------

    Private Sub cmdStartServer_Click()

    ' Close sockets if not closed
    If sock1.State <> sckClosed Then
    sock1.Close
    End If

    ' Set the server in listen mode
    sock1.Listen

    End Sub

    --------------------------------------------------------------------

    Private Sub sock1_ConnectionRequest(ByVal requestID As Long)

    MsgBox "Request received by client to connect. Checking current connections..."

    ' Check the state of the current socket connection. If not closed, close it
    If sock1.State <> sckClosed Then
    sock1.Close
    End If

    sock1.Accept requestID
    MsgBox "Connection accepted"

    MsgBox sock1.State
    MsgBox sock2.State - This is where it shows a state of 6
    End Sub

    --------------------------------------------------------------------

    Private Sub cmdTransmit_Click()

    ' Client sending the data
    sock2.SendData txtDataPattern.Text

    End Sub

    --------------------------------------------------------------------

    Private Sub sock1_DataArrival(ByVal bytesTotal As Long)

    Dim temp

    ' Server receiving the data
    sock1.GetData temp, vbString

    End Sub

    --------------------------------------------------------------------

    Private Sub sock2_DataArrival(ByVal bytesTotal As Long)

    Dim temp

    ' Server receiving the data
    sock2.GetData temp, vbString

    End Sub

    --------------------------------------------------------------------

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    Are the adapters connected to each other? If not, they can't see each other, so the client can't connect to the server. (It's like trying to connect to AOL when your internet connection isn't connected to your computer.)
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    9

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    Yes, the adapters are connected to each other. Also, Msgbox sock1.state is 7 (sckConnected).

    Thanks.

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    Instead of using Socket.Bind for the server, try using the .LocalPort and .Listen.

    ie:

    sock1.LocalPort = 5014
    sock1.Listen

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    9

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    If I do not use the Bind statement, then the sock1 will have the localIP of the first adapter it sees.

    The reason for using the Bind statement is that each of sock1 and sock2 are tied to different adapters.

  6. #6
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    do you try to connect a"server" and a "client" where both are in the same .exe? Is that possible?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    9

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    Yes, they both are in the same exe. I will repeat your question. Is that possible?

    Thanks

  8. #8
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    On the last question I can't help, but while we are at it:
    As far as I understand you have two networkcards/adapter on your pc, and you want the communicate from one card to the other, is that correct?
    I wouldn't evenknow how the tell a Winsock to use one or the other card!
    BuT what I'm doing is connecting my (multiple) applications on one PC via the loopbackIP (127.0.01), and I have it running!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    9

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    The way to have Winsock use one card or the other is by using the BIND method.

  10. #10
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    But Bind is used for the UDP protocoll! not for TCP!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  11. #11

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    9

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    Look here. It says use BIND for TCP applications

    http://msdn.microsoft.com/library/de...bindmethod.asp

  12. #12
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    According to the help of VB6 I have to take that back, seems like you'Re correct!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  13. #13
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    Although I still believe the version out of one .EXE won't work, I'd look for the Sock"_Connect event, does it fire?, try putting some code in there.
    I just had an issue where I had to use a Doevent loop, you could try that also
    (use a global boolean "connect", set it to False before the client tries to connect, set the Connect=true in the sock2_connect event-sub.A nd at the end of the Sub where the client tries to connect put something like:
    Code:
    Do
      DoEvents
    Loop until connect=True
    But I still believe you need to run seperate .exe (in my case I have the code for both i n one exe, but the code runs twice with a different ID set).
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  14. #14

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    9

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    I made some progress this morning.

    Using my original code, I separated the client and the server code first into 2 separate forms in the same project AND then into 2 separate programs. Both the times, I was able to establish a connection (sock.state = sckConnected = 7).

    Before I start passing data, I wanted to see the different sock properties.

    The server properties (localIP, localPort, remoteIP and remotePort) were correct before and after the connection.
    Local IP = 11.12.13.14 Remote IP = 11.12.13.15
    Local Port = 5014 Remote Port = 5015

    The client properties were correct before the connection. However, after the connection was made, the client properties were
    Local IP = 11.12.13.14 Remote IP = 11.12.13.14
    Local Port = 5015 Remote Port = 5014
    Remote IP

    Can anyone explain to me why the LocalIP for the client was correct before the connection, but changed after the connection?

    Thanks. (Special thanks to opus for suggesting me to have separate exe)

  15. #15
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    Funny thing with the IP, did you really connect the two networkcards via a cable (directly by crossed cable or via a switch or hub)?

    BTW if your question is answered please mark the tread as solved (edit original post and change the topic or put checkmark to on), also ratings are welcome!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  16. #16
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    I don't think you ever did have a problem. You were simply checking the State of sock2 before it had a chance to change!

    Adding this would have made things clearer:

    Code:
    Private Sub sock2_Connect()
        MsgBox sock2.State 'It'll show 7 here!
    End Sub
    You were caught up in sock1_ConnectionRequest, and until exiting and returning control to the message loop sock2 couldn't respond to the accepted connection and update its State value.

    You don't need 2 EXEs at all.


    In cmdStartClient_Click you Close sock2, basically terminating the Bind. Then you re-open via Connect. This picked up the "first" adapter bound to the TCP/IP protocol.

  17. #17

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    9

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    Responding to your second recommendation.

    The sequence for the client that I have in the code above is BIND - CLOSE - CONNECT. As per your recommendation, I changed to CLOSE - BIND - CONNECT.

    Using this, I am getting an error "Invalid operation at current state" when I am trying to CONNECT.

    Any other things I can be trying out?

    Thanks for your inputs.

  18. #18

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    9

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    Another interesting thing...if I change the order of my network adapter bidnings, I get the right IPs, but now, the cleint cannot connect to the server at all.

  19. #19
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    Sorry, I guess I wasn't clear about this.

    My point was that the Winsock control's Bind and Connect methods are not precisely the same as the underlying Winsock library's Bind function. What this means is that you cannot use the Bind method for a client socket through the Winsock control.

    To accomplish what you want to do you may have to use calls into the Winsock library. This may give you the ability to connect to a remote port using a specific local adapter specified by IP address.

    By doing the Bind on the client socket you're setting it up as a server connection. By Close-ing it you undo this, then when you Connect you initiate a client connection... once again using the "first" IP channel available to Windows.

    If you do that Bind on the client it will "Bind" the socket and then cause the State to go to 1 (open). You can't Connect when a socket is in this State.

  20. #20
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Winsock issue - Client state stuck at 6=sckConnecting

    Regarding your most recent post, I suspect it has something to do with routing. To establish a connection through an adapter that isn't in the default routing path you'll have to provide more complex routing directions to Windows.

    You can get an idea of the complexity by typing route print at a command prompt.

    Networking for multihomed machines can get complicated. I think you're fighting the default behaviors and I'm not sure how much payback you're going to see even if you overcome these issues.

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