How to handle multiple clients connected to a server in CPP? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to learn winsock2 by following a tutorial. The problem is that the last section where it tells you about handling multiple clients, has empty code. How would this be achieved with multi-threading in a nice-mannered way?
Code: https://pastebin.com/D3L8CgAi
Since links to pastebin must be accompanied by code, I need to add this.

To clarify: I would not use threads to handle multiple clients.
To your question:
1 thread should listen for new connections.
When a connection is accepted a new socket is created.
For each accepted socket: create a thread for reading/writing to that socket.
The reason I would not implement it this way, is because it will not scale well. After ~100 concurrent connections (maybe more, maybe less) the process will crash due to out of memory. (Threads are expensive).
Google "multi thread socket windows C++" you should find numerous examples including videos with explanations.
If you really want to create a scalable server review libraries such as libevent (which wrap asynchronous mechanisms such as epoll).

Related

Which is the best way to run some code on the main thread on Linux from another thread (C/C++)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
There is one GUI application (host) and one plugin application. I need to run some code from plugin in the main thread of host application. I also have no access to the source code of host application.
On Mac it's possible via performSelectorOnMainThread. On Win we can set a hook and then send a message. But what about Linux?
Long story:
Host application can use plugin by the two ways: directly in the main thread (via click button) and in some processes in another thread. Plugin is the Qt-based application and qApp always have to be created in the main thread (to avoid some problems), but it doesn't happen in the second case. On Win and Mac this issue is already solved through invoking creation code in the main thread (as I've written above). I'm looking for the similar way to do that on Linux.
Upd.
Finally I found solution of this problem via host API. Unfortunately there is no strong guarantee, because implementation maybe different for another host (despite documentation). But for main cases it works well.
Question is still actual, btw.

What is the best way to call functions over a network? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm planning the network communications system for a program. The program is divided a client and a server class, but I might split them into separate programs if they get too big, and nothing else prevents it.
At the moment, all of the network communication is to call set and get functions on the server (of which their are about 500, and I expect to generate about 500-1,600 requests per second across eight clients in typical operations).
My current plan is to use a basic network API to send machine code inspired Opcodes to the server as a string, along with a few bytes of parameters, and have the server select the function and interpret the parameters using a massive switch statement.
However, with hundreds of possible function to call, this will get A) hard to read, and B)slow to implement. I'm also a bit concerned about the performance of large switch statements.
In light of that, could anyone confirm if this is the best way to implement function calls over a network, or tell me if there's a better way. I'd love a magic API that allows be to do something like x = callServerFunc(server, function, parameter1...) but my searches have not yet found such a library.
You can write your code from scratch. Feel free on how to do it.
Or you can use one of many ready mature technologies. Just google for CORBA, DCOM, XPCOM and so on. You can look at Remote call in common. Or consider existing web-based technologies with different data formats. This is just a starting point. This tehnologies are mature but heavyweight. You'll need some knowledge and practice to start with them.

Is it possible to send a pointer to function via socket? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two processes of the same program, possibly running on two different machines.
I'd like the process P2 to receive a function pointer from process P1 via socket.
Is is possible? Is it a good practice?
EDIT: more interesting would be to send the code of the function too, but I'm skeptic about this. Would it be possible?
You can send a function pointer from one process to another, the same way you can send a pointer to some other object.
The problem is that the pointer may not actually point to the function as it exists in the target process. Especially if the OS is protecting itself with things like ASLR.
You could also send the code across, provided you had some way of figuring out where it ended, and that it was position independent code, and that your environment allowed you to write arbitrary data to memory and then call it.
But, to be honest, there are better ways to achieve what you seem to want, such as the use of RPC (remote procedure calls), in a more portable manner.

Large File I/O Async vs. Multi-Threaded Sync I/O [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I've got a Linux program which is copying fairly large files(400MB to 10GB) to a remote NFS server. I am currently using synchronous I/O calls to copy the data to the NFS mount. All of these calls are occurring within separate threads in a thread pool. So I'm not really blocking the operation of the main thread.
I've heard a lot about using Linux AIO for tasks like these. But I don't really see the advantage it would give for large files.
What are some of the pros/cons of using AIO versus running synchronous IO in threads? Are there any statistical comparisons of this type of scenario out there in the web?
To faster data transfer, you may want to transfer file chunks in parallel. This requires many parallel threads. With AIO, you can initiate multiple data transfers with limited number of threads, so saving memory for thread stacks. But if the number of parallel data transfers that network and NFS server can process is limited, better use threads for simplicity of programming.

What are my options for cross program communication? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
A while ago i made a database framework in c++ and have been using it in various places, even made a wrapper for it for vb.net.
Now i have a new project that would require multiple programs accessing a single database and it would be wasteful to load up the database multiple times for every one of them not to mention the syncing horrors.
So I figured i would turn the framework into a standalone application and access to the data would be done in some xx magical way from those other programs. From what I've seen php and mysql do something like this..?
Problem is, I have no clue where to start. The only kind of cross program communication i've done is one program reading and writing directly into the other ones memory, seems kinda hacky though and I'm not sure if that sort of thing is going to fly with managed languages (I want to make it accessible in vb.net too).
Tips?
The most portable way to do IPC (inter-process communication) is probably going to be Sockets.
What about D-Bus? There ist a port for Windows.