Questions about socket

Hello,
I've studied network programming, and I'd write my first client/server program to be sure I understood how it works but before I have some questions that I would like to ask.

I'd write a client/server program where both of them can send and recevice messages asynchronously.
The client connects to the server, send the first message, the server response then they can send and receive messages when it is needed.
Is it possible to do something like that?

If I should write a multi-thread server, or multi-process, it could be good to limit the number of process/thread created.
What should be the maximum number allowed?

Thanks.
I'd write a client/server program where both of them can send and recevice messages asynchronously.
The client connects to the server, send the first message, the server response then they can send and receive messages when it is needed.
Is it possible to do something like that?
Yes. IP sockets are bidirectional.

If I should write a multi-thread server, or multi-process, it could be good to limit the number of process/thread created.
What should be the maximum number allowed?
The physical limit isn't so much the problem as the practical limit. Kernel threads and processes are scheduled in pretty much the same way. If you're using user space threads, different story.

A multi-threaded server will run all connections within the same process. So faulty code that's run when handling one thread can affect code servicing other clients. But as many data structures can be shared, it uses less machine resources.
Yes. IP sockets are bidirectional.

Sockets are bidirectional, I think I have to use nonblocking IO.
I have to call send and recv functions in both client and server, is it right?


The physical limit isn't so much the problem as the practical limit. Kernel threads and processes are scheduled in pretty much the same way. If you're using user space threads, different story.

A multi-threaded server will run all connections within the same process. So faulty code that's run when handling one thread can affect code servicing other clients. But as many data structures can be shared, it uses less machine resources.


I will use user space threads, but in general I think it is necessary to limit the max number of clients.
Last edited on
Sockets are bidirectional, I think I have to use nonblocking IO.
IP and sockets aren't quite the same thing.

IP is the network protocol that connects to computers. Sockets is a library that makes network programming similar to using files.

So, yes you need to send and receive data using send() and recv() for TCP in both the client and server.

I will use user space threads. ...
I'm not sure why you'd choose to do that. Normally (as in C and Java), they're used when kernel threads aren't available. Errlang and Go use them to implement very light weight threads--lots and lots of them.

Apache 1 forks clients and Apache 2 uses threads, initially to support Windows.

I'm not clear on the context of your comments. If you're just supporting a few thousand clients with lightwight processing on the server a simple single threaded server might suffice.
Working with sockets sometimes tends to make your code a bit more complex. This complexity might lead your code to suffer from errors. Those errors can be dealt using programs such as checkmarx for code detection.
Goo luck!
Topic archived. No new replies allowed.