Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WinSock2.h>
- #include <stdio.h>
- #pragma comment(lib, "Ws2_32.lib")
- typedef unsigned int uint32;
- typedef unsigned short uint16;
- typedef float float32;
- struct IP_Endpoint {
- uint32 address;
- uint16 port;
- };
- enum {
- PACKETTYPE_JOIN = 1,
- PACKETTYPE_LEAVE = 2,
- PACKETTYPE_SCORE = 3
- };
- enum {
- PACKETTYPE_JOINRESULT = 1,
- PACKETTYPE_PLAYERSCORE = 2
- };
- #define MAX_CLIENTS 2
- IP_Endpoint client_endpoints[MAX_CLIENTS];
- int client_scores[MAX_CLIENTS];
- int main(int argc, char** argv) {
- WORD winsock_version = 0x202;
- WSADATA winsock_data;
- if (WSAStartup(winsock_version, &winsock_data)) {
- printf("WSAStartup failed: %d\n", WSAGetLastError());
- return -1;
- }
- int address_family = AF_INET;
- int type = SOCK_DGRAM;
- int protocol = IPPROTO_UDP;
- SOCKET sock = socket(address_family, type, protocol);
- if (sock == INVALID_SOCKET) {
- printf("socket failed: %d\n", WSAGetLastError());
- return -1;
- }
- SOCKADDR_IN local_address;
- local_address.sin_family = AF_INET;
- local_address.sin_port = htons(11000);
- local_address.sin_addr.s_addr = INADDR_ANY;
- if (bind(sock, (SOCKADDR*)&local_address, sizeof(local_address)) == SOCKET_ERROR) {
- printf("bind failed: %d\n", WSAGetLastError());
- return -1;
- }
- u_long enabled = 1;
- ioctlsocket(sock, FIONBIO, &enabled);
- char buffer[1024];
- while (true) {
- int flags = 0;
- SOCKADDR_IN from;
- int from_size = sizeof(from);
- int bytes_received = recvfrom(sock, buffer, 1024, flags, (SOCKADDR*)&from, &from_size);
- if (bytes_received != SOCKET_ERROR) {
- int error = WSAGetLastError();
- if (error == WSAEWOULDBLOCK) {
- printf("recvfrom returned SOCKET_ERROR, WSAGetLastError() %d", WSAGetLastError());
- } else {
- IP_Endpoint from_endpoint;
- from_endpoint.address = from.sin_addr.S_un.S_addr;
- from_endpoint.port = from.sin_port;
- switch (buffer[0]) {
- case PACKETTYPE_JOIN:
- printf("PACKETTYPE_JOIN\n");
- uint16 slot;
- slot = uint16(-1);
- for (uint16 i = 0; i < MAX_CLIENTS; ++i) {
- if (client_endpoints[i].address == 0) {
- slot = i;
- break;
- }
- }
- printf("Use slot %d\n", slot);
- buffer[0] = PACKETTYPE_JOINRESULT;
- if (slot != uint16(-1)) {
- buffer[1] = 1;
- memcpy(&buffer[2], &slot, 2);
- flags = 0;
- if (sendto(sock, buffer, 4, flags, (SOCKADDR*)&from, from_size) != SOCKET_ERROR) {
- client_endpoints[slot] = from_endpoint;
- client_scores[slot] = 0;
- }
- } else {
- buffer[1] = 0;
- flags = 0;
- sendto(sock, buffer, 2, flags, (SOCKADDR*)&from, from_size);
- }
- break;
- case PACKETTYPE_LEAVE:
- printf("PACKETTYPE_LEAVE\n");
- uint16 slot2;
- memcpy(&slot2, &buffer[1], 2);
- printf("Clear slot %d\n", slot2);
- client_endpoints[slot2] = {};
- break;
- case PACKETTYPE_SCORE:
- printf("PACKETTYPE_SCORE\n");
- uint16 slot3;
- memcpy(&slot3, &buffer[1], 2);
- int score;
- memcpy(&score, &buffer[3], 4);
- client_scores[slot3] = score;
- printf("Slot %d score: %d\n", slot3, score);
- SOCKADDR_IN to;
- to.sin_family = AF_INET;
- int to_length = sizeof(to);
- buffer[0] = PACKETTYPE_PLAYERSCORE;
- for (uint16 i = 0; i < MAX_CLIENTS; i++) {
- if (client_endpoints[i].address) {
- to.sin_addr.S_un.S_addr = client_endpoints[i].address;
- to.sin_port = client_endpoints[i].port;
- sendto(sock, buffer, 7, flags, (SOCKADDR*)&to, to_length);
- }
- }
- break;
- }
- }
- }
- memset(&buffer, 0, sizeof(buffer));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement