here is the situation: I need to send a data to a neighbor(socket) and then switch to listening mode. Ive got a client part in client.c, which just listens, and server part in server.c – sends data. Using sockets I need to have a main() in both of them. How should I get them “cooperate” together, so both mainss are not going result in error?
Or any other ideas how to solve this issue with sending and listening?
Thanks in advance!
Lucas
You can always create two executables from the sources. Each of them will have its own
main.Or, you can create a single executable and let it
forkanother process or create another thread. When creating a new thread you’ll specify the second “main” to be the thread function.When
fork-ing, you should create two functionsmain_serverandmain_clientand let the actualmaindecide which of them to call, just after thefork. See snippet:Hope it helps.
Note: it’s better to create a separate executable but if you are required to have only one, use the above snippet.