Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

dyad

> 编程语言
Open source

Asynchronous networking for C

1.5K stars0 likes0 views
WebsiteGitHub

About

Asynchronous networking for C

Overview

Dyad.c is an asynchronous networking library which aims to be lightweight, portable and easy to use. It can be used both to create small standalone servers and to provide network support to existing projects.

Getting started

The dyad.c and dyad.h files can be dropped into an existing project; if you're using Windows you will also have to link to ws2_32.

An overview of the API can be found at doc/api.md.

Usage examples can be found at example/.

Server example

A simple server which listens on port 8000 and echoes whatever is sent to it:

#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");
}

int main(void) {
  dyad_init();

  dyad_Stream *serv = dyad_newStream();
  dyad_addListener(serv, DYAD_EVENT_ACCEPT, onAccept, NULL);
  dyad_listen(serv, 8000);

  while (dyad_getStreamCount() > 0) {
    dyad_update();
  }

  dyad_shutdown();
  return 0;
}

Client example

A simple example program which connects to a daytime server and prints the response:

#include <stdio.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);
}

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_connect(s, "time-nw.nist.gov", 13);

  while (dyad_getStreamCount() > 0) {
    dyad_update();
  }
  
  dyad_shutdown();
  return 0;
}

License

This library is free software; you can redistribute it and/or modify it under the terms of the MIT license. See LICENSE for details.

GitHub Issues· 16 open

View all on GitHub
  • #30

    dyad_listen does not accept ipv4 addresses

    Updated Apr 25, 2019
  • #25

    Feature request: Support UNIX socket

    Updated Nov 19, 2018
  • #27

    Sends message once per second

    Updated Jul 22, 2018
  • #24

    Please add a timer event

    Updated Apr 18, 2018
  • #23

    Conan package for dyad

    Updated Jan 12, 2018
  • #22

    Are there any plans for UDP support?

    Updated Jan 4, 2018
  • #21

    About the Vec (v)->data[(v)->length++] = (val)

    Updated Dec 25, 2017
  • #18

    Fail to get valid host ip address in Mac OS X 10.11.6

    Updated Aug 9, 2017
  • #17

    Python compatible?

    Updated May 14, 2017
  • #16

    How to loop on active streams?

    Updated Feb 11, 2017

Highlights

  • •C

> Tags

C

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言