#if defined(__WIN32)
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif
#include <winsock2,h>
#include <ws2tcpip.h>
#pragma comment (lib, "ws2_32.lib")
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#endif
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#if defined(_WIN32)
#define ISVALIDSOCKET(s) ((s) != INVALIV_SOCKET)
#define CLOSESOCKET(s) closesocket(s)
#define GETSOCKETERRNO()(WSAGetLastError())
#else
#define ISVALIDSOCKET(s) ((s) >= 0)
#define CLOSESOCKET(s) close(s)
#define GETSOCKETERRNO()(errno)
#define SOCKET int
#endif
#if defined(_WIN32)
#include <conio.h>
#endif
int main(int argc, char* argv[])
{
#if defined(_WIN32)
WSADATA d;
if (WSAStartup(MAKEWORD( 2, 2), &d)){
fprintf(stderr, "Failed to initialize.\n");
return 1;
}
#endif
/// Configure ocal address for server.
printf("Configuring local address...\n");
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
struct addrinfo *bind_address;
getaddrinfo(0, "8080", &hints, &bind_address);
/// Create scket.
printf("Creting socket...\n");
SOCKET socket_listner;
socket_listner = socket(bind_address->ai_family, bind_address->ai_socktype, bind_address->ai_protocol);
if (!ISVALIDSOCKET(socket_listner)){
fprintf(stderr, "Coud not create socket (%d).\n", GETSOCKETERRNO());
return 1;
}
/// Bind socket
printf("Binding socket\n");
if (bind(socket_listner, bind_address->ai_addr, bind_address->ai_addrlen)){
fprintf(stderr, "Could not bind address (%d).\n", GETSOCKETERRNO());
return 1;
}
freeaddrinfo(bind_address);
/// Listen on the socket for connections
printf("Listening...\n");
if (listen(socket_listner, 10) < 0){
fprintf(stderr, "Listening failed: (%d).\n", GETSOCKETERRNO());
return 1;
}
fd_set master;
FD_ZERO(&master);
FD_SET(socket_listner, &master);
SOCKET max_socket = socket_listner;
printf("Waiting for connections...\n");
while(1){
fd_set reads;
reads = master;
if (select(max_socket, &reads, 0, 0, 0)){
fprintf( stderr, "Select() failed. (%d).\n", GETSOCKETERRNO());
return 1;
}
SOCKET i;
for(i = 1; i <= max_socket; ++i){
if (FD_ISSET(i, &reads)){
if (i == socket_listner){ /// Is it a new client?
struct sockaddr_storage client_address;
socklen_t client_len = sizeof(client_address);
SOCKET socket_client = accept( socket_listner, (struct sockaddr*) &client_address, &client_len);
if (!ISVALIDSOCKET(socket_client)){
fprintf(stderr, "Accept() failed with error: (%d).\n", GETSOCKETERRNO());
return 1;
}
FD_SET(socket_client, &master);
if (socket_client > max_socket)
max_socket = socket_client;
char address_buffer[100];
getnameinfo((struct sockaddr*)&client_address, client_len,
address_buffer, sizeof(address_buffer), 0, 0, NI_NUMERICHOST);
printf("New connection from %s \n", address_buffer);
}
else /// Is not e listener.
{
char read[1024];
int bytes_received = recv(i, read, 1024, 0);
if (bytes_received < 1){
FD_CLR( i, &master);
CLOSESOCKET(i);
continue;
}
/*** DO SOMETHING WITH THE BUFFER DATA ***/
}
} /// if FD_ISSET.
} /// for i to max socket.
}/// END while.
printf("Closing listening....\n");
CLOSESOCKET(socket_listner);
#if defined(_WIN32)
WSACleanup();
#endif
printf("Finished!");
return 0;
}