Add simple socket test (works)
This commit is contained in:
1 parent
f18ddb7451
commit
015a9ccca1
1 file changed
+145
@@ -0,0 +1,145 @@
|
||||
// test_etcp_link_simple.c - Simple test with two ETCP_LINKs via UDP sockets
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
|
||||
#define TEST_PORT 22345
|
||||
|
||||
typedef struct {
|
||||
int fd;
|
||||
struct sockaddr_in addr;
|
||||
} socket_t;
|
||||
|
||||
// Create UDP socket
|
||||
static socket_t* create_socket(int port) {
|
||||
socket_t* sock = calloc(1, sizeof(socket_t));
|
||||
if (!sock) return NULL;
|
||||
|
||||
sock->fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sock->fd < 0) {
|
||||
free(sock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int flags = fcntl(sock->fd, F_GETFL, 0);
|
||||
fcntl(sock->fd, F_SETFL, flags | O_NONBLOCK);
|
||||
|
||||
memset(&sock->addr, 0, sizeof(sock->addr));
|
||||
sock->addr.sin_family = AF_INET;
|
||||
sock->addr.sin_addr.s_addr = INADDR_ANY;
|
||||
sock->addr.sin_port = htons(port);
|
||||
|
||||
if (bind(sock->fd, (struct sockaddr*)&sock->addr, sizeof(sock->addr)) < 0) {
|
||||
perror("bind");
|
||||
close(sock->fd);
|
||||
free(sock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
printf("Socket created on port %d (fd=%d)\n", port, sock->fd);
|
||||
return sock;
|
||||
}
|
||||
|
||||
// Send data
|
||||
static int send_data(socket_t* sock, const char* data, size_t len, int dst_port) {
|
||||
struct sockaddr_in dst;
|
||||
memset(&dst, 0, sizeof(dst));
|
||||
dst.sin_family = AF_INET;
|
||||
dst.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
dst.sin_port = htons(dst_port);
|
||||
|
||||
ssize_t sent = sendto(sock->fd, data, len, 0, (struct sockaddr*)&dst, sizeof(dst));
|
||||
printf("[SEND] Sent %zd/%zu bytes to port %d\n", sent, len, dst_port);
|
||||
return (sent == len) ? 0 : -1;
|
||||
}
|
||||
|
||||
// Receive data
|
||||
static int recv_data(socket_t* sock, char* buffer, size_t max_len) {
|
||||
struct sockaddr_in from;
|
||||
socklen_t from_len = sizeof(from);
|
||||
|
||||
ssize_t received = recvfrom(sock->fd, buffer, max_len, 0, (struct sockaddr*)&from, &from_len);
|
||||
if (received < 0) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
printf("[RECV] No data available\n");
|
||||
} else {
|
||||
perror("[RECV] recvfrom");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("[RECV] Received %zd bytes from port %d\n", received, ntohs(from.sin_port));
|
||||
return received;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
printf("=== Simple Socket Test ===\n");
|
||||
|
||||
// Create sockets
|
||||
socket_t* server = create_socket(TEST_PORT);
|
||||
socket_t* client = create_socket(TEST_PORT + 1);
|
||||
|
||||
if (!server || !client) {
|
||||
fprintf(stderr, "Failed to create sockets\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
float start_time = (float)clock() / CLOCKS_PER_SEC;
|
||||
|
||||
// Send from client to server
|
||||
const char* test_data = "Hello, world!";
|
||||
printf("\n[SEND] Client sending: '%s'\n", test_data);
|
||||
if (send_data(client, test_data, strlen(test_data), TEST_PORT) < 0) {
|
||||
fprintf(stderr, "Send failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Receive on server
|
||||
usleep(50000);
|
||||
char buffer[2048];
|
||||
int received = recv_data(server, buffer, sizeof(buffer));
|
||||
if (received > 0) {
|
||||
buffer[received] = '\0';
|
||||
printf("[RECV] Server got: '%s'\n", buffer);
|
||||
|
||||
if (strcmp(buffer, test_data) == 0) {
|
||||
printf("✅ SUCCESS: Data matches!\n");
|
||||
} else {
|
||||
printf("❌ FAIL: Data mismatch\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Send response from server to client
|
||||
const char* reply = "Reply from server";
|
||||
printf("\n[SEND] Server sending reply: '%s'\n", reply);
|
||||
if (send_data(server, reply, strlen(reply), TEST_PORT + 1) < 0) {
|
||||
fprintf(stderr, "Send failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Receive on client
|
||||
usleep(50000);
|
||||
received = recv_data(client, buffer, sizeof(buffer));
|
||||
if (received > 0) {
|
||||
buffer[received] = '\0';
|
||||
printf("[RECV] Client got: '%s'\n", buffer);
|
||||
}
|
||||
|
||||
float end_time = (float)clock() / CLOCKS_PER_SEC;
|
||||
printf("\nTiming: %.3f ms\n", (end_time - start_time) * 1000);
|
||||
|
||||
// Cleanup
|
||||
close(server->fd);
|
||||
close(client->fd);
|
||||
free(server);
|
||||
free(client);
|
||||
|
||||
printf("\n=== Test completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user