简体   繁体   中英

Can a C program be both a server and client at the same time?

I'm a learning C and just started reading about sockets.

I was wondering if a program can be coded to be both a server and a client at the same time. For example, a program starts off as a server but if it wanted to connect to another server, it could, in parallel, start a client to connect to other program's servers, while maintaining it's server status for others to connect too.

If so, how can that be done? With different processes?

Thank you guys!

Yes, it can.

There is absolutely nothing which stops this.

If you want to manage more than one socket from the same process, you might find the poll function useful, or select on Windows. ( select also works on Linux but poll is better in my opinion)

This is one of the use cases for "threading" where there are multiple execution instances performing different jobs in concert. https://simple.m.wikipedia.org/wiki/Thread_(computer_science)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM