Open MrRobertYuan opened 1 month ago
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define PORT 5555
#define BUFFER_SIZE 1024
void *server_thread(void *arg) {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = {0};
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Socket failed");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("Bind failed");
close(server_fd);
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("Listen failed");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("Server listening on 127.0.0.1:%d\n", PORT);
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("Accept failed");
close(server_fd);
exit(EXIT_FAILURE);
}
read(new_socket, buffer, BUFFER_SIZE);
printf("Server received: %s\n", buffer);
send(new_socket, "Hello from server", strlen("Hello from server"), 0);
close(new_socket);
close(server_fd);
return NULL;
}
void *client_thread(void *arg) {
sleep(1); // Ensure the server is listening before the client tries to connect
struct sockaddr_in serv_addr;
char *message = "Hello from client";
char buffer[BUFFER_SIZE] = {0};
int sock = 0;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socket creation error");
return NULL;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
perror("Invalid address / Address not supported");
close(sock);
return NULL;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("Connection failed");
close(sock);
return NULL;
}
send(sock, message, strlen(message), 0);
printf("Client sent: %s\n", message);
read(sock, buffer, BUFFER_SIZE);
printf("Client received: %s\n", buffer);
close(sock);
return NULL;
}
int main() {
pthread_t server_tid, client_tid;
if (pthread_create(&server_tid, NULL, server_thread, NULL) != 0) {
perror("Failed to create server thread");
exit(EXIT_FAILURE);
}
if (pthread_create(&client_tid, NULL, client_thread, NULL) != 0) {
perror("Failed to create client thread");
exit(EXIT_FAILURE);
}
pthread_join(server_tid, NULL);
pthread_join(client_tid, NULL);
return 0;
}
this code is also connect to localhost in same port, but it could run correctly , I don't konw why
Server address should bind at INADDR_ANY
When testing Python, some tests use socket failed. The reason is: when try to run a multi-thread code, which create a thread to accept on localhost:5555, and the other one connect to the same port, the connect one will continue to send message, but the accept one doesn't receive.
Change this question to a C code. This code cannot work on RuxOS. Can replace this one to apps/c/httpserver, and run
make ARCH=aarch64 A=apps/c/httpserver/ NET=y run
to reproduce the problem.