Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6135979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:32:47+00:00 2026-05-23T17:32:47+00:00

here is the situation: I need to send a data to a neighbor(socket) and

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:32:48+00:00Added an answer on May 23, 2026 at 5:32 pm

    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 fork another 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 functions main_server and main_client and let the actual main decide which of them to call, just after the fork. See snippet:

    int main_server(int argc, int argv){
      //TODO: complete
      return 0;
    }
    
    int main_client(int argc, int argv){
      //TODO: complete
      return 0;
    }
    
    int main(int argc, int argv){
      //TODO: parse args and get argv_server, argv_client, argc_server, argc_client
      int pid = fork();
      if (pid < 0) {
         //TODO: handle error and leave
      } else if (pid) {
         // start client here for example
         main_client(argc_client, argv_client);
      } else {
         main_server(argc_server, argv_server);
         wait(pid);
      }
      return 0;
      /* TODO: each of the above calls should be checked for errors */
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the situation: I have a huge data set that I need quick
My situation here is that I need to send my program to my university's
Here is the situation. I need to hit ~50 servers and grab some data
Here's the situation: we have an Oracle database we need to connect to to
Here's the situation - I've got a shell that loads an external .swf. Now,
We're often faced with the need to send a data file to one of
i have a situation here where i need to pass the date as sysdate
I am dealing with a puzzling situation here,and need any help I can get
I have a situation here where I need to distribute work over to multiple
Here is my situation: I am developing a java typing game, and I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.