c - Two mains in program -
c - Two mains in program -
here situation: need send info neighbor(socket) , switch listening mode. ive got client part in client.c, listens, , server part in server.c - sends data. using sockets need have main() in both of them. how should them "cooperate" together, both mainss not going result in error?
or other ideas how solve issue sending , listening?
thanks in advance!
lucas
you can create 2 executables sources. each of them have own main.
or, can create single executable , allow fork process or create thread. when creating new thread you'll specify sec "main" thread function.
when fork-ing, should create 2 functions main_server , main_client , allow actual main decide of them call, after fork. see snippet:
int main_server(int argc, int argv){ //todo: finish homecoming 0; } int main_client(int argc, int argv){ //todo: finish homecoming 0; } int main(int argc, int argv){ //todo: parse args , argv_server, argv_client, argc_server, argc_client int pid = fork(); if (pid < 0) { //todo: handle error , leave } else if (pid) { // start client here illustration main_client(argc_client, argv_client); } else { main_server(argc_server, argv_server); wait(pid); } homecoming 0; /* todo: each of above calls should checked errors */ } hope helps.
note: it's improve create separate executable if required have one, utilize above snippet.
c networking client-server
Comments
Post a Comment