Код: Выделить всё
//snippet begin
if (server_fd == -1) {
printf("Socket creation failed: %s...\n", strerror(errno));
return 1;
}
int reuse = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
printf("SO_REUSEADDR failed: %s \n", strerror(errno));
return 1;
}
struct sockaddr_in serv_addr = { .sin_family = AF_INET ,
.sin_port = htons(6379),
.sin_addr = { htonl(INADDR_ANY) },
};
if (bind(server_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) != 0) {
printf("Bind failed: %s \n", strerror(errno));
return 1;
}
int connection_backlog = 5;
if (listen(server_fd, connection_backlog) != 0) {
printf("Listen failed: %s \n", strerror(errno));
return 1;
}
printf("Waiting for a client to connect...\n");
client_addr_len = sizeof(client_addr);
while (1) {
client_fd = accept(server_fd, (struct sockaddr *) &client_addr, &client_addr_len);
if (client_fd == -1) {
printf("Accept failed: %s \n", strerror(errno));
return 1;
}
child_pid = fork();
switch (child_pid) {
case -1:
printf("Fork failed: %s \n", strerror(errno));
return 1;
case 0:
// child process
close(server_fd);
handle_client(client_fd);
//snippet end
Код: Выделить всё
//snippet begins
ssize_t bytes_read;
char client_request[1024];
char *command;
bytes_read = read(client_fd, client_request, sizeof(client_request));
if (bytes_read == -1) {
printf("Read failed: %s \n", strerror(errno));
return;
}
if (bytes_read < 2) {
handle_send_usage(client_fd);
return;
}
extract_command(client_request, command);
Код: Выделить всё
gef➤ x/gx client_request
0xffffffffee98: 0x0000ffff474e4950
Я использую
< pre class="lang-bash Prettyprint-override">
Код: Выделить всё
echo -n "PING" | nc localhost 6379
Подробнее здесь: https://stackoverflow.com/questions/790 ... at-the-end
Мобильная версия