45 lines
1.2 KiB
C
45 lines
1.2 KiB
C
#include "include/networking.h"
|
|
#include "include/defs.h"
|
|
|
|
//C89 stuff
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
//Networking Stuff
|
|
#ifdef _WIN32
|
|
#include <winsock.h>
|
|
#else
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#endif
|
|
|
|
int createTCPSocket(char *ipaddr, unsigned short server_port)
|
|
{
|
|
|
|
int sock; /* Socket descriptor */
|
|
struct sockaddr_in ServAddr; /* Address Structure */
|
|
|
|
#ifdef _WIN32
|
|
WSADATA wsaData; /* Winsock struct. */
|
|
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)/* Winsock 2.0 DLL. */
|
|
DieWithError("WSAStartup() failed");
|
|
#endif
|
|
|
|
if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
|
|
DieWithError("socket() failed");
|
|
|
|
memset(&ServAddr, 0, sizeof(ServAddr));
|
|
ServAddr.sin_family = AF_INET;
|
|
ServAddr.sin_addr.s_addr = inet_addr(ipaddr);
|
|
ServAddr.sin_port = htons(server_port);
|
|
|
|
if (connect(sock, (struct sockaddr *) &ServAddr, sizeof(ServAddr)) < 0) {
|
|
return -1;
|
|
} else {
|
|
return sock;
|
|
}
|
|
}
|