Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #undef UNICODE
- #define WIN32_LEAN_AND_MEAN
- #include <inttypes.h>
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <vcruntime.h>
- #include <windows.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- // Need to link with Ws2_32.lib
- #pragma comment(lib, "Ws2_32.lib")
- // #pragma comment (lib, "Mswsock.lib")
- #define DEFAULT_BUFLEN 512
- #define DEFAULT_PORT "27015"
- #define lengthof(x) (sizeof(x) / sizeof((x)[0]))
- void *safemalloc(size_t size) {
- void *block = malloc(size);
- if (!block) {
- (void)fputs("Memory allocation has failed.", stderr);
- abort();
- }
- return block;
- }
- uint64_t abs64(int64_t integer) { return integer < 0 ? -(uint64_t)integer : integer; }
- typedef struct {
- int64_t integer;
- const char *error;
- } StringToIntResult;
- StringToIntResult *stringToInt(const char *string) {
- StringToIntResult *result = safemalloc(sizeof(StringToIntResult));
- if (!string) {
- *result = (StringToIntResult){0, "Passed NULL."};
- return result;
- }
- if (string[0] == '\0') {
- *result = (StringToIntResult){0, "Invalid string."};
- return result;
- }
- bool startsWithNegativeSign = (string[0] == '-');
- if (startsWithNegativeSign) {
- string = string + 1;
- }
- uint64_t maxInt = abs64(startsWithNegativeSign ? INT64_MIN : INT64_MAX);
- uint8_t maxDigitNumber = snprintf( // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
- NULL, 0, "%" PRIu64, maxInt);
- size_t stringLength = strlen(string);
- if (stringLength == 0 || string[0] == '0' || stringLength > maxDigitNumber) {
- *result = (StringToIntResult){0, "Invalid string."};
- return result;
- }
- for (uint32_t i = 0; i < stringLength; ++i) {
- if (string[i] < '0' || string[i] > '9') {
- *result = (StringToIntResult){0, "The string contains non numerical characters."};
- return result;
- }
- }
- if (stringLength == maxDigitNumber) {
- char *maxString = safemalloc(maxDigitNumber + 1);
- (void)snprintf(maxString, // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
- maxDigitNumber + 1, "%" PRIu64, maxInt);
- for (uint32_t i = 0; i < stringLength; ++i) {
- if (string[i] > maxString[i]) {
- free(maxString);
- *result = (StringToIntResult){0, "The resulting integer is too large."};
- return result;
- }
- }
- free(maxString);
- }
- int64_t integer = strtoll(string, NULL, 10);
- if (startsWithNegativeSign) {
- integer = -integer;
- }
- *result = (StringToIntResult){integer, NULL};
- return result;
- }
- int __cdecl main(void) {
- WSADATA wsaData;
- int iResult;
- SOCKET ListenSocket = INVALID_SOCKET;
- SOCKET ClientSocket = INVALID_SOCKET;
- struct addrinfo *result = NULL;
- struct addrinfo hints;
- int iSendResult;
- char recvbuf[DEFAULT_BUFLEN];
- int recvbuflen = DEFAULT_BUFLEN;
- // Initialize Winsock
- iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (iResult != 0) {
- printf("WSAStartup failed with error: %d\n", iResult);
- return 1;
- }
- ZeroMemory(&hints, sizeof(hints));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- hints.ai_flags = AI_PASSIVE;
- // Resolve the server address and port
- iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
- if (iResult != 0) {
- printf("getaddrinfo failed with error: %d\n", iResult);
- WSACleanup();
- return 1;
- }
- // Create a SOCKET for connecting to server
- ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
- if (ListenSocket == INVALID_SOCKET) {
- printf("socket failed with error: %ld\n", WSAGetLastError());
- freeaddrinfo(result);
- WSACleanup();
- return 1;
- }
- // Setup the TCP listening socket
- iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
- if (iResult == SOCKET_ERROR) {
- printf("bind failed with error: %d\n", WSAGetLastError());
- freeaddrinfo(result);
- closesocket(ListenSocket);
- WSACleanup();
- return 1;
- }
- freeaddrinfo(result);
- iResult = listen(ListenSocket, SOMAXCONN);
- if (iResult == SOCKET_ERROR) {
- printf("listen failed with error: %d\n", WSAGetLastError());
- closesocket(ListenSocket);
- WSACleanup();
- return 1;
- }
- // Accept a client socket
- ClientSocket = accept(ListenSocket, NULL, NULL);
- if (ClientSocket == INVALID_SOCKET) {
- printf("accept failed with error: %d\n", WSAGetLastError());
- closesocket(ListenSocket);
- WSACleanup();
- return 1;
- }
- // No longer need server socket
- closesocket(ListenSocket);
- // Receive until the peer shuts down the connection
- int64_t sum = 0;
- while (1) {
- for (uint32_t i = 0; i < recvbuflen; ++i) {
- recvbuf[i] = '\0';
- }
- iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
- if (iResult < 0) {
- printf("recv failed with error: %d\n", WSAGetLastError());
- closesocket(ClientSocket);
- WSACleanup();
- return 1;
- }
- if (!iResult) {
- printf("Connection closing...\n");
- break;
- }
- FILE *fp;
- fp = fopen("lr5.txt", "a+");
- fprintf(fp, recvbuf);
- fclose(fp);
- printf("Bytes received: %d\n", iResult);
- // Echo the buffer back to the sender
- char *newlinePtr = strchr(recvbuf, '\n');
- if (newlinePtr) {
- *newlinePtr = '\0';
- }
- StringToIntResult *result = stringToInt(recvbuf);
- if (result->error) {
- iSendResult = send(ClientSocket, "Invalid input.\n", lengthof("Invalid input.\n"), 0);
- } else {
- sum += result->integer;
- char *buffer[50];
- sprintf(buffer, "%" PRId64 "\n", sum);
- iSendResult = send(ClientSocket, buffer, strlen(buffer), 0);
- }
- free(result);
- if (iSendResult == SOCKET_ERROR) {
- printf("send failed with error: %d\n", WSAGetLastError());
- closesocket(ClientSocket);
- WSACleanup();
- return 1;
- }
- printf("Bytes sent: %d\n", iSendResult);
- }
- // shutdown the connection since we're done
- iResult = shutdown(ClientSocket, SD_SEND);
- if (iResult == SOCKET_ERROR) {
- printf("shutdown failed with error: %d\n", WSAGetLastError());
- closesocket(ClientSocket);
- WSACleanup();
- return 1;
- }
- // cleanup
- closesocket(ClientSocket);
- WSACleanup();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement