|
|
Home » Community » Newbie corner » Socket Communication 101
Socket Communication 101 [message #38236] |
Fri, 07 December 2012 18:02  |
nejnadusho
Messages: 60 Registered: October 2012
|
Member |
|
|
Hi,
I need to figure out sockets.
And I have no much knowledge in that.
I run the reference examples and the communication works great and I understood what is going on both sides.
However, I still cannot imagine/assimilate how to transfer large chunks of data.
For example:
If a query to a database is returning an array of data how do I send it to the client side as one chunk?
Also how can I lable/mark that chunk to which class in my application is the receiver?
I already and I assume, but not sure, I can send an array with RawSend/RawRecv, then shoud I initialize/end RawOpen/RawClose the connection in a specific way?
Thank you very much.
Best,
Georgi
P.S. Please let me know if I should try to clarify myself.
|
|
|
|
|
|
Re: Socket Communication 101 [message #38243 is a reply to message #38242] |
Fri, 07 December 2012 19:45   |
nlneilson
Messages: 644 Registered: January 2010 Location: U.S. California. Mojave &...
|
Contributor |
|
|
sock.Put("-- BEGIN FRUIT LIST --\n");
for(int i = 0; i < FruitList.GetCount(); i++)
sock.Put(FruitList[i] + '\n');
sock.Put("-- END FRUIT LIST --\n");
Each sock.Put would be a different connect/disconnect, not good.
Add all the char to a single string or buf with the \0 at the end and send that.
One connect/disconnect
[Updated on: Fri, 07 December 2012 19:48] Report message to a moderator
|
|
|
|
Re: Socket Communication 101 [message #38246 is a reply to message #38236] |
Fri, 07 December 2012 20:41   |
lectus
Messages: 329 Registered: September 2006 Location: Brazil
|
Senior Member |
|
|
Since it's using TCP protocol, I believe there's a connection alive.
If you used UDP then yes, it doesn't use connections.
I think what you mean is that you can send it all in ONE chunk. Yes, it's also possible.
Maybe something like this:
String acc;
for(int i=0; i < FruitList.GetCount(); i++)
{
acc += FruitList[i];
acc += "-";
}
acc += '\n';
sock.Put(acc);
Then on the other side you can do:
String line = sock.GetLine();
String fruit;
for(int i=0;i < line.GetLength(); i++)
{
if (line[i] != '-')
fruit += line[í];
else
{
FruitList.Add(fruit);
fruit="";
}
}
Data gets sent like this:
Quote: | Apple-Banana-Orange
|
[Updated on: Fri, 07 December 2012 20:45] Report message to a moderator
|
|
|
Re: Socket Communication 101 [message #38247 is a reply to message #38244] |
Fri, 07 December 2012 20:53   |
nlneilson
Messages: 644 Registered: January 2010 Location: U.S. California. Mojave &...
|
Contributor |
|
|
In the for() loop
sock.Put(FruitList[i] + '\n');
it looks like a sock.Put is called each time through the loop or is each char sent through the same connection and then '\n' is sent after all the char been sent?
or is it ch \n ch \n ch \n
Also does String GetLine() stop at each \n or is it looking for \0
Maybe String GetAll(int len) will read a bunch of data including several \n until the max len is reached or \0
My client is in U++ and it will send a string with several \n and it is parsed after the server in the Java app.
edit: The last post by lectus where the data is added and that is sent is the way I do it.
[Updated on: Sat, 08 December 2012 00:14] Report message to a moderator
|
|
|
|
|
Re: Socket Communication 101 [message #38253 is a reply to message #38236] |
Sat, 08 December 2012 00:42   |
lectus
Messages: 329 Registered: September 2006 Location: Brazil
|
Senior Member |
|
|
Lower level sockets work with \0 and buffer sizes.
GetLine is a higher level function which parses the string until it finds \n. It's good for protocols based on lines of string such as HTTP protocol where you need to send a \n at the end.
[Updated on: Sat, 08 December 2012 00:43] Report message to a moderator
|
|
|
|
|
|
|
|
Re: Socket Communication 101 [message #38432 is a reply to message #38403] |
Sat, 15 December 2012 16:37   |
nlneilson
Messages: 644 Registered: January 2010 Location: U.S. California. Mojave &...
|
Contributor |
|
|
This is a GPS GGA NEMA sentence:
$GPGGA,175226.000,3502.264328,N,11758.158283,W,2,8,,810.661, M,-31.8,M,,*73
This is stripped of everything except the latitude, longitude and altitude.
Then an ID such as a planes tail number and position number relative to a pilots aircraft.
35.030438,-118.167888,828.3,N10801,1
35.034438,-118.173888,1438.3,N10802,2
35.036438,-118.158888,1133.3,N10803,3
35.038438,-118.179888,1438.3,N10804,4
35.040438,-118.152888,1133.3,N10805,5
.........
To send this data with 50 lines each on individual packets with sock.Put(...) every second does not work without errors.
Adding all 50 lines and stripping the \0 except at the end it can be sent in one packet every second without errors or problems.
|
|
|
|
|
Re: Socket Communication 101 [message #39671 is a reply to message #39664] |
Tue, 16 April 2013 19:51   |
nlneilson
Messages: 644 Registered: January 2010 Location: U.S. California. Mojave &...
|
Contributor |
|
|
Thanks for the link, I saved that and also the document so it can be read off line.
Yes the data does get buffered but the difference was in my code.
Before it was like this:
getline
reformat
send
That was one packet.
getline
reformat
send
That was another packet.
etc. for each line.
It is the time required to send each individual packet that is important before they get corrupted or out of order.
Now the code is like this:
getline
reformat, keep the \n but remove the \0
add to buffer
get next line
reformat, keep the \n but remove the \0
add to buffer
etc until all lines are added to buffer
add \0
send
That is one packet
The amount of data or size of the buffer is irrelevant at least for the amount I have been working with.
By the time the Upp file choose box disappears all paths are displayed in the Java app.
Upp is great to be able to make changes.
[Updated on: Tue, 16 April 2013 19:54] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Sun Apr 27 01:36:16 CEST 2025
Total time taken to generate the page: 0.01964 seconds
|
|
|