Results 1 to 11 of 11

Thread: Winsock connect And Send Issue :(

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    4

    Exclamation Winsock connect And Send Issue :(

    Hey all

    I'm Trying to connect With Winsock and Send Data With One Command
    I have problem to do that becouse the state connection is 6 when he try to send data
    i try to put delay timer but its not work, i alsow try to use loop and the app get stuck What can i do to Soulve This ?
    Code:
    Private Sub CmdAuto_Click()
    Dim iNumber As Integer
    Dim xxx As Integer
    Dim str As String
    
    Dim sSpilt
    iNumber = 1
    lblCaseStep.Caption = iNumber
    txtSend.Text = ""
    
    'For xxx = 1 To 100 Step 1
        Select Case iNumber
           Case 1
                
                sockMain.RemoteHost = "127.0.0.1" '"169.254.11.22"
                sockMain.RemotePort = 12345 '2000
                sockMain.Connect
                'Sleep 1000
                'Delay 5
                iNumber = 5
                lblCaseStep.Caption = iNumber
                
          Case 5
          
                If sockMain.State = 7 Then
                    iNumber = 10
                    lblCaseStep.Caption = iNumber
                End If
                
           Case 10
             lblCaseStep.Caption = iNumber
              Open FilePath.Text For Input As #1
               Do While Not EOF(1)
                    Line Input #1, str
                    Text1.Text = Text1.Text & str & vbCrLf
                    sSpilt = Split(str, ":")
                    If sSpilt(0) = "Length" Then
                        txtLength.Text = sSpilt(1)
                    ElseIf sSpilt(0) = "Quantity" Then
                        txtQuntity.Text = sSpilt(1)
                    End If
                Loop
                If EOF(1) Then
                    txtSend.Text = txtLength.Text & vbCrLf & txtQuntity.Text
                    iNumber = 15
                    Close #1
                End If
            Case 15
                sockMain.SendData "111"
                iNumber = 20
        End Select
        Delay 1
    'Next xxx
    End Sub
    Last edited by Hack; Apr 7th, 2013 at 11:37 AM. Reason: Added code tags

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Winsock connect And Send Issue :(

    Moved from the Codebank (which is for sharing code rather than posting questions )

  3. #3
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Winsock connect And Send Issue :(

    Make iNumber static:

    Static iNumber As Integer

    Dimensioning iNumber causes it to be set to 0 each time you click on the button so you need to make sure it's value stays constant as to what you set it to in your code so you have to declare it as a static variable

    Remove If EOF(1).......End If. You don't need the If and End If but keep the code in it. You know that it is EOF(1) because you have left the loop above it therefore it is not necessary to check if it is EOF(1).

    'If EOF(1) Then
    txtSend.Text = txtLength.Text & vbCrLf & txtQuntity.Text
    iNumber = 15
    Close #1
    ' End If
    Last edited by jmsrickland; Apr 7th, 2013 at 12:02 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Winsock connect And Send Issue :(

    Making the var static is not going to help much. Only one Case will execute per click so you would have to click the button several times to get the one you want to execute.

    You will not be able to connect and send with one command since at the very least this is 2 commands 1 to connect and 1 to send.

    You can however do it with one click of one button if you write the proper commands behind it.

    You first need to issue the connection request and then you need to give it a moment before you try to send data.
    One simple way to know when you are connected is have the server send something back when it accepts the connection request and then in your data arrival event you check for that value and if it is received you know you are connected and it is ok to send data. No need for a loop, no need for any artificial delay. I can only guess what delay 1 is but most likely it is something that should not be used.

  5. #5
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Winsock connect And Send Issue :(

    It depends on your point of view.

    I interpreted his request as to connect and send using one Command button, not one click of a command.

    I wasn't trying to solve his problem completely as I only noticed that he first needs to make the variable static. As far as the If statement he can use it or omit it. The only other thing that he needs is to check the variable so that he doesn't set it to 1 each time he clicks the button so he needs something like this:

    Code:
     If iNumber < 1 Then
       iNumber = 1
     End If
    Assuming his other code is correct (I'm not even checking for that) about the file and in the manner as I assumed his request to be based on his first statement:

    I'm Trying to connect With Winsock and Send Data With One Command

    One command and not one click of one command his code will work if he makes the changes I suggested.

    Maybe he really meant one click of one command but he didn't say that.

    If he wants to do this with one click of a command then that is a different story and of course different code
    Last edited by jmsrickland; Apr 7th, 2013 at 01:35 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

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

    Re: Winsock connect And Send Issue :(

    To paraphrase Obi-Wan:
    Trust the Events, Luke!
    Busy-wait polling Winsock.State will end up taking you off into the weeds of DoEvents, Sleep, and clever (and flaky) alternatives.

    Once you call the Connect method in your Command Button Click event handler just exit. Then write a handler for the Winsock control's Connect event. From there you can begin sending.

    If you need to do more than one SendData call you'd use the Winsock control's SendComplete event to "pump" your sending process:

    Code:
    Command_Click:
        Do preparation.  Open file, etc.
        Winsock.Connect remoteHost, remotePort
    
    Winsock_Connect:
        Send first chunk of output.
    
    Winsock_SendComplete:
        If more chunks:
            Send next chunk
        Else:
            Winsock.Close
            Close file, clean up, report finished, etc.
    You end up not needing any loop to send chunks of output, the events replace that.

    Note that normally you are sending "messages" of a sort as chunks and these need some kind of length header or delimiting trailer so that the other end can figure out where the message boundaries are.

    One SendData does not equal one GetData at the other end except in cases where you get lucky. It isn't like using Print # statements where the line-ending delimiter (CRLF) gets appended for you. At the other end it isn't like Line Input # either, and data can arrive in pieces of "random" lenth (even as few as 1 character at a time).
    Last edited by dilettante; Apr 7th, 2013 at 01:47 PM.

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    4

    Red face Re: Winsock connect And Send Issue :(

    Thank You for all comments
    i am not so good with VB i think i need to learn from the begening becouse i have a lot of mess i my head becouse the logic work diffrent then PLC
    Any way the problem was soulved when i put DoEvents in the Do loop
    Thanks for your Help

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Winsock connect And Send Issue :(

    Quote Originally Posted by ShayCasper View Post
    Thank You for all comments
    i am not so good with VB i think i need to learn from the begening becouse i have a lot of mess i my head becouse the logic work diffrent then PLC
    Any way the problem was soulved when i put DoEvents in the Do loop
    Thanks for your Help
    You have got to be kidding. DoEvents or no DoEvents your code as is wont work


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    4

    Re: Winsock connect And Send Issue :(

    This Is the Code And Its Work Fine
    - Connect
    - Read The File
    - Send Data
    Code:
    Private Sub CmdAuto_Click()
    Dim iNumber As Integer
    Dim xxx As Integer
    Dim str As String
    Dim sSpilt
    
    iNumber = 1
    lblCaseStep.Caption = iNumber
    txtSend.Text = "sockMain.State"
      Delay 1
    
    
    Do
    DoEvents
        Select Case iNumber
           Case 1
           
                If sockMain.State = 0 Then
                sockMain.RemoteHost = "127.0.0.1" '"169.254.11.22"
                sockMain.RemotePort = 12345 '2000
                sockMain.Connect
                ElseIf sockMain.State = 7 Then
                    iNumber = 10
                    lblCaseStep.Caption = iNumber
                End If
            
      
           Case 10
             
              Open FilePath.Text For Input As #1
               Do While Not EOF(1)
                    Line Input #1, str
                    Text1.Text = Text1.Text & str & vbCrLf
                    sSpilt = Split(str, ":")
                    If sSpilt(0) = "Length" Then
                        txtLength.Text = sSpilt(1)
                    ElseIf sSpilt(0) = "Quantity" Then
                        txtQuntity.Text = sSpilt(1)
                    End If
                Loop
                If EOF(1) Then
                    txtSend.Text = txtLength.Text & vbCrLf & txtQuntity.Text
                    iNumber = 15
                    lblCaseStep.Caption = iNumber
                    Close #1
                End If
                
            Case 15
            
                sockMain.SendData txtSend.Text
                iNumber = 20
                lblCaseStep.Caption = iNumber
                
        End Select
        
    Loop Until iNumber = 20
    End Sub

  10. #10
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Winsock connect And Send Issue :(

    OK, it works, but I don't understand what your point is


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  11. #11

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    4

    Re: Winsock connect And Send Issue :(

    The Point is thaty before i added the "DoEvents" it was Stuck all the time

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