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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:14:29+00:00 2026-06-16T00:14:29+00:00

Here’s the deal, I’m writing a simple tcp socket server in C (with unix

  • 0

Here’s the deal, I’m writing a simple tcp socket server in C (with unix system calls) that I’m not able to get to accept connections.

From what I can tell, I get through the server initialization just fine, but when I try to connect to the port that I print out (see code below) it refuses as if nothing is there.

More to the point, when I netstat that port isn’t even in use. I’m not throwing any errors with my current set up, I’m all dried up for ideas.

int main(){

    int sock_fd;
    int conn_fd;
    struct sockaddr_in serv_addr;
    struct sockaddr_in cli_addr;
    socklen_t* serlen;
    socklen_t* clilen;
    clilen  = malloc(sizeof(socklen_t));
    serlen  = malloc(sizeof(socklen_t));
    *serlen = sizeof(serv_addr);
    *clilen = sizeof(cli_addr);

    /*=============================Create Socket=============================*/


        //Create Socket
        sock_fd = socket(AF_INET, SOCK_STREAM, 0);
            if(sock_fd<0){
                fprintf(stderr,"error creating socket\n");
                exit(1);}

        //Initialize Server Address Struct
        bzero((char *) &serv_addr, *serlen);
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr = INADDR_ANY;
        serv_addr.sin_port = 0;

    /*=============================Bind Address==============================*/

        //Bind socket to an address
        if(bind(sock_fd,(struct sockaddr*)&serv_addr,*serlen)<0){
            fprintf(stderr,"error binding\n");
            exit(1);}

        //Get socket data
        if(getsockname(sock_fd,(struct sockaddr*)&serv_addr, serlen)<0){
            fprintf(stderr,"error with socket name");
            exit(1);}

    /*=============================Server Started============================*/

        //Listen for connections
        listen(sock_fd,32);

        //Print port
        printf("%i", serv_addr.sin_port);

        conn_fd = accept(sock_fd,(struct sockaddr*)&cli_addr,clilen);

        /**Do something exciting with my new connection**/

}
  • 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-16T00:14:30+00:00Added an answer on June 16, 2026 at 12:14 am

    props to @askmish for inspiring this one

        //Print port
        printf("%i", serv_addr.sin_port);
    

    becomes

        //Print port
        printf("%i", htons(serv_addr.sin_port));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is an object that I'd like to use with ng-repeat, but it's not
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is my SQL script CREATE TABLE tracks( track_id int NOT NULL AUTO_INCREMENT, account_id
Here is the scenario. I'm writing my geo-ruby oracle adapter for Ruby On Rails
Here is the final result of what my page is(not much fancy stuff yet):
Here's the setup - I have a view that lists products. On that same
Here is a scenario: User installs .NET application that you have made. After some
Here's a sample of a SpinBox that writes its changes to underlying variables. The
I know there's a lot of other questions out there that deal with this
Here is my scenario. I have a website running under AppPool1 and that works

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.