dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Error 40006 wrong protocol or connection state....

  1. #1
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,264

    Error 40006 wrong protocol or connection state....

    Hello guys! Yes it's me again....
    I'm using TCP
    I get this error in run time
    Both computers are "connected" by clicking the Connect button on each, but when it is about to send the data (txtSend_LostFocus), it gives me this error.
    Why?
    How can I fix this?

    Here is my code for the server
    Code:
    Private Sub Form_Load()
        tcpClient2.RemoteHost = "Cmdupreez"
        tcpClient2.RemotePort = 1001
    End Sub
    
    Private Sub cmdConnect_Click()
        tcpClient2.Connect
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    tcpClient2.Close
    End Sub
    
    Private Sub tcpClient2_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)
    MsgBox Number & Description
    End Sub
    
    Private Sub txtSend_LostFocus()
        tcpClient2.SendData txtSend.Text
    End Sub
    
    Private Sub tcpClient2_DataArrival _
    (ByVal bytesTotal As Long)
        Dim strData As String
        tcpClient2.GetData strData
        txtOutPut.Text = strData
    End Sub
    and here is the code for the client
    Code:
    Private Sub Form_Load()
        tcpClient.RemoteHost = "HANNES"
        tcpClient.RemotePort = 1001
    End Sub
    
    Private Sub cmdConnect_Click()
        tcpClient.Connect
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    tcpClient.Close
    End Sub
    
    Private Sub txtSend_LostFocus()
        tcpClient.SendData txtSend.Text
    End Sub
    
    Private Sub tcpClient_DataArrival _
    (ByVal bytesTotal As Long)
        Dim strData As String
        tcpClient.GetData strData
        txtOutPut.Text = strData
    End Sub
    Enjoy your day!
    Hannes

  2. #2
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766
    I have a project running using WINSOCK, with differing results, on my computers at home it works fine, on the servers in work it keeps getting time out errors so I may not be the best person to help here.

    However, if I remember correctly the server should be listening for the client, you use the property winsock.listen.

    Then when the client does winsock.connect, it fires off the connection request in your server program.

    Unless you are using a UDP protocol, in which case both computers have to bind the connection. I don't have access to the code here, so can't give definite answers but I'm 99% certain about this.

    HTH
    JP

    Please remember to rate all postings.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,264
    Hi! thanx for the reply!

    I've tried this as well
    Code:
    Private intMax As Long
    Private Sub Form_Load()
       intMax = 0
       tcpServer(0).LocalPort = 1001
       tcpServer(0).Listen
    End Sub
    
    Private Sub tcpServer_DataArrival _
    (Index As Integer, ByVal bytesTotal As Long)
        Dim strData As String
        tcpServer(intMax).GetData strData
        txtOutPut.Text = strData
    End Sub
    
    Private Sub tcpServer_ConnectionRequest _
    (Index As Integer, ByVal requestID As Long)
       If Index = 0 Then
          intMax = intMax + 1
          Load tcpServer(intMax)
          tcpServer(intMax).LocalPort = 0
          tcpServer(intMax).Accept requestID
       End If
    End Sub
    
    Private Sub txtSend_LostFocus()
        tcpServer(intMax).SendData txtSend.Text
    
    End Sub
    But still, I get the same error

  4. #4
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766
    In the server program, connection event you need to accept the request.
    JP

    Please remember to rate all postings.

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    would that be what this line is for:

    tcpServer(intMax).Accept requestID


    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  6. #6
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766

    Red face

    Emm yes sorry I missed that.

    Do you need to have the following property set for the inital socket (i.e. index 0):
    .Protocol = sckTCPProtocol

    What error are you getting?

    Is it possible that a). something else is using port 1001, or b). that it is blocked by a firewall.

    Have you any code in the socket error event, and if so what is that telling you?

    Which side is giving you the error: Client or Server?
    JP

    Please remember to rate all postings.

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,264
    Morning!
    Thanx for alll the replies..
    Quote Originally Posted by jp140768

    Do you need to have the following property set for the inital socket (i.e. index 0):
    .Protocol = sckTCPProtocol

    What error are you getting?

    Is it possible that a). something else is using port 1001, or b). that it is blocked by a firewall.

    Have you any code in the socket error event, and if so what is that telling you?

    Which side is giving you the error: Client or Server?
    1) I have set the .Protocol Property to sckTCPProtocol
    2) I still get Error 40006
    3) I don't know if something else is using the port (perhaps a printer) - if that's the case, should I use a different port number┐
    4) I configured the firewall settings, to always allow my program
    5) I get the error on both sides.


    Thanx again for the replies
    HannesTheNotSoGreat

  8. #8
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766
    Have you tried running both programs on the same computer? All you need to do in the client is change the remote host to have your computer's IP address.

    This way you can run both progams in the development / debug environment and identify where they're getting a problem.

    I don't know a lot about firewalls, but have you tried running on two computers inside the firewall - i.e. can we eliminate the firewall or point to it as the culprit.

    Your code seems very similar to mine, the only thing I do different is in the client connection event, send a message to the server (this was something someone suggested when I first tried this program).
    JP

    Please remember to rate all postings.

  9. #9
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,264
    Hi JP!
    Guess what it, works!
    I finally figured out that the client(only) is supposed to connect to wherever, the server doesn't need to, because it just needs to accept the request.
    So.. I'm going to make 2 programs for each of the computers - one program to connect and send messages, the other just to receive the messages
    It all actually makes sense now
    PS. another thing that stuffed me around was the Tabindex Property
    So I ended up making the connect button's TabIndex 0
    Thanks for all your help!
    I'll email you a couple of beers sometime!

    HannesTheReleivedAndHappy

  10. #10
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766

    Talking

    Thats great news, your second code (post 3), more resembles mine and I didn't see a Connect in the server there.

    I think if you use the UDP protocol, you could avoid needing two progams - although maybe I didn't read that properly.
    JP

    Please remember to rate all postings.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)