Comments (4)
Would you be able to provide the code to an example program which demonstrates this behaviour? Thanks.
from dyad.
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;
}
from dyad.
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 .
from dyad.
@karl2016 It worked for me, but wouldn't that prevent you from using the ipv6? Also for some reason, when i used the ipv6 address i found using my ipconfig command in the commandline it said , that it couldn't resolve the host (before i changed it to AF_INET)
from dyad.
Related Issues (20)
- Windows: Socket created with dyad_listen() won't take IPv4 connections HOT 3
- Dyad forcibly closes sockets on shutdown
- Dev C++ Compile dyad.c errors HOT 1
- How to reconnect? HOT 1
- How to loop on active streams? HOT 1
- Python compatible? HOT 2
- Fail to get valid host ip address in Mac OS X 10.11.6
- You should use F_GETFL when setting O_NONBLOCK HOT 1
- any memory leak problem ? HOT 1
- About the Vec (v)->data[(v)->length++] = (val)
- Are there any plans for UDP support?
- Conan package for dyad
- Please add a timer event
- Feature request: Support UNIX socket HOT 1
- Sends message once per second HOT 1
- dyad_listen does not accept ipv4 addresses
- this is select IO will it be faster compare to epoll?
- Cygwin / mingw examples compilation HOT 2
- dyad c++ conversion HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dyad.