Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "uv.h"
- #include "stdio.h"
- void on_new_connection(uv_stream_t *server, int status) {
- if (status == -1) {
- // error!
- return;
- }
- uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
- uv_tcp_init(server->loop, client);
- if (uv_accept(server, (uv_stream_t*) client) == 0) {
- fprintf(stderr, "New client\n");
- }
- else {
- uv_close((uv_handle_t*) client, NULL);
- }
- }
- int main(){
- uv_loop_t *loop = uv_loop_new();
- uv_tcp_t server;
- uv_tcp_init(loop, &server);
- struct sockaddr_in bind_addr = uv_ip4_addr("0.0.0.0", 7000);
- uv_tcp_bind(&server, bind_addr);
- int r = uv_listen((uv_stream_t*) &server, 128, on_new_connection);
- if (r) {
- fprintf(stderr, "Listen error %s\n", uv_err_name(uv_last_error(loop)));
- return 1;
- }
- uv_run(loop);
- }
Advertisement
Add Comment
Please, Sign In to add comment