could not connect to server on Win10
Truth1987 opened this issue · comments
I wrote a server and a client in local by example, but client always show "could not connect to server".
Would you be able to provide the code to an example program which demonstrates this behaviour? Thanks.
OK here is the code of server.
#include <stdlib.h>
#include "dyad.h"
static void onData(dyad_Event *e) {
dyad_write(e->stream, e->data, e->size);
}
static void onAccept(dyad_Event *e) {
dyad_addListener(e->remote, DYAD_EVENT_DATA, onData, NULL);
dyad_writef(e->remote, "Echo server\r\n");
}
static void onListen(dyad_Event *e) {
printf("listen: %s\n", e->msg);
}
int main(void) {
dyad_init();
dyad_Stream *serv = dyad_newStream();
dyad_addListener(serv, DYAD_EVENT_ACCEPT, onAccept, NULL);
dyad_addListener(serv, DYAD_EVENT_DATA, onData, NULL);
dyad_addListener(serv, DYAD_EVENT_LISTEN, onListen, NULL);
dyad_listen(serv, 8800);
while (dyad_getStreamCount() > 0) {
dyad_update();
}
dyad_shutdown();
return 0;
}
then, the following code is client.
#include <stdlib.h>
#include "dyad.h"
static void onConnect(dyad_Event *e) {
printf("connected: %s\n", e->msg);
}
static void onData(dyad_Event *e) {
printf("%s", e->data);
}
static void onError(dyad_Event *e) {
printf("error: %s\n", e->msg);
}
static void onTimeout(dyad_Event *e) {
printf("timeout: %s\n", e->msg);
}
int main(void) {
dyad_init();
dyad_Stream *s = dyad_newStream();
dyad_addListener(s, DYAD_EVENT_CONNECT, onConnect, NULL);
dyad_addListener(s, DYAD_EVENT_DATA, onData, NULL);
dyad_addListener(s, DYAD_EVENT_ERROR, onError, NULL);
dyad_addListener(s, DYAD_EVENT_TIMEOUT, onTimeout, NULL);
//dyad_connect(s, "time-nw.nist.gov", 13);
dyad_connect(s, "127.0.0.1", 8800);
while (dyad_getStreamCount() > 0) {
dyad_update();
}
dyad_shutdown();
return 0;
}
int dyad_listenEx(
dyad_Stream *stream, const char *host, int port, int backlog
) {
struct addrinfo hints, *ai = NULL;
int err, optval;
char buf[64];
dyad_Event e;
/* Get addrinfo */
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
because dyad_listenEx ai_family is AF_UNSPEC, socket addr may format to ip_v6. so ipv4 addr "127.0.0.1" cannot connect it.
solution: change hint.ai_family to AF_INET both in function "dyad_listenEx" and "dyad_connect", connect "127.0.0.1" will be work .