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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:04:40+00:00 2026-05-30T10:04:40+00:00

I am trying to wrap unix socket api calls in a C++ class to

  • 0

I am trying to wrap unix socket api calls in a C++ class to make it easier to work with. I have taken a minimal working example written in C and duplicated the code in a class. As you can see from below the code is identical whether I am calling the function version or method versions of the wrapper code. See ugly temporary code below:

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

static int sockfd;
static int rec_sock;
static int len;

typedef struct sockaddr_in sockaddr_in;
static sockaddr_in addr;
static sockaddr_in recaddr;

int ServerSocket_socket(int family, int type, int protocol)
{
    sockfd = socket(AF_INET, SOCK_STREAM, 0)
    return sockfd;
}

void ServerSocket_bind(int port)
{
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_family = AF_INET;
    addr.sin_port = port;

    bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
}

void ServerSocket_printInfo(void)
{
    len = sizeof(addr);
    getsockname(sockfd, (struct sockaddr *)&addr, (socklen_t *)&len);
    printf("ip = %s, port = %d\n", inet_ntoa(addr.sin_addr), (addr.sin_port));
}

void ServerSocket_listen(int backlog)
{
    listen(sockfd, backlog);
}

void ServerSocket_accept(void)
{
  rec_sock = accept(sockfd, (struct sockaddr *)(&recaddr), (socklen_t *)&len);
}

void ServerSocket_printPeerInfo(void)
{
    printf("remote machine = %s, port = %x, %x.\n", inet_ntoa(recaddr.sin_addr), recaddr.sin_port, ntohs(recaddr.sin_port));
    memset(&recaddr, 0, sizeof(recaddr));
    len = sizeof(addr);
    getpeername(rec_sock, (struct sockaddr *)&recaddr, (socklen_t *) &len);
}

void ServerSocket_write(void)
{
  write(rec_sock, "hi, there", 10);
  sleep(20);
  exit(1);
}

struct ServerSocket
{
  int socket(int family, int type, int protocol)
  {
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      return sockfd;  
  }

  void bind(int port)
  {
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_family = AF_INET;
    addr.sin_port = port;

    ::bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
  }

  void printInfo(void)
  {
      len = sizeof(addr);
      getsockname(sockfd, (struct sockaddr *)&addr, (socklen_t *)&len);
      printf("ip = %s, port = %d\n", inet_ntoa(addr.sin_addr), (addr.sin_port));
  }

  void listen(int backlog)
  {
      ::listen(sockfd, backlog);
  }

  void accept(void)
  {
    rec_sock = ::accept(sockfd, (struct sockaddr *)(&recaddr), (socklen_t *)&len);
  }

  void printPeerInfo(void)
  {
      printf("remote machine = %s, port = %x, %x.\n", inet_ntoa(recaddr.sin_addr), recaddr.sin_port, ntohs(recaddr.sin_port));
      memset(&recaddr, 0, sizeof(recaddr));
      len = sizeof(addr);
      getpeername(rec_sock, (struct sockaddr *)&recaddr, (socklen_t *) &len);
      printf("remote machine = %s, port = %d, %d.\n", inet_ntoa(recaddr.sin_addr), recaddr.sin_port, ntohs(recaddr.sin_port));
  }

  void write(void)
  {
    ::write(rec_sock, "hi, there", 10);
    sleep(20);
    exit(1);
  }
};

When I call the functions the code works like a champ. However when I run the almost identical code wrapped in methods I get a connection refused. Any idea what this minimal code change is doing to cause the example not to work?

   ServerSocket_socket(AF_INET, SOCK_STREAM, 0);
    ServerSocket_bind(1031);
    ServerSocket_printInfo();
    ServerSocket_listen(5);
    ServerSocket_accept();
    ServerSocket_printPeerInfo();
    ServerSocket_write();
  /*
    ServerSocket sock;
    sock.socket(AF_INET, SOCK_STREAM, 0);
    sock.bind(1031);
    sock.printInfo();
    sock.listen(5);
    sock.accept();
    sock.printPeerInfo();
    sock.write();
  */
  • 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-30T10:04:42+00:00Added an answer on May 30, 2026 at 10:04 am

    In ServerSocket_bind change:

    addr.sin_port = port;

    to

    addr.sin_port = htons(port);

    And it should work. Also, use strace to debug syscalls (or ltrace for libc) like:

    strace -f ./a.out

    ltrace ./a.out

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

Sidebar

Related Questions

I have been trying to wrap my head around creating a RESTFul API using
I've been trying to wrap my head around how threads work in Python, and
I'm trying to wrap my head around asp.net. I have a background as a
Trying to wrap my head around the apple design scheme. I have a UIViewController
I'm trying to wrap my head around this to make the correct design decisions.
Still trying to wrap my noggin around OAuth, but I have a question. I
I'm trying to wrap a class around the first or the second half of
Trying to wrap this short example in C++. (and its been a while since
I have been trying to wrap my head around this FXCop violation DoNotDeclareReadOnlyMutableReferenceTypes MSDN:
trying to wrap a native cpp class using managed c++ class. all looks good

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.