Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DWORD WINAPI networkCl(void *userData)
- {
- char server[256] = {0};
- strcpy(server, (char*)userData);
- int iError = 0;
- struct addrinfo *result = NULL, hints;
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- iError = getaddrinfo(server, "9904", &hints, &result);
- if (iError != 0) { printf("getaddrinfo failed: %d\n", iError); WSACleanup(); return 0; }
- thisSock = INVALID_SOCKET;
- thisSock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
- if (thisSock == INVALID_SOCKET) { printf("socket failed: %d\n", WSAGetLastError()); WSACleanup(); return 1; }
- iError = connect(thisSock, result->ai_addr, (int)result->ai_addr);
- freeaddrinfo(result);
- if (iError == SOCKET_ERROR) { printf("connect failed: %d\n", WSAGetLastError()); closesocket(thisSock); }
- if (thisSock == INVALID_SOCKET) { printf("unable to connect to server\n"); WSACleanup(); return 1; }
- sendPacket(IDBYTE_SETNAME, local.name);
- char recvData[256] = {0}; int recvBytes = 0;
- do
- {
- recvBytes = recv(thisSock, recvData, sizeof(recvData), 0);
- if (recvBytes > 0)
- {
- char idByte = recvData[0];
- char *rData = &recvData[1];
- printf("Packet received: ");
- for (int i = 0; i < strlen(recvData); i++)
- printf("0x%02X ", recvData[i]);
- printf("\n");
- if (idByte == IDBYTE_SETNAME)
- {
- strcpy(other.name, rData);
- chatPrint("other changed name to %s", rData);
- }
- else if (idByte == IDBYTE_CHATMSG)
- {
- chatPrint("%s", rData);
- }
- else if (idByte == IDBYTE_MOVEPOS)
- {
- float deltaX = 0;
- deltaX = atof(rData);
- other.sprite.Move((float)deltaX, 0);
- }
- else if (idByte == IDBYTE_SHOOTBT)
- {
- actionSounds[0].Play();
- }
- }
- } while (recvBytes > 0);
- closesocket(thisSock);
- WSACleanup();
- printf("End of client function.\n");
- return 0;
- }
- DWORD WINAPI networkSv(void *userData)
- {
- int iError = 0;
- struct addrinfo *result = NULL, hints;
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- hints.ai_flags = AI_PASSIVE;
- iError = getaddrinfo(NULL, "9904", &hints, &result);
- if (iError != 0) { printf("getaddrinfo failed: %d\n", iError); WSACleanup(); return 0; }
- thisSock = INVALID_SOCKET;
- thisSock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
- if (thisSock == INVALID_SOCKET) { printf("socket failed: %d\n", WSAGetLastError()); WSACleanup(); return 1; }
- iError = bind(thisSock, result->ai_addr, (int)result->ai_addr);
- if (iError == SOCKET_ERROR) { printf("bind failed: %d\n", WSAGetLastError()); freeaddrinfo(result); closesocket(thisSock); WSACleanup(); return 1; }
- if (listen(thisSock, SOMAXCONN) == SOCKET_ERROR)
- {
- printf("listen error: %d\n", WSAGetLastError());
- closesocket(thisSock);
- WSACleanup();
- return 1;
- }
- clSock = INVALID_SOCKET;
- clSock = accept(thisSock, NULL, NULL);
- if (clSock == INVALID_SOCKET)
- {
- printf("accept error: %d\n", WSAGetLastError());
- closesocket(thisSock);
- WSACleanup();
- return 1;
- }
- sendPacket(IDBYTE_SETNAME, local.name);
- char recvData[256] = {0}; int recvBytes = 0;
- do
- {
- recvBytes = recv(clSock, recvData, sizeof(recvData), 0);
- if (recvBytes > 0)
- {
- char idByte = recvData[0];
- char *rData = &recvData[1];
- printf("Packet received: ");
- for (int i = 0; i < strlen(recvData); i++)
- printf("0x%02X ", recvData[i]);
- printf("\n");
- if (idByte == IDBYTE_SETNAME)
- {
- strcpy(other.name, rData);
- chatPrint("other changed name to %s", rData);
- }
- else if (idByte == IDBYTE_CHATMSG)
- {
- chatPrint("%s", rData);
- }
- else if (idByte == IDBYTE_MOVEPOS)
- {
- float deltaX = 0;
- deltaX = atof(rData);
- other.sprite.Move((float)deltaX, 0);
- }
- else if (idByte == IDBYTE_SHOOTBT)
- {
- actionSounds[0].Play();
- }
- }
- } while (recvBytes > 0);
- closesocket(thisSock);
- WSACleanup();
- printf("End of server function.\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement