Still doesn't work after the advice provided by Fedor and Thiago.
Le mardi 18 avril 2017 19:27:03 UTC+2, Florent Cabret a écrit :
>
> Hi,
>
> I dont know what is wrong with my code but it trigger a memory exception
> in uv_process_tcp_connect_req at line req->cb(req,
> uv_translate_sys_error(err)); where it call a random address.
>
--
You received this message because you are subscribed to the Google Groups
"libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/d/optout.
// gcc -Os -s -nostartfiles -e _entry -I./include -L. tcp_client.c -o tcp_client -luv -lws2_32 -lpsapi -liphlpapi -luserenv
#include <stdint.h>
#include <stdio.h>
#include <uv.h>
void tcp_read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf){
puts(buf->base);
}
void tcp_alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf){
buf->base = malloc(suggested_size);
buf->len = suggested_size;
}
void tcp_write_cb(uv_write_t* req, int status){
printf("write -> %d\n", status);
uv_read_start(req->handle, tcp_alloc_cb, tcp_read_cb);
}
void tcp_connect_cb(uv_connect_t* req0, int status){
printf("connect -> %d\n", status);
uv_buf_t bufs[1];
uint8_t msg[] = "GET / HTTP/1.1\r\n"
"Host: google.fr\r\n"
"Connection: close\r\n"
"\r\n";
bufs[0].base = msg;
bufs[0].len = sizeof(msg);
uv_write_t req1;
uv_write(&req1, req0->handle, bufs, 1, tcp_write_cb);
}
void tcp_getaddrinfo_cb(uv_getaddrinfo_t* req0, int status, struct addrinfo* res){
printf("getaddrinfo -> %d\n", status);
uint8_t addr[16];
uv_ip4_name((struct sockaddr_in*)res->ai_addr, addr, sizeof(addr));
printf("ip4_name -> %s", addr);
uv_connect_t req1;
uv_tcp_connect(&req1, req0->data, res->ai_addr, tcp_connect_cb);
uv_freeaddrinfo(res);
}
void tcp_idle_cb(uv_idle_t *idle){
printf("idle -> %d\n", uv_now(idle->loop));
uv_idle_stop(idle);
}
void entry(){
uv_loop_t *loop = uv_default_loop();
uv_idle_t idle;
uv_idle_init(loop, &idle);
uv_idle_start(&idle, tcp_idle_cb);
uv_tcp_t tcp;
uv_tcp_init(loop, &tcp);
uv_getaddrinfo_t req;
req.data = &tcp;
uv_getaddrinfo(loop, &req, tcp_getaddrinfo_cb, "google.fr", "http", NULL);
uv_run(loop, UV_RUN_DEFAULT);
uv_loop_close(loop);
exit(0x00);
}