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 7725923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:04:41+00:00 2026-06-01T05:04:41+00:00

I need my client to bind to a particular port and then connect to

  • 0

I need my client to bind to a particular port and then connect to its server. The problem is if I bind to the given port successfully but unable to connect to the server. If I don’t attempt to bind to the port I am able to connect to the server. The below is my program:

int main()
{

    int sock, bytes_recieved;
    char send_data[1024],recv_data[1024];
    struct hostent *host;
    struct sockaddr_in server_addr,client_addr;

    host = gethostbyname("127.0.0.1");

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("Socket");
        exit(1);
    }
    client_addr.sin_family = AF_INET;
    client_addr.sin_port = htons(4000);
    client_addr.sin_addr = *((struct in_addr *)host->h_addr);
    bzero(&(client_addr.sin_zero),8);

    //host = gethostbyname("192.168.61.67");
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(2404);
    server_addr.sin_addr = *((struct in_addr *)host->h_addr);
    bzero(&(server_addr.sin_zero),8);

    if (bind(sock, (struct sockaddr *)&client_addr, sizeof(struct sockaddr))== -1)
    {
        perror("Unable to bind");
        exit(1);
    }
    else
    {
        fprintf(stderr,"Client bound to 4000\n");
    }

    if (connect(sock, (struct sockaddr *)&server_addr,
                sizeof(struct sockaddr)) == -1)
    {
        perror("Connect");
        exit(1);
    }
    else
    {
        printf("Connected to the server\n");
    }

    while(1)
    {

      bytes_recieved=recv(sock,recv_data,1024,0);
      recv_data[bytes_recieved] = '\0';

      if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
      {
       close(sock);
       break;
      }

      else
       printf("\nRecieved data = %s " , recv_data);

       printf("\nSEND (q or Q to quit) : ");
       gets(send_data);
        if (strcmp(send_data , "q") != 0 && strcmp(send_data , "Q") != 0)
       send(sock,send_data,strlen(send_data), 0);

      else
      {
       send(sock,send_data,strlen(send_data), 0);
       close(sock);
       break;
      }

    }
return 0;
}

In this program, If I use localhost for both the client and the server I am able to bind to the pot and connect to the server successfully. The problem appears if I give a different IP to the server. Kindly suggest any changes to the code to achieve my goal

  • 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-01T05:04:44+00:00Added an answer on June 1, 2026 at 5:04 am

    Your problem is that you are binding to the address 127.0.0.1. This address can only be used to connect to other loopback addresses.

    Instead, you should be setting the sin_addr member of the bound address to INADDR_ANY:

    client_addr.sin_addr.s_addr = INADDR_ANY;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What client-side situations need bind()? I saw the server program binding to
I need to model-bind data from textboxes generated dynamically on the client side (using
I need a client-server encryption solution for web transfer, (i.e. signed and encrypted client-side,
I have a client server application in which the server and the client need
I need the full client path of the file on the server side so
I am developing an OPC client and need a free OPC server that can
I need to create Client/Server application to send files from clients to Server. I
I'm working on a client/server application in C#, and I need to get Asynchronous
My client application uses a boost::asio::ip::tcp::socket to connect to a remote server. If the
I'm trying to create a client-server program that send and receive a string (its

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.