瀏覽代碼

adapt ENABLE_UDS

ithewei 4 年之前
父節點
當前提交
acde1d2819
共有 2 個文件被更改,包括 17 次插入5 次删除
  1. 9 3
      examples/tcp_echo_server.c
  2. 8 2
      examples/udp_echo_server.c

+ 9 - 3
examples/tcp_echo_server.c

@@ -82,10 +82,16 @@ static void on_accept(hio_t* io) {
 
 int main(int argc, char** argv) {
     if (argc < 2) {
-        printf("Usage: %s port\n", argv[0]);
+        printf("Usage: %s port|path\n", argv[0]);
         return -10;
     }
+    const char* host = "0.0.0.0";
     int port = atoi(argv[1]);
+#if ENABLE_UDS
+    if (port == 0) {
+        host = argv[1];
+    }
+#endif
 
 #if TEST_SSL
     hssl_ctx_init_param_t ssl_param;
@@ -110,9 +116,9 @@ int main(int argc, char** argv) {
 
     hloop_t* loop = hloop_new(0);
 #if TEST_SSL
-    hio_t* listenio = hloop_create_ssl_server(loop, "0.0.0.0", port, on_accept);
+    hio_t* listenio = hloop_create_ssl_server(loop, host, port, on_accept);
 #else
-    hio_t* listenio = hloop_create_tcp_server(loop, "0.0.0.0", port, on_accept);
+    hio_t* listenio = hloop_create_tcp_server(loop, host, port, on_accept);
 #endif
     if (listenio == NULL) {
         return -20;

+ 8 - 2
examples/udp_echo_server.c

@@ -26,13 +26,19 @@ static void on_recvfrom(hio_t* io, void* buf, int readbytes) {
 
 int main(int argc, char** argv) {
     if (argc < 2) {
-        printf("Usage: %s port\n", argv[0]);
+        printf("Usage: %s port|path\n", argv[0]);
         return -10;
     }
+    const char* host = "0.0.0.0";
     int port = atoi(argv[1]);
+#if ENABLE_UDS
+    if (port == 0) {
+        host = argv[1];
+    }
+#endif
 
     hloop_t* loop = hloop_new(0);
-    hio_t* io = hloop_create_udp_server(loop, "0.0.0.0", port);
+    hio_t* io = hloop_create_udp_server(loop, host, port);
     if (io == NULL) {
         return -20;
     }