Socket usage

25 views
Skip to first unread message

Rafik Abdulwahab

unread,
Jul 25, 2013, 9:18:02 AM7/25/13
to lib...@googlegroups.com
Hi,

Great job on this simple yet powerful cross platform socket and multi-threading library, guys. Multithreading with ting has been an amazing experience so far. 
However, I could not find a concise tutorial on how to send data over sockets. I'm stumped at how the ting:: datatypes should be used. I'll really like it if anyone could provide an example on how to go about this? 

Thanks

Ivan Gagis

unread,
Jul 26, 2013, 8:00:49 AM7/26/13
to lib...@googlegroups.com
Hi,

there is a tutorial here:
https://code.google.com/p/ting/wiki/SocketsUsage

if you are using latest version of ting from SVN trunk then this tutorial may be a bit outdated, but should give general representation.

If there's something unclear for you, ask exact question, what exactly is not clear, then I might be able to help. Tell more about your task, what kind of communication you are trying to do, UDP or TCP etc.

--
Ivan


четверг, 25 июля 2013 г., 16:18:02 UTC+3 пользователь Rafik Abdulwahab написал:

Rafik Abdulwahab

unread,
Jul 26, 2013, 8:49:59 AM7/26/13
to lib...@googlegroups.com
Okay to be more exact, I'm working on an application that generates a json document from user interactions with its GUI. I want to send the document which I have retrieved into an std::string container over TCP to another application running on a remote server. The tutorials I could find use a ting::StaticBuffer<ting::u8, 4> datatype. How do I go about sending more than just four bytes because I'll like to send over 20MB of data through the socket. 

Ivan Gagis

unread,
Jul 26, 2013, 11:34:22 AM7/26/13
to lib...@googlegroups.com
Well, std:string is just an array of bytes which you need to send. So, normally you establish the TCP connection as shown in the tutorial I mentioned before. After that you have opened TCP socket which works like stream. So you need to send all your bytes. You can allocate larger buffer for example 4kb like this:

ting::StaticBuffer<ting::u8, 4096> buf;

just check out docs for ting::Buffer, ting::StaticBuffer, ting::Array to know more about them.

Then you just copy data from your string to that buffer which you then pass on to the socket. So, you can send your 20mb by, for example, 4kb chunks, one after another.
But, check docs for TCPSocket::Send() to know how it works, it does not guarantee to send the whole buffer at once, it returns the number of bytes actually sent.
You should also know how to use ting::WaitSet in order to know when the socket is ready to send/receive the next portion of data. So, generally, you should get familiar with basic principles of how sockets work... I mean BSD sockets. ting::net is just a C++ wrapper for them, which makes many things easier, but you still have to understand the general things about sockets, and about select() function (this is what ting::WaitSet for).

So, if you have problems making your code to work, then just post here your code snippet which does not work and then I might see what is wrong in how you use ting::net and help you.

--
Ivan



ting::StaticBuffer<ting::u8, 4>


пятница, 26 июля 2013 г., 15:49:59 UTC+3 пользователь Rafik Abdulwahab написал:

Rafik Abdulwahab

unread,
Jul 26, 2013, 12:03:25 PM7/26/13
to lib...@googlegroups.com
Sorry about that. I misunderstood what the Buffer class was about. I thought that it does the size checking and portion sending automatically behind the scenes. I made a static library from the ting source files so I cant really access the implementations just their declarations in the header files. I guess I should be using the source files as is then. 
Thanks for the support.


--
You received this message because you are subscribed to a topic in the Google Groups "libting" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/libting/yDbT8cn30EM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to libting+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages