Results 1 to 6 of 6

Thread: connect and senddata

  1. #1
    Guest
    Hey all,

    it seems that whenever I put Winsock1.Connect and Senddata in the same sub an error occurs, is this true?

    Sunny

  2. #2
    Fanatic Member Dim's Avatar
    Join Date
    Jul 2000
    Posts
    620
    Hi,
    I have noticed that with several controls that require a connection. And i believe it's because the control doesn't wait to see if a connection has been established and rushes off to the next command being sent off (in your case senddata) If you realy have to have them under the same sub then i would suggest setting up somekind of waiting system and after the winsock1.Connect give it a couple seccods and then send the data. Or even after the winsock connect check the connection then continue. Other than that i would suggest placing them in different subs...Connect in form_load and have a command button for senddata.

    Hope that helps,
    D!m


    PS. It could also be a bigger problem with ppl on slower connections where it takes a bit of time to establish a connection. I don't know the EXACT cause for this...but it could be one of those.
    Dim

  3. #3
    Hyperactive Member
    Join Date
    May 2000
    Location
    Or
    Posts
    316
    I have noticed this problem too when using the winsock control. The error that I am assuming that you are encountering is a "state not ready" error, or something of that nature. This is because the winsock does not wait for a ready state to begin the next command:

    Here is an example of what I mean:

    Code:
    Private sub frmMain_Load()
    
    Winsock1.Connect "www.microsoft.com","23"
    
    Winsock1.SendData "Just testing"
    
    End sub
    
    
    Private sub Winsock1_DataArrival(Byval bytestotal as long)
    
    Winsock1.GetData sData, vbstring
    
    Text1.Text=Text1.Text & sData
    
    end sub
    In this example, the problem is that the program will try to execute the senddata command, while the Winsock Control is still receiving/listening for data in the DataArrival event. Dim is right. The only solution that I have ever had with this is to either, write the function using the Winsock.dll through the API or putting in a wait sequence, usually Sleep or breaking the connect and send into two separately called subs/functions.



  4. #4
    Guest

    Lightbulb

    Would having senddata in Winsock1's Connect event be a better alternative?

    Right now, I have several IPs in a combobox, and as soon as an IP is selected, winsock1 connects, when the "send" button is clicked, senddata is performed in command1's click event.

    like:

    Code:
    Private Sub Combo1_Click()
        If Combo1.Text = "" Then Exit Sub
        Call ResetConnection  'closes and re-prepares winsocks
        Winsock1.RemoteHost = frmSend.Combo1.Text
        Winsock1.Connect
    End Sub
    
    Private Sub Command1_Click()       'send
        If Combo1.Text = "" Then
            MsgBox "Select a recipient", vbOKOnly +  _   
            vbExclamation, "Error Connecting"
            Exit Sub
        End If
        Call Sending   'Sending is a sub that sends text from
    End Sub            'textbox, along with other bits, error  
                       'handling is also in the sub (ie, it has
                       'senddata in it)
    The problem with this is that if a user picks the IP before typing, the address will be in use and can't be connected to by other computers, unless a control array is used instead.

    Would this be logically better:

    Code:
    'no combo1_click event
    
    Private Sub Command1_Click()       'send
        If Combo1.Text = "" Then
            MsgBox "Select a recipient", vbOKOnly +  _   
            vbExclamation, "Error Connecting"
            Exit Sub
        End If
        Call ResetConnection  
        Winsock1.RemoteHost = frmSend.Combo1.Text
        Winsock1.Connect
    End Sub
    
    Private Sub Winsock1_Connect()
        Call Sending
    End Sub

  5. #5
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    It's because winsock is not ready to send data coz it aint connected, you need to put it in another sub and then call that sub when winsock is connected

  6. #6

    Talking umm try this

    try putting the commands in the connect event of the winsock so its like this
    Code:
    Private Sub Winsock1_Connect()
    'put your code here
    End Sub
    that way once the winsock is connected it sends the data
    it should work
    it always works for me

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