|
-
Mar 6th, 2007, 04:38 AM
#1
Thread Starter
Lively Member
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!
-
Mar 6th, 2007, 09:47 AM
#2
Member
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
-
Mar 6th, 2007, 06:51 PM
#3
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
-
Mar 6th, 2007, 11:28 PM
#4
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!
-
Mar 7th, 2007, 04:16 AM
#5
Thread Starter
Lively Member
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.
-
Mar 7th, 2007, 12:22 PM
#6
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!
-
Mar 7th, 2007, 05:51 PM
#7
Re: Sending data line by line
I've attached some sample code that does this sort of thing too.
-
Mar 8th, 2007, 04:11 AM
#8
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|