Skip to content

Commit

Permalink
🌟 cross-compilling windows/unix
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeFTN committed Dec 23, 2022
1 parent d8cd1c4 commit 1449eba
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
27 changes: 26 additions & 1 deletion client/socket/socket.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifdef _WIN32
#include <winsock2.h>
#include <Ws2tcpip.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
Binary file removed lib/liblto_plugin-0.dll
Binary file not shown.
39 changes: 36 additions & 3 deletions server/socket/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1449eba

Please sign in to comment.