first commit

This commit is contained in:
Jezura777 2025-04-06 22:29:54 +02:00
commit c455885c35
3 changed files with 100 additions and 0 deletions

BIN
a.out Executable file

Binary file not shown.

BIN
dump.bin Normal file

Binary file not shown.

100
main.c Normal file
View file

@ -0,0 +1,100 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <wchar.h>
#include <stdint.h>
#define PORT 8080
#define BUFFER_SIZE 1024
enum PacketTypeClient {
LOGIN = 0x02
};
enum PacketTypeServer {
LOGIN_OK = 0x00
};
struct Packet {
uint8_t type;
union {
struct {
uint16_t padd1;
uint8_t padd2;
wchar_t name[256];
};
};
};
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE];
// Create socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Attach socket to the port
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Bind the socket to the address
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// Start listening for connections
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
// printf("Server listening on port %d\n", PORT);
while (1) {
// Accept a new connection
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
// Read data from the socket
ssize_t bytes_read;
struct Packet *p;
// while ((bytes_read = read(new_socket, buffer, BUFFER_SIZE)) > 0) {
while (1) {
bytes_read = read(new_socket, buffer, BUFFER_SIZE);
p = (struct Packet *)&buffer;
printf("type: %x; padd: %x %x", p->type, p->padd1, p->padd2);
fputws(p->name, stdout);
// Print the raw bytes to stdout
//fwrite(buffer, 1, bytes_read, stdout);
fflush(stdout); // Ensure the output is flushed immediately
}
// Close the socket after reading
close(new_socket);
}
// Close the server socket (this line will never be reached in this example)
close(server_fd);
return 0;
}