You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Nice hard work (for you and all your collaborators) to make this library.
I have been playing with the xev api for C code on linux today.
However, some trouble was encountered when running the same code with or without xev.
Client: curl 127.0.0.1:8000.
Without xev
C code - server
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<arpa/inet.h>#include<sys/socket.h>#include<netinet/in.h>#definePORT 8000 // The port number for the server to listen on
intmain() {
intserver_socket, client_socket;
structsockaddr_inserver_addr, client_addr;
socklen_tclient_addr_len=sizeof(client_addr);
// Create a socketserver_socket=socket(AF_INET, SOCK_STREAM, 0);
if (server_socket==-1) {
perror("Socket creation failed");
exit(1);
}
// Bind the socket to an IP address and portserver_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=INADDR_ANY; // Listen on all available network interfacesserver_addr.sin_port=htons(PORT);
if (bind(server_socket, (structsockaddr*)&server_addr, sizeof(server_addr)) ==-1) {
perror("Socket binding failed");
close(server_socket);
exit(1);
}
// Listen for incoming connectionsif (listen(server_socket, 10) ==-1) {
perror("Listen failed");
close(server_socket);
exit(1);
}
printf("Server listening on port %d...\n", PORT);
while (1) {
// Accept a client connectionclient_socket=accept(server_socket, (structsockaddr*)&client_addr, &client_addr_len);
if (client_socket==-1) {
perror("Accept failed");
continue; // Continue listening for other connections
}
printf("Connection accepted from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// Handle the client connection (in this example, we simply echo back received data)charbuffer[1024];
ssize_tbytes_received;
while ((bytes_received=recv(client_socket, buffer, sizeof(buffer), 0)) >0) {
buffer[bytes_received] ='\0'; // Null-terminate the received dataprintf("Received: %s", buffer);
// Echo back to the clientsend(client_socket, buffer, bytes_received, 0);
}
// Close the client socketclose(client_socket);
printf("Connection closed by %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
}
// Close the server socket (this part will not be reached in this example)close(server_socket);
return0;
}
Output
$> ./server_example
Server listening on port 8000...
Connection accepted from 127.0.0.1:42220
Received: GET / HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: curl/8.2.1
Accept: */*
Connection closed by 127.0.0.1:42220
With xev
C code - server
#include"xev.h"#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<netinet/in.h>#include<arpa/inet.h>#definePORT 8000 // The port number for the server to listen on
voidon_connection(xev_loop*loop, xev_completion*c, intresult, void*userdata) {
intserver_socket=*(int*)userdata;
intclient_socket=accept(server_socket, NULL, NULL);
if (client_socket==-1) {
perror("Accept failed");
return;
}
charbuffer[1024];
ssize_tbytes_received;
while ((bytes_received=recv(client_socket, buffer, sizeof(buffer), 0)) >0) {
buffer[bytes_received] ='\0'; // Null-terminate the received dataprintf("Received: %s", buffer);
// Echo back to the clientsend(client_socket, buffer, bytes_received, 0);
}
close(client_socket);
}
intmain() {
xev_looploop;
if (xev_loop_init(&loop) !=0) {
fprintf(stderr, "Failed to initialize event loop\n");
return1;
}
intserver_socket, client_socket;
structsockaddr_inserver_addr, client_addr;
socklen_tclient_addr_len=sizeof(client_addr);
// Create a socketserver_socket=socket(AF_INET, SOCK_STREAM, 0);
if (server_socket==-1) {
perror("Socket creation failed");
exit(1);
}
// Bind the socket to an IP address and portserver_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=INADDR_ANY; // Listen on all available network interfacesserver_addr.sin_port=htons(PORT);
if (bind(server_socket, (structsockaddr*)&server_addr, sizeof(server_addr)) ==-1) {
perror("Socket binding failed");
close(server_socket);
exit(1);
}
// Listen for incoming connectionsif (listen(server_socket, 10) ==-1) {
perror("Listen failed");
close(server_socket);
exit(1);
}
printf("Server listening on port %d...\n", PORT);
// Pass the server_socket as user data to the connection handlerintuser_data=server_socket;
xev_completioncompletion;
xev_completion_zero(&completion);
xev_threadpool_tasktask;
xev_threadpool_task_init(&task, (xev_task_cb)on_connection);
xev_threadpool_batchbatch;
xev_threadpool_batch_init(&batch);
xev_threadpool_batch_push_task(&batch, &task);
xev_threadpoolthreadpool;
xev_threadpool_init(&threadpool, NULL);
xev_threadpool_schedule(&threadpool, &batch);
xev_threadpool_deinit(&threadpool);
xev_loop_run(&loop, XEV_RUN_UNTIL_DONE);
xev_loop_deinit(&loop);
close(server_socket);
return0;
}
Output
$> ./server_example
Server listening on port 8000...
Accept failed: Bad file descriptor
Thanks, the C API is admittedly anemic right now and not well maintained beyond extremely basic tested usage. I'm sure there are many improvements to be made here.
Hi @mitchellh,
Nice hard work (for you and all your collaborators) to make this library.
I have been playing with the xev api for C code on linux today.
However, some trouble was encountered when running the same code with or without xev.
Client:
curl 127.0.0.1:8000
.Without
xev
C code - server
Output
With
xev
C code - server
Output
$> ./server_example Server listening on port 8000... Accept failed: Bad file descriptor
hold client, needed Ctrl+C to stopping.
Note:
gcc
get errors ondata
array on header.The text was updated successfully, but these errors were encountered: