Boost asio - synchronous write / read - how to do? - c++

First, I want to say that I'm new with Boost asio, and I see a lot of examples but it remains things I don't understand.
I want to create a server, that will accept two clients (it will use two socket). The first client will send messages to the server and the server will send this message to the other client (yes, it is useless to use a server, but it's not the point here, I want to understand how all this work). This will happen until one of the client close.
So, I created a server, the server wait for the clients, and then, it must wait for the first client to send some message. And this is my question: what must I do after?
I thought I need to read the first socket, and then write on the second, and so and so, but how I know if the first client writed on the socket? Same, how I know if the second client read the second socket?
I don't need code, I just want to know the good way to do that.
Thanks a lot for reading!

When you perform async_read you specifify a callback which is going to be called whenever any data is read to the buffer ( you should provide the buffer also, check the async_read's documentation ). Respectively you should provide callback for the async_write to know when your data is already sent. So, from the server perspective, for the client which 'writes' you should do async_read, and for the second client which 'reads' you should do async write. With the offered dataflow client1->server->client2 it is hard to recognize which client the server should read from and which one is write to. It's up to you. You can choose the first connected client as writer and the second as reader, for example.
You might want to start with asio iostreams. It's a high-level iostream-like abstraction above asynchronous sockets.
P.S.: also, don't forget to run io_service.run() loop somewhere. Because all the asio callbacks are executed within that loop.

Related

Multithreaded socked server (chilkat, c)

I want to setup socket server which receive incoming connections, and for each connections makes few operations (reveive bytes, check something, send response bytes). But every connection should work independly and if something goes wrong - dont block entire server.
I want to do it using chilkat socket and task libraries.
Where in source code could i put the receiving/processing/answering/code?
Just below CkTask_Run()?
I cant get the idea how forking is done, what is "old" process/thread doing, and where is new process going.
Is there any example to get an idea how it works, or could i get some low-level explanation?
In my opinion, the easiest solution is to write a multi-threaded application. You have one thread for accepting new connections. A new thread is started to run each accepted connection. No async or tasks are necessary. Each thread can operate with synchronous calls for reading/writing/accepting.

c++ communicate with specific client Boost asio

I'm new to c++ and I started to code my server with boost. I follow a lot of example on the web and on the official doc. But i found nothing (maybe I don't ask the good question) about this-> communicate with a specific client. By this I mean that->
old question:
Server launch and wait for connection-> client(1) connect through
TCP-> server accept client and start async_read
Let's say 3 clients also connect->
How I'll tell to my server too write too client(2) or (3) but not
both?
I express myself badly
New question:
My server work fine, when client send data to server (custom client in Unreal engine 4) he can read it then write back to my client with no problem. I search a way to speak to the client I want without needed him to send data. Example:
client 1 write to server-> the data send to server launch the next action-> write to a specific client.
More specific example:
Client 1 want to send request to client 10, so client 1 write to the server the action «action, id client» (request, 10) then the server know that he need to talk to the client 10 and send request.
My problem is not on the client side, but on the server side.
I'm sure it's pretty easy and I just don't understand some basic stuff, if someone could provide me a direction, an example or simply an explanation it would be appreciated. Thanks for future answer.
EDIT:
If somebody have hard time like me (I know it's easy but we never know :p, maybe it could help someone) here the answer.
I include this inside the file where I use to connect, write, send, etc.
std::map<int, tcp::socket> playerRemote;
I set it->
playerRemote.insert(std::pair<int, tcp::socket>(id, std::move(socket_)));
use the socket->
boost::asio::async_read(playerRemote.at(id_to_use)
Question resolve! thanks for help!
Every time that your server program did an accept it got a new socket with a new client on the other end of it.
The usual practice is to have some kind of object which you create and initialize with this new socket. And then you put that object into some kind of structure. Like a set, a map, a vector, a list, anything really.
When you want a particular client then search that data structure for it. If you used a map or a unordered_map then you can get it quickly by whatever key you used.
Now you have your client object you can call a method on it. Like your own version of "send" which can add it to a per-client buffer. Since message sending is asynchronous in Boost ASIO (it's right there in the name) you know you may not be able to send it right away.
The Boost ASIO chat example application is good about this.
Look at the link that The Quantum Physicist put in comments. Especially this one: http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp11/chat/chat_server.cpp

C++ Sockets Unsynched/Parallel Code without Multithreading

I have this 2-side(client and server) program written in c++, which is basically a "chat program".
So one side sends data to the other side which sends data to the other side .
I am using C-Sockets (TCP/SOCK_STREAM).
So my Problem is, that while the server is waiting for data ("recv()") it is unable to send data itself, as the recv queue is infinite, as far as my experience shows.
I have thought of two ways to solve this, but I am not sure if its worth the time. Because there maybe is a simple solution, which I am just too stupid too think of.
Make it a 6-sided connection, meaning I have a "physical Server", where my server is running on in four instances, twice for each client(sending and recieving). so if one server recieves data it stores the data in a file, for the sending server of the other client to read out and.. send it. Works same the other way around.
As this solution sounds rather shitty, I might not go with it.
Using Multithreading. Which is as I have heard pure cancer to use and you shouldn't use it, unless you have to.
So my question is, do I have to? or is there maybe a simple solution?
recv() on a blocking socket is used when you need to wait for incoming data and have nothing to do until data is arrived. If it's not your case you can use non-blocking socket, or select with timeout. select would be simpler, just check if anything arrived on your socket regularly (e.g. in a loop), and do what you want in between.
By the way, it's called "BSD socket", not "C socket", this can help you googling.

C++ how to accept server-push data?

My situation: I would like to create a hobby project for improving my C++ involving real-time/latency programming.
I have decided I will write a small Java program which will send lots of random stock prices to a client, where the client will be written in C++ and accept all the prices.
I do not want the C++ client to have to poll/have a while loop which continuously checks for data even if there is none.
What options do I have for this? If it's easier to accomplish having a C++ server then that is not a problem.
I presume for starters I will have to use the boost ASIO package for networking?
I will be doing this on windows 7.
Why not just have the Java server accept connections and then wait for some duration of time. e.g. 10 seconds. Within that time if data becomes available, send it and close the connection.
Then the C++ client can have a thread which opens a connection whenever the previous one has completed.
That should give quite low latency without creating connections very often when there is no new data.
This is basically the Comet web programming model, which is used for many applications.
Think about how a web server receives data. When a URL is accessed the data is pushed to the server. The server need not poll the client (or indeed know anything about the client other than its a service pushing bytes towards it).
You could use a Java servlet to accept the data over HTTP and write the code in this fashion. Similarly, boost::asio has a server example that should get you started. Under the hood, you could enable persistent HTTP so that the connections aren't opened / closed frequently. This'll make the coding model much simpler.
I do not want the C++ client to have to poll/have a while loop which
continuously checks for data
Someone HAS to.
Need not be you. I've never used boost ASIO, but it might provide a callback registration. If yes, then just register a callback function of yours with boost, boost would do the waiting and give you a call back when it gets some data.
Other option is of course that you use some functions which are synchronous. Like (not a real function) Socket.read() which blocks the thread until there is data in the socket or it's closed. But in this case you're dedicating a thread of your own.
--edit--
Abt the communication itself. Just pick any IPC mechanism (sockets/pipes/files/...), someone already described one I think. Once you send the data, the data itself is "encoded" and "decoded" by you, so you can create your own protocol. E.g. "%%<STOCK_NAME>=<STOCK_PRICE>##" where "%%", = and ## (markers to mark start, mid and end) that you add on sender side and remove on receiver side to get stock name and price.
You can develop the protocol further based on your needs. Like you can also send buy/sell recommendation or, text alert msgs with major stock exchange news. As long as your client and server understand how the data is "encoded" you're good.
Finally, if you want to secure teh communication (and say you're not using some secure layer (SSL)) then you can encrypt the data. But that's a different chapter. :)
HTH

client-server design

i want to develop a pretty basic client-server program.
one software reads xml (or any data) and send it to the server who in turn will manipulate it a little bit and eventually will write it to the disk.
the thing is that if i have many xml files on disk (on my client side), i want to open multiple connection to the server , and not doint one by one.
my first question is : let's say i have one thread who keeps all the files handles and waitformultipleobjects on them, so it will know when one of them is ready to be read from disk. and for every file i have an appropriate socket who suppose to send that specifi file to the server. for the socket i can use the select function to know which sockets are ready for sent. but is there way to know that both the file and the appropraite socket are ready to be sent ?
second, is there a more efficient way to design the client, cuase on my current design i'm using just one thread which on multi processor computer is rather not efficient enough.
(though i'm sure is till better then laucning new thread for every socket connection)
third, for the server i read about the reactor pattern. it seems appropriate but still ,like my second question, seems not effient enought while using one thread.
maybe i can use something with completion ports ? think they are pretty efficient but never really used them, so don't know exactly how.
any answers and general suggestion would be great.
Take a look at boost::asio it uses a proactor pattern (see the docs) that basically uses the OS wait operations (waitforsingle/multiple,select,epoll, etc...) to make very efficient use of a single thread in a system like you're looking at implementing.
asio can read/write files as well as sockets. You could sumbit an async read for the file using asio, it would call your callback on completion then you would submit that read buffer as an async write to the socket. Asio would take care of delivering all async writes buffers as the socket completed each pending write operation.
Each of these operations is done asynchronously so the thread is only really busy to initiate reads or writes, sitting idle the rest of the time.