У меня есть простой сервер C и клиент Python. Когда я отправляю сообщение с сервера, вызов клиента Python Recv() никогда не возвращается, хотя сервер, кажется, успешно отправляет данные. Все проверки ошибок проходят с обеих сторон, поэтому я не знаю, почему клиент зависает.
Сервер C (server.c):
#include
#include
#include
#include
#include
#include
#include
#define MEMBER_COUNT 1
#define MAX_SIZE 1024
#define SERVER_PORT 8080
#define RECV_TIMEOUT 2
#define MSG_POK "POK"
#define MSG_PINC "PINC"
typedef struct Client {
int fd;
struct sockaddr_in addr;
} Client_t;
typedef struct Clients {
Client_t *clients;
size_t size;
size_t capacity;
} Clients_Arr_t;
char question[MAX_SIZE];
char password[MAX_SIZE];
int server_fd = -1;
Clients_Arr_t clients_arr;
int favor_count = 0;
void clients_arr_add(Clients_Arr_t *clients_arr, int client_fd, struct sockaddr_in addr) {
if (clients_arr->size == clients_arr->capacity) {
size_t new_capacity = clients_arr->capacity == 0 ? 1 : clients_arr->capacity * 2;
Client_t *new_clients = realloc(clients_arr->clients, new_capacity * sizeof(Client_t));
clients_arr->clients = new_clients;
clients_arr->capacity = new_capacity;
}
clients_arr->clients[clients_arr->size++] = (Client_t){client_fd, addr};
}
void clients_arr_free(Clients_Arr_t *clients_arr) {
if (!clients_arr->clients) return;
for (size_t i = 0; i < clients_arr->size; i++) close(clients_arr->clients.fd);
free(clients_arr->clients);
clients_arr->clients = NULL;
clients_arr->size = 0;
clients_arr->capacity = 0;
}
void clients_arr_init(Clients_Arr_t *clients_arr) {
clients_arr->size = 0;
clients_arr->clients = malloc(sizeof(Client_t));
if (!clients_arr->clients) {
fprintf(stderr, "[ERROR] Initial allocation failed.\n");
clients_arr->capacity = 0;
return;
}
clients_arr->capacity = 1;
}
void read_question(void) {
printf("[PROMPT] Enter your question: ");
if (!fgets(question, MAX_SIZE, stdin)) {
fprintf(stderr, "[ERROR] Failed to read question.\n");
exit(1);
}
question[strcspn(question, "\n")] = '\0';
}
void read_password(void) {
printf("[PROMPT] Enter the required password: ");
if (!fgets(password, MAX_SIZE, stdin)) {
fprintf(stderr, "[ERROR] Failed to read password.\n");
exit(1);
}
password[strcspn(password, "\n")] = '\0';
}
int server_init(void) {
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "[ERROR] Could not open socket.\n");
goto err;
}
struct sockaddr_in address = {0};
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(SERVER_PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
fprintf(stderr, "[ERROR] Could not bind to host machine.\n");
goto err;
}
if (listen(server_fd, MEMBER_COUNT) < 0) {
fprintf(stderr, "[ERROR] Could not start listener.\n");
goto err;
}
return 0;
err:
return -1;
}
int server_receive_pass(int client) {
char buff[MAX_SIZE + 1];
ssize_t size;
if ((size = recv(client, buff, MAX_SIZE, 0))
Подробнее здесь: https://stackoverflow.com/questions/792 ... ket-server
Клиент Python «recv()» зависает при получении данных с сервера сокетов C ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Клиент Python «recv()» зависает при получении данных с сервера сокетов C
Anonymous » » в форуме Python - 0 Ответы
- 7 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Клиент Python «recv()» зависает при получении данных с сервера сокетов C
Anonymous » » в форуме Python - 0 Ответы
- 13 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Python запрашивает POST-запрос зависает при использовании с библиотекой веб-сокетов
Anonymous » » в форуме Python - 0 Ответы
- 20 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Python запрашивает POST-запрос зависает при использовании с библиотекой веб-сокетов
Anonymous » » в форуме Python - 0 Ответы
- 19 Просмотры
-
Последнее сообщение Anonymous
-