diff --git a/Makefile b/Makefile index e60b2e5..6d9bd4c 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,18 @@ ## Compile client & server CC=gcc + +# Uncomment this lines to use compile to windows on an unix based system +# CC=i686-w64-mingw32-gcc +# CFLAGS+= -lws2_32 + PROGRAMS=$(SERVER_BINARY) $(CLIENT_BINARY) # Uncomment this line in case of linker error while building -# CFLAGS=-fno-use-linker-plugin +# CFLAGS+= -fno-use-linker-plugin ifeq ($(OS),Windows_NT) - CFLAGS+= -l ws2_32 + CFLAGS+= -lws2_32 endif ## SERVER @@ -24,11 +29,11 @@ all: $(PROGRAMS) $(SERVER_BINARY): $(SERVER_OBJECTS) mkdir -p bin/ - $(CC) $^ -o $@ + $(CC) $^ -o $@ $(CFLAGS) $(CLIENT_BINARY): $(CLIENT_OBJECTS) mkdir -p bin/ - $(CC) $^ -o $@ + $(CC) $^ -o $@ $(CFLAGS) %.o:%.c $(CC) -c $^ -o $@ $(CFLAGS) diff --git a/client/socket/socket.c b/client/socket/socket.c index 6cdd3fa..33a4c71 100644 --- a/client/socket/socket.c +++ b/client/socket/socket.c @@ -1,5 +1,6 @@ #ifdef _WIN32 #include +#include #else #include #include @@ -16,7 +17,12 @@ int init_socket(int *client, int *sock) { #ifdef _WIN32 WSADATA wsa; - WSAStartup(MAKEWORD(2, 0), &wsa); + + if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) + { + printf("Failed. Error Code : %d",WSAGetLastError()); + return -1; + } #endif int port = 8080; @@ -30,12 +36,22 @@ int init_socket(int *client, int *sock) serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(port); +#ifdef _WIN32 + // Convert IP address from text to binary + if (InetPtonW(AF_INET, L"127.0.0.1", &serv_addr.sin_addr) <= 0) + { + printf("[x] Error while converting IP.\n"); + return -1; + } + +#else // Convert IP address from text to binary if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) { printf("[x] Error while converting IP.\n"); return -1; } +#endif // Connect client to server if((*client = connect(*sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) < 0) @@ -82,12 +98,21 @@ int receive_socket(int sock, char *buffer) { int error; +#ifdef _WIN32 + error = recv(sock, buffer, 1024, 0); + if (error < 0) + { + printf("[x] Error while receiving from server\n"); + return -1; + } +#else error = read(sock, buffer, 1024); if (error < 0) { printf("[x] Error while receiving from server\n"); return -1; } +#endif printf("< %s\n", buffer); return 0; diff --git a/lib/liblto_plugin-0.dll b/lib/liblto_plugin-0.dll deleted file mode 100644 index 2944099..0000000 Binary files a/lib/liblto_plugin-0.dll and /dev/null differ diff --git a/server/socket/socket.c b/server/socket/socket.c index 1a54070..42cdf8e 100644 --- a/server/socket/socket.c +++ b/server/socket/socket.c @@ -15,7 +15,12 @@ int init_socket(int *client, int *sock) { #ifdef _WIN32 WSADATA wsa; - WSAStartup(MAKEWORD(2, 0), &wsa); + + if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) + { + printf("Failed. Error Code : %d",WSAGetLastError()); + return -1; + } #endif const int port = 8080; @@ -36,15 +41,24 @@ int init_socket(int *client, int *sock) return -1; } +#ifdef _WIN32 + // Set address for reuse + if (setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)) < 0) + { + printf("[x] Error while setting address for reuse\n"); + return -1; + } +#else // Set address for reuse - if (setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, &opt, (socklen_t)sizeof(opt)) < 0) + if (setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { printf("[x] Error while setting address for reuse\n"); return -1; } +#endif // Bind address and port to socket - if (bind(*sock, (struct sockaddr *)&address, (socklen_t)addrlen) < 0) + if (bind(*sock, (struct sockaddr *)&address, addrlen) < 0) { printf("[x] Error while binding the socket.\n"); return -1; @@ -57,12 +71,22 @@ int init_socket(int *client, int *sock) return -1; } +#ifdef _WIN32 + // Accept client connection + if ((*client = accept(*sock, (struct sockaddr *)&address, &addrlen)) < 0) + { + printf("[x] Error while accepting the client.\n"); + return -1; + } +#else // Accept client connection if ((*client = accept(*sock, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) { printf("[x] Error while accepting the client.\n"); return -1; } +#endif + printf("[+] Client Connected!\n"); return 0; @@ -103,12 +127,21 @@ int receive_socket(int client, char *buffer) { int error; +#ifdef _WIN32 + error = recv(client, buffer, 1024, 0); + if (error < 0) + { + printf("[x] Error while receiving from client\n"); + return -1; + } +#else error = read(client, buffer, 1024); if (error < 0) { printf("[x] Error while receiving from client\n"); return -1; } +#endif printf("< %s\n", buffer); return 0;