Reputation: 409
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();
*/
Upvotes: 1
Views: 327
Reputation: 595412
Try this:
#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>
typedef struct sockaddr_in sockaddr_in;
struct ServerSocket
{
int sockfd;
int rec_sock;
ServerSocket()
{
sockfd = -1;
rec_sock = -1;
}
~ServerSocket()
{
if (rec_sock != -1) closesocket(rec_sock);
if (sockfd != -1) closesocket(sockfd);
}
void socket(int family, int type, int protocol)
{
sockfd = socket(AF_INET, SOCK_STREAM, 0);
}
void bind(int port)
{
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
::bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
}
void printInfo(void)
{
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
int len = sizeof(addr);
getsockname(sockfd, (struct sockaddr *)&addr, (socklen_t *)&len);
printf("ip = %s, port = %d\n", inet_ntoa(addr.sin_addr), ntohs(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)
{
sockaddr_in recaddr;
memset(&recaddr, 0, sizeof(recaddr));
int len = sizeof(addr);
getpeername(rec_sock, (struct sockaddr *)&recaddr, (socklen_t *) &len);
printf("remote machine = %s, port = %d.\n", inet_ntoa(recaddr.sin_addr), ntohs(recaddr.sin_port));
}
void write(void *data, int len)
{
::write(rec_sock, (char*)data, len);
}
};
.
ServerSocket sock;
sock.socket(AF_INET, SOCK_STREAM, 0);
sock.bind(1031);
sock.printInfo();
sock.listen(5);
sock.accept();
sock.printPeerInfo();
sock.write("hi, there", 10);
sleep(20);
exit(1);
Upvotes: 2
Reputation: 1027
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
Upvotes: 2