I found this:
/* basic network functionality, borrowed from OpenTTD */
#ifdef _MSC_VER
#include <direct.h>
#else
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "network.h"
#ifdef __BEOS__
#include <net/netdb.h>
#endif
// Haiku has select in an additional header
#ifndef FD_SET
#include <sys/select.h>
#endif
#include "../simconst.h"
#include "../simdebug.h"
#include "../simgraph.h"
#include "../dataobj/translator.h"
#include "../dataobj/umgebung.h"
#include "../utils/simstring.h"
#include "../tpl/vector_tpl.h"
#ifdef WIN32
#define socklen_t int
#endif
static bool network_active = false;
// local server cocket
static SOCKET my_socket = INVALID_SOCKET;
// local client socket
static SOCKET my_client_socket = INVALID_SOCKET;
static char pending[4096];
// to query all open sockets, we maintain this list
static vector_tpl<SOCKET> clients;
static uint32 our_client_id;
static uint32 active_clients;
// global client id
static uint32 client_id;
uint32 network_get_client_id()
{
return client_id;
}
/**
* Initializes the network core (as that is needed for some platforms
* @return true if the core has been initialized, false otherwise
*/
bool network_initialize()
{
if(!network_active) {
#ifdef WIN32
/* Let's load the network in windows */
WSADATA wsa;
if(WSAStartup(MAKEWORD(2, 0), &wsa) != 0) {
dbg->error("NetworkInitialize()","failed loading windows socket library");
return false;
}
#endif /* WIN32 */
}
network_active = true;
return true;
}
// open a socket or give a decent error message
const char *network_open_address( const char *cp)
{
// Network load. Address format e.g.: "net:128.0.0.1:13353"
char address[32];
static char err_str[256];
uint16 port = 13353;
const char *cp2 = strrchr(cp,':');
if(cp2!=NULL) {
port=atoi(cp2+1);
// Copy the address part
tstrncpy(address,cp,cp2-cp>31?31:cp2-cp+1);
cp = address;
}
struct sockaddr_in server_name;
#ifdef WIN32
server_name.sin_addr.s_addr = inet_addr(cp);
if((int)server_name.sin_addr.s_addr==-1) {// Bad address
#else
#if defined(__BEOS__)
struct hostent *theHost;
theHost = gethostbyname( cp );
if(theHost) {
server_name.sin_addr.s_addr = *(ulong *)theHost->h_addr_list[0];
}
else {// Bad address
#else
if(inet_aton(cp,&server_name.sin_addr)==0) { // Bad address
#endif
#endif
sprintf( err_str, "Bad address %s", cp );
return err_str;
}
server_name.sin_port=htons(port);
server_name.sin_family=AF_INET;
// now activate network
if( !network_initialize() ) {
return "Cannot init network!";
}
my_client_socket = socket(PF_INET,SOCK_STREAM,0);
if(my_client_socket==INVALID_SOCKET) {
return "Cannot create socket";
}
if(connect(my_client_socket,(struct sockaddr *)&server_name,sizeof(server_name))==-1) {
sprintf( err_str, "Cannot connect to %s", cp );
return err_str;
}
pending[0] = 0;
active_clients = 0;
return NULL;
}
// connect to address (cp), receive game, save to (filename)
const char *network_connect(const char *cp, const char *filename)
{
// open from network
const char *err = network_open_address( cp );
if( err==NULL ) {
int len;
dbg->warning( "network_connect", "send :" NET_TO_SERVER NET_GAME NET_END_CMD );
len = send( my_client_socket, NET_TO_SERVER NET_GAME NET_END_CMD, 9, 0 );
len = 64;
char buf[64];
network_add_client( my_client_socket );
if( network_check_activity( 60000, buf, len )!=INVALID_SOCKET ) {
// wait for sync message to finish
if( memcmp( NET_FROM_SERVER NET_GAME, buf, 7 )!=0 ) {
err = "Protocoll error (expecting " NET_GAME ")";
}
else {
int len = 0;
client_id = 0;
sscanf(buf+7, " %lu,%li" NET_END_CMD, &client_id, &len);
dbg->warning( "network_connect", "received: id=%li len=%li", client_id, len);
err = network_recieve_file( my_client_socket, filename, len );
}
}
else {
err = "Server did not respond!";
}
}
if(err) {
network_close_socket( my_client_socket );
}
return err;
}
// if sucessful, starts a server on this port
bool network_init_server( int port )
{
struct sockaddr_in name;
network_initialize();
my_socket = socket(PF_INET, SOCK_STREAM, 0);
if( my_socket==INVALID_SOCKET ) {
dbg->fatal("init_server()", "Fail to open
........................ and more .........................