Results 1 to 8 of 8

Thread: Sending data line by line

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2006
    Posts
    67

    Sending data line by line

    Hey all, im writing a small winsock application. Im trying to send a file line by line to a server, currently the code is supposed to do this:

    Send the text "SND" to the server, when the server receives this it stores the rest of the incoming data into a file until the client sends the text "FIN"

    on the client send im using this code:


    Code:
    Private Sub WSocksend_dataArrival(ByVal bytesTotal As Long)
    Dim indata As String
    Dim cline As String
    WSockSend.GetData indata
    indata = Replace(indata, vbLf, vbCrLf)
    TStatus.Text = TStatus.Text & indata
    If indata = "ACK" Then
    
    WSockSend.SendData ("SND")
    Open CFFile For Input As #1
    While Not EOF(1)
    Line Input #1, cline
    WSockSend.SendData cline
    Wend
    WSockSend.SendData ("FIN")
    Else
    End If
    And on the server side im using this:

    Private Sub FXListen_dataArrival(Index As Integer, ByVal bytesTotal As Long)


    Code:
    FXListen(NumSockets).GetData indata
    indata = Replace(indata, vbLf, vbCrLf)
    TConData.Text = TConData.Text & indata & vbCrLf
    If indata = "SND" Then
    Do Until indata = "FIN"
    Print #1, indata & vbCrLf
    TConData.Text = TConData.Text & indata & vbCrLf
    Loop
    Else
    End If
    
    Close #1
    
    End Sub
    Now the problem is that the server is receiving everything in one big chunk! which means its not writing it to the file because it never receives the text "SND" on its own, it receives SND and then the whole of the file and then FIN at the end which ive got it to display a text box for debugging purposes.

    Is there anyway i can get it to just send one line at a time?

    Thanks!

  2. #2
    Member
    Join Date
    Jul 2006
    Posts
    47

    Re: Sending data line by line

    Never used line input myself for transferring files as it seems to have trouble with non-printable characters like linefeed etc.

    When I want to transfer a file I usually send it in predefined chunks of data using Binary Access

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

    Re: Sending data line by line

    You have to include some sort of framing at the sender (CR, CRLF, whatever) and buffer input at the receiver, looking for the framing, and break it up into lines for processing.

    If people would stop saying "packet" they'd stop confusing people. TCP is a stream protocol, not a "packet" protocol.

    Send:

    AAA BBB CCC DDD

    Receive:

    AAABB BCCCD DD

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

    Re: Sending data line by line

    As dilettante said, you need to have "something" around each message you send, in order to enable the recieving end to know when a message ends. I allways use a ";" at the end of each message, the reciever will look for that to truncate the message read into the parts that can be "understood".
    I had the same problem, and this "method" helped!
    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!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2006
    Posts
    67

    Re: Sending data line by line

    Thanks for your help! Could someone give me an example bit of code that would do this?
    Last edited by Stanuk; Mar 7th, 2007 at 05:35 AM.

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

    Resolved Re: Sending data line by line

    Let me see, I try to add the things I would do to your code:

    Your Client code:
    (Added a ";" at the end of each line, I hope your file doesn't have this character, the idea is to have a unique character for the nd of each message that is sent.
    Code:
    Private Sub WSocksend_dataArrival(ByVal bytesTotal As Long)
    Dim indata As String
    Dim cline As String
    WSockSend.GetData indata
    indata = Replace(indata, vbLf, vbCrLf)
    TStatus.Text = TStatus.Text & indata
    If indata = "ACK" Then
       WSockSend.SendData ("SND;")
       Open CFFile For Input As #1
       While Not EOF(1)
          Line Input #1, cline
          WSockSend.SendData cline & ";"
       Wend
       WSockSend.SendData ("FIN;")
    End If
    End Sub
    On the server side you need a Sub that primarily cheks the incoming data, wether they are one or more messages.
    I'd make a Sub (Called "ReadData")that is called each time the _Dataarrival event is fired.
    Your Server code:
    Code:
    Private Sub ReadData (Message as String)
    'Message will be the String that was read from the WinSock in the dataarival event.
    Dim MessagePart() as String
    Dim i as Integer
    Dim indata as String
    MessagePart = Split(Message, ";")
    'Now all the messages that are in the big Message-chunk are split into an array.
    For i=0 to UBound (MessagePart)
       'First throw the ";" away!
       MessagePart(i)=Left(MessagePart(i),Len(MessagePart(i)-1)
       indata = Replace(MessagePart(i), vbLf, vbCrLf)
       TConData.Text = TConData.Text & indata & vbCrLf
       If indata = "SND" Then
       'You need to put the Open Statement in here
       ElseIf indata= "FIN" Then 
          Close #1
       Else
          Print #1, indata & vbCrLf
          TConData.Text = TConData.Text & indata & vbCrLf
       End If
    Next i
    End Sub
    call that Sub like:
    Code:
    Private Sub FXListen_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim Stream as String
        Stream = ""
        FXListen(Index).GetData Stream
        ReadData(Stream)
    Exit Sub
    End Sub
    Note: I didn't test these lines of code!
    Last edited by opus; Mar 7th, 2007 at 12:25 PM.
    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
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Sending data line by line

    I've attached some sample code that does this sort of thing too.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2006
    Posts
    67

    Re: Sending data line by line

    Thankyou very much for these examples guys, i shall test these out :-)

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