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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:23:05+00:00 2026-06-17T00:23:05+00:00

I am using the following code to connect to the server at a certain

  • 0

I am using the following code to connect to the server at a certain port which are provided as command line arguments …

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>

int main(int argc,char *argv[])
{
   struct sockaddr_in serverAddr;
   int clientSocketFd ;
   char buffer[1024];

 if((clientSocketFd = socket(AF_INET, SOCK_STREAM, 0))==-1)
    perror("socket");

//get the server IP address and PORT
bzero(&serverAddr, sizeof serverAddr);
printf("ip address :- %s\n",argv[1]);
inet_pton(AF_INET, argv[1], &(serverAddr.sin_addr));
serverAddr.sin_family=AF_INET;
serverAddr.sin_port = atoi(argv[2]);

printf("PORT :- %d\n",serverAddr.sin_port);
//connect to server
if(connect(clientSocketFd,(struct sockaddr *) &serverAddr, sizeof(serverAddr)) == -1)
    perror("connect");

printf("Connecting to the server %s on port %s \n",argv[1],argv[2]);

while (1)
{
    //receive incoming data
    if(recv(clientSocketFd, buffer,1023, 0)==-1)
    {
    printf("buffer : %s\n" ,buffer);
    printf("Received from Server : %s \n",buffer);
        break;
    }


}
close(clientSocketFd);

}

but at the client side , it shows “connect : Connection Refused”…

If I use telnet , then it shows connected , but not able to connect through the above code of client.c
plz help

Also I changed the number of maximum allowed pending connections to 100 , then also the problem did not solved… 🙁 …help plz

  • 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-17T00:23:06+00:00Added an answer on June 17, 2026 at 12:23 am

    You are connecting to the wrong port. Change:

    serverAddr.sin_port = atoi(argv[2]);
    

    To:

    serverAddr.sin_port = htons(atoi(argv[2]));
    

    Think of these structures as being used to communicate with another planet where they write their numbers differently. You have to convert to and from the way you write numbers to the way they write numbers, otherwise you get nonsense.

    The htons function converts ports numbers from the way your computer stores them to the way they are used on the network. The ntohs function converts port numbers from the way they are used on the network to the way your computer stores them. Socket addresses are in network byte order.

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

Sidebar

Related Questions

I'm using the following code↓ to connect to a ftp server and get a
I am trying to connect to my remote MySQL server, using the following code.
Using the following code, I am able to connect to the weblogic server. Now
I am starting our JMX server using the following code: Integer port = 8291;
I am trying to connect to sql server using php using following code: $myServer
I am trying to connect to server using keystore which is provided by server
I am using the following code to connect to an Oracle stored procedure, with
I am using the following code to connect to a MySQL DB and simply
I am using following code to create new wifi access point and to connect
I'm using the boto library in Python to connect to DynamoDB. The following code

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.