PHP-Quellcode
Die headerdatei crossnet.h:
PHP-Quellcode
- //header file for cross-platform networking
- #ifdef __WIN32__
- #include <winsock.h>
- #endif
- #ifdef __linux__
- #include <sys/types.h>
- #include <sys/socket.h>
- #endif
- void error_exit(const char *string)
- {
- #ifdef __WIN32__
- printf("%s: %d",string,WSAGetLastError());
- #endif
- #ifdef __linux__
- perror(string);
- #endif
- exit(-1);
- }
- int create_socket(int af, int type, int protocol)
- {
- #ifdef __WIN32__
- int err;
- LPWSADATA wsadat = (LPWSADATA) malloc(sizeof(LPWSADATA)); //LPWSADATA = wsadat* ==> malloc()
- if(WSAStartup(MAKEWORD(2,2),wsadat) != 0)
- {
- printf("WSAStartup() failed");
- exit(-1);
- }
- #endif
- int sock;
- if(sock = socket(af,type,protocol) < 0)
- {
- error_exit("socket creating failed");
- }
- return sock;
- }
- void con(int socket, int af, const char *ip, int port)
- {
- struct sockaddr_in server;
- unsigned long addr = inet_addr(ip);
- memset( &server, 0, sizeof (server)); //needed?
- memcpy((char *) &server.sin_addr, &addr, sizeof(addr));
- server.sin_family = af;
- server.sin_port = htons(port);
- if(connect(socket, (struct sockaddr *)&server, sizeof(server)) != 0)
- {
- error_exit("connect() failed");
- }
- }