WinSock2 bind error - c++

I started making a C++ server, but I can't bind to socket.
#pragma once
#include <WinSock2.h>
#include <thread>
#include "Logging.h"
namespace network
{
static SOCKET sock;
static VOID startAccept()
{
while (true)
{
struct sockaddr_in serv_addr, cli_addr;
int clilen = sizeof(cli_addr);
SOCKET accepted;
if (accepted == NULL)
{
accepted = accept(sock, (struct sockaddr *) &cli_addr, &clilen);
if (accepted < 0)
{
core::writeln("Error accept: " + WSAGetLastError());
}
else
{
core::writeln("New connection from " + cli_addr.sin_addr.S_un.S_addr);
}
}
}
}
static VOID connect(const char* ipAddress, u_short port)
{
struct sockaddr_in serv_addr, cli_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0)
{
core::writeln("Error creating socket: " );
perror("error:");
return;
}
else if (bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
core::writeln("Error binding to socket: " );
perror("error:");
return;
}
else if (listen(sock, 10) < 0)
{
core::writeln("Error listening socket: ");
return;
}
else
{
core::writeln("Bound WinSock to " + serv_addr.sin_addr.S_un.S_addr);
std::thread accepting(startAccept);
accepting.join();
}
}
};
In my main int:
int _tmain(int argc, _TCHAR* argv[])
{
connect("127.0.0.1", 500);
}
But everytime I try to bind, I get the following output:
Error binding to socket:
error:No Error
But the socket isn't bound on. What am I doing wrong?
FIXED, had to use WSAStartup.

Related

c udp socket not receiving/sending correctly

I am currently experiencing some trouble while testing UDP sockets.
So I have 2 c programs, one sends and receives, the other receives and sends a message.
Here is send_recv.c:
#define PORT 1010
int main(int argc, const char *argv[]) {
int socketInfo, opt;
struct sockaddr_in server;
char msgOut[30], msgIn[30];
opt = 1;
printf("Input message: ");
fgets(msgOut, 30, stdin);
socketInfo = socket(AF_INET, SOCK_DGRAM, 0);
if(socketInfo < 0) {
perror("socket error");
return EXIT_FAILURE;
}
if(setsockopt(socketInfo, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt failed");
return EXIT_FAILURE;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(PORT);
if(bind(socketInfo, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("bind error");
return EXIT_FAILURE;
}
if(connect(socketInfo, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("connection error");
return EXIT_FAILURE;
}
puts("connected");
send(socketInfo, msgOut, strlen(msgOut), 0);
puts("sleeping for 5");
sleep(5);
puts("message sent");
recv(socketInfo, msgIn, sizeof(msgIn), 0);
printf("received msg: %s\n", msgIn);
close(socketInfo);
return EXIT_SUCCESS;
}
And here is recv_send.c:
#define PORT 1010
int main(int argc, const char *argv[]) {
int socketInfo, opt;
struct sockaddr_in server;
char msgOut[30], msgIn[30];
opt = 1;
socketInfo = socket(AF_INET, SOCK_DGRAM, 0);
if(socketInfo < 0) {
perror("socket error");
return EXIT_FAILURE;
}
if(setsockopt(socketInfo, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt failed");
return EXIT_FAILURE;
}
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY;
if(bind(socketInfo, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("bind error");
return EXIT_FAILURE;
}
if(connect(socketInfo, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("connection error");
return EXIT_FAILURE;
}
puts("connected");
recv(socketInfo, msgIn, sizeof(msgIn), 0);
printf("received msg: %s\n", msgIn);
printf("Input message: ");
fgets(msgOut, 30, stdin);
send(socketInfo, msgOut, strlen(msgOut), 0);
puts("message sent");
close(socketInfo);
return EXIT_SUCCESS;
}
The problem is that the receive program never receives anything, and the send program sends and then just receives the message it already received.
The output of send_rcv is this:
Input message: hello there
connected
message sent
received msg: hello there
and the output of rcv_send is this:
connected
(ctrl+z) Job 6, 'sudo ./rcv_send' has stopped
Another problem is that when I don't run the program with sudo it just says this: bind error: Permission denied
The problem is that both endpoints are listening on the same port while running on the same machine, and you have both sides sending to INADDR_ANY.
Also, port numbers below 1024 are privileged ports and therefore require root access to bind to.
Use a different port (1024 or higher) for each side of the connection, and choose a specific IP address to connect to. For example:
#define LOCAL_PORT 1100
#define REMOTE_PORT 1101
...
struct sockaddr_in local, remote;
local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY;
local.sin_port = htons(LOCAL_PORT);
remote.sin_family = AF_INET;
remote.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
remote.sin_port = htons(LOCAL_PORT);
if(bind(socketInfo, (struct sockaddr *)&local, sizeof(local)) < 0) {
perror("bind error");
return EXIT_FAILURE;
}
if(connect(socketInfo, (struct sockaddr *)&remote, sizeof(remote)) < 0) {
perror("connection error");
return EXIT_FAILURE;
}
The values of LOCAL_PORT and REMOTE_PORT would be swapped in the two programs.
Note also that you can get rid of the setsockopt call as you don't have two programs open on the same port.

Why can't my client connect to the server?

I try to make a application that client send a message and the server respond on the same machine using winsock TCP c++. The problem is that after the server waited for client to connect, I ran the client code and it stopped at the connect and exit. This is my code.
Server:
#include <stdio.h>
#include <tchar.h>
#include <winsock2.h>
#include <iostream>
#define MY_PORT 8888
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Init Winsock
WSADATA SData;
if (WSAStartup(0x0202, &SData) != 0)
{
cout << "KHONG THE KHOI DONG WINSOCK";
return 1;
}
//Init Socket
int listeningSocket;
if ((listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
perror("socket failed");
WSACleanup();
exit(EXIT_FAILURE);
}
//Set IP and PORT
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(MY_PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
//Bind
if (bind(listeningSocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
perror("bind failed");
closesocket(listeningSocket);
WSACleanup();
exit(EXIT_FAILURE);
}
//Listen
if (listen(listeningSocket, 5) < 0)
{
perror("listen failed");
closesocket(listeningSocket);
WSACleanup();
exit(EXIT_FAILURE);
}
//Accept
int new_socket;
sockaddr_in client_addr;
int nSizeAddr = sizeof(sockaddr);
if (new_socket = accept(listeningSocket, (struct sockaddr *)&client_addr, &nSizeAddr) < 0)
{
perror("accept failed");
closesocket(listeningSocket);
WSACleanup();
exit(EXIT_FAILURE);
}
while (1)
{
//recv
char buff[100];
if (recv(new_socket, buff, 100, 0) < 0)
{
perror("recv failed");
closesocket(listeningSocket);
WSACleanup();
exit(EXIT_FAILURE);
}
cout << buff << endl;
//send
string sndStr = "Da nhan";
if (send(new_socket, sndStr.c_str(), sndStr.size(), 0) < 0)
{
perror("send failed");
closesocket(listeningSocket);
WSACleanup();
exit(EXIT_FAILURE);
}
}
//close socket
closesocket(new_socket);
//Cleanup winsock
WSACleanup();
return 0;
}
Client:
#include <stdio.h>
#include <tchar.h>
#include <winsock2.h>
#include <iostream>
#include <string>
#define MY_PORT 8888
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Init winsock
WSADATA SData;
if (WSAStartup(0x0202, &SData) != 0)
{
cout << "KHONG THE KHOI DONG WINSOCK";
return 1;
}
//Init socket
int clientSocket;
if (clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) < 0)
{
perror("socket failed");
WSACleanup();
exit(EXIT_FAILURE);
}
//Set IP and PORT
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(MY_PORT);
server_addr.sin_addr.s_addr = INADDR_ANY; //Vi Client-Server chung 1 may
//Connect
if (connect(clientSocket, (SOCKADDR*)&server_addr, sizeof(server_addr)) < 0)
{
perror("connect failed");
closesocket(clientSocket);
WSACleanup();
exit(EXIT_FAILURE);
}
while (1)
{
//Send
string sndStr;
getline(cin, sndStr);
if (send(clientSocket, sndStr.c_str(), sndStr.size(), 0) < 0)
{
perror("send failed");
closesocket(clientSocket);
WSACleanup();
exit(EXIT_FAILURE);
}
//Recv
char buff[100];
if (recv(clientSocket, buff, 100, 0) < 0)
{
perror("recv failed");
closesocket(clientSocket);
WSACleanup();
exit(EXIT_FAILURE);
}
cout << buff << endl;
}
//Close socket
closesocket(clientSocket);
//Cleanup Winsock
WSACleanup();
}
It had an error at the accept step but I don't know what was the error name. How can I fix it?
The client_socket value turn out INVALID_SOCKET.
EDIT: New problem found - I still don't believe that using INADDR_ANY as a client address is good, but that isn't actually the problem here.
Instead the problem is in the line:
if (clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) < 0)
Note that < has higher precedence than =, so you assign a boolean value to clientSocket here. The similar line in the server code has parenthesis added that makes it work correctly.
Working line in the server code:
if ((listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
OLD ANSWER: I still stand by this, but it isn't the problem here
.
This is your problem right here (in the client):
server_addr.sin_addr.s_addr = INADDR_ANY; //Vi Client-Server chung 1 may
The way sockets work, is that you create a server socket that listens for incoming connections, and on creation you tell it who to accept connections from. Either from a specific address, a range of addresses, or just from anybody. The third option is the most common one here, and the one you have chosen in your server (bind to address INADDR_ANY).
This part is good.
However, when the client has to connect to the server, you cannot just say: "Connect to whatever!" and expect it to find your server (or anything at all). So when you try to connect with the client socket you have to give it the actual address of the server.
I found a example of how to do this:
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr*) & serv_addr, sizeof(serv_addr)) < 0)
Change the address to the correct one for your server, if localhost (127.0.0.1) don't work for you.

Hello World UDP multicast not working

I am trying to get a very basic hello world UDP sender and UDP multicast listener to work. I have a PC but have a virtual machine with the Linux OS CentOS. It has no problems connecting to the internet. The sender and listener are two separate programs, Eclipse is my environment.
The Sender...
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#define UDP_PORT 5403
#define UDP_GROUP "225.0.0.1" // 127.0.0.1
int main(int argc, char *argv[])
{
struct sockaddr_in addr;
int fd;
struct ip_mreq mreq;
char *message="Hello, World!";
int message_size = strlen(message) + 1;
// Create a UDP socket
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
perror("socket(...) ");
return -1;
}
// allow multiple sockets to use the same PORT number
u_int reuse_port = 1;
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &reuse_port, sizeof(reuse_port)) < 0)
{
perror("setsockopt(...) ");
return -1;
}
// set up destination address
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(UDP_GROUP);
addr.sin_port = htons(UDP_PORT);
printf("Begin sendto(...) infinite loop\n");
while (true)
{
printf("Sending message: %s, of size: %d\n", message, message_size);
if (sendto(fd, message, message_size, 0, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("sendto(...): ");
return -1;
}
// printf("message sent: %s\n", message);
sleep(1);
}
return 1;
}
The Listener...
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#define UDP_PORT 5403
#define UDP_GROUP "225.0.0.1"
#define MAX_BUFFER_SIZE 256
int main(int argc, char *argv[])
{
struct sockaddr_in addr;
int fd, nbytes;
socklen_t addrlen;
struct ip_mreq mreq;
char msgbuf[MAX_BUFFER_SIZE];
u_int reuse_port = 1;
// Create a socket
fd = socket(AF_INET,SOCK_DGRAM,0);
if (fd < 0)
{
perror("create socket failed");
return -1;
}
// allow multiple sockets to use the same PORT number
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_port, sizeof(reuse_port)) < 0)
{
perror("Reusing port number failed");
return -1;
}
// set up destination address
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(UDP_PORT);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind");
return -1;
}
// Set the recvfrom timeout after 1 s
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
{
perror("Error setting recvfrom timeout\n");
return -1;
}
// use setsockopt() to request that the kernel join a multicast group
mreq.imr_multiaddr.s_addr = inet_addr(UDP_GROUP);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
{
perror("setsockopt");
return -1;
}
addrlen = sizeof(addr);
printf("Begin recvfrom(...) infinite loop\n");
while (true)
{
nbytes = recvfrom(fd, msgbuf, MAX_BUFFER_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
if (nbytes < 0)
{
printf("recvfrom timeout\n");
}
else
{
printf("message received: %s\n", msgbuf);
}
}
return 1;
}
Every second, the sender program printf's "Sending message: Hello, World!, of size: 14" and every two seconds the receiver printf's "recvfrom timeout". I have set Wireshark to look at UDP traffic and I definitely see the sento data. The recvfrom is not getting any data. I have tried using many different Group IP's from 255.0.0.0 to 239.255.255.255, no change. I have tried many different ports, no change. Is their a special setup I need to do on my network card? I'm not sure what else to do. Small edit, the recvfrom and sendto message should not have "&".

FIXED: C++ server/client program: "Connection refused"

I'm writing a simple program to transfer files from server to client (both on the same computer for now). Using "telnet 127.0.0.1 [port]", I can succesfully get the file from the server, but when I run the client, the server refuses connection. I suspect that the client is trying to connect to the wrong address, but I'm not sure. I also added some GDB test output if it helps.
Server: "./server 0 4100 bigfile 100 0.01"
int main(int argc, char* argv[])
{
int sockfd;
if(!(sockfd = socket(AF_INET, SOCK_STREAM, 0))) {
error("Failed to create socket");
}
struct sockaddr_in serv_addr, cl_addr;
bzero((void*) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(atoi(argv[2]));
if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
error("Failed to bind");
}
listen(sockfd, 10);
int fd = open(argv[3], O_RDONLY);
int bufsize = atoi(argv[4]);
int packet_period = atoi(argv[5]);
size_t cl_addr_len = sizeof(cl_addr);
char *buf = new char[bufsize];
while(true) {
int sd;
cout << "waiting for client..." << endl;
if(!(sd = accept(sockfd, (struct sockaddr *) &cl_addr, (socklen_t*) &cl_addr_len))) {
error("Failed to acccept");
}
cout << "Accepted client connection" << endl;
lseek(fd, 0, SEEK_SET);
while(int n = read(fd, buf, bufsize)) {
cout << "Transferring " << buf << endl;
usleep(100000);
write(sd, buf, n);
}
}
}
Client: "./client 0 127.0.0.1 4100 bigfile stats"
int main(int argc, char *argv[])
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
error("Failed to open socket");
}
struct sockaddr_in serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[2]);
serv_addr.sin_port = htons(atoi(argv[3])); // I tried both htons and htonl
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
error("Failed to connect");
}
int n;
char buf[256];
int fd = open(argv[4], O_RDONLY);
while((n = read(sockfd, buf, 256)) > 0) {
printf("Received: %s\n", buf);
write(fd, buf, n);
}
close(fd);
close(sockfd);
return 0;
}
GDB Output:
(gdb) r
Starting program: [...]/client 0 127.0.0.1 4100 bigfile stats
Breakpoint 1, main (argc=6, argv=0x7fffffffde68) at client.cpp:31
31 if (connect(sockfd,(struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
(gdb) p serv_addr
$1 = {sin_family = 2, sin_port = 0, sin_addr = {s_addr = 16777343},
sin_zero = "\000\000\000\000\000\000\000"}
{sin_family = 2, sin_port = 0, sin_addr = {s_addr = 16777343}, sin_zero = "\000\000\000\000\000\000\000"}
So sin_port is wrong: it shouldn't be zero. The code that sets it is:
serv_addr.sin_port = htonl(atoi(argv[3]));
The problem is here. It should be
serv_addr.sin_port = htons(atoi(argv[3]));
Not today's problem but
printf("Received: %s\n", buf);
should be
printf("Received: %.*s\n", n, buf);

socket api not working inside of a class

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();
*/
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
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);