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

  • SEARCH
  • Home
  • 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 7852053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:19:40+00:00 2026-06-02T19:19:40+00:00

How can I setup my sockets routine to either send (first) or (switch) to

  • 0

How can I setup my sockets routine to either “send” (first) or (switch) to “receive” if data is “sent” from another computer (first)?

thanks

general code:

-(void) TcpClient{
    char buffer[128];
    struct sockaddr_in sin;
    struct hostent *host;
    int s;

    host = gethostbyname("10.0.0.3");

    memcpy(&(sin.sin_addr), host->h_addr,host->h_length);
    sin.sin_family = host->h_addrtype;
    sin.sin_port = htons(4000);

    s = socket(AF_INET, SOCK_STREAM, 0);
    connect(s, (struct sockaddr*)&sin, sizeof(sin));

    while(1){//this is the Client sequence:
        send(s, buffer, strlen(buffer), 0);//but what if the Server sends first ?? Client needs to receive here first
        recv(s, buffer, sizeof(buffer), 0);
    }
    close(s);
}
  • 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-06-02T19:19:42+00:00Added an answer on June 2, 2026 at 7:19 pm

    A socket is bi-directional. It can be read from and written to at any time. If you want to write a single routine that decides when to read and when to write, you need to use the select() function. It will tell you when a socket has data available for reading, and when the socket can accept data for sending. If the socket receives data before you have data to send, your routine can detect that and perform a “receive/send” operation. If you have data to send before the socket receives data, your routine can detect that and perform a “send/receive” operation instead. For example:

    while (1)
    {
        fd_set fd;
        FD_ZERO(&fd);
        FD_SET(s, &fd);
    
        timeval tv;
        tv.tv_sec = 0;
        tv.tv_usec = 0;
    
        int ret;
    
        if (select(s+1, &fd, NULL, NULL, &tv) > 0)
        {
            ret = recv(s, buffer, sizeof(buffer), 0); 
            if (ret > 0)
                send(s, buffer, ret, 0);
        } 
        else
        {
            ret = send(s, buffer, strlen(buffer), 0);
            if (ret > 0)
                recv(s, buffer, ret, 0); 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you setup a variable for javascript in one page and have another page
I can setup a connection manager that uses the Directory Services OLE provider and
I'm trying to follow the instructions here ( http://docs.pylonsproject.org/projects/pyramid_beaker/en/latest/ ) so I can setup
Can I setup a custom MIME type through ASP.NET or some .NET code? I
Can I setup Team Foundation Server in such a manner that every uer have
How can I setup a shared ccache without falling into a permissions problem? I
How can I setup a default value to a property defined as follow: public
I can correctly setup up a windows hook, but I get confused by the
How can I setup expires headers in PHP + Apache? I'm currently using an
How can I setup custom sorting method for a QTableView , or for the

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.