dyad

Asynchronous networking for C

Github星跟蹤圖

dyad.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.

主要指標

概覽
名稱與所有者rxi/dyad
主編程語言C
編程語言Python (語言數: 2)
平台
許可證MIT License
所有者活动
創建於2014-08-03 19:00:04
推送於2020-10-06 10:51:15
最后一次提交2016-08-28 10:57:21
發布數0
用户参与
星數1.4k
關注者數70
派生數223
提交數66
已啟用問題?
問題數20
打開的問題數12
拉請求數4
打開的拉請求數4
關閉的拉請求數4
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?