關於 UnixDomaiSocket 中 send(; ; ; ) 的第二個參數使用 char *str 的可行性驗證

來源:互聯網
上載者:User

    今天要寫的內容是來實驗 send( , ,) 中的第二個參數是否可以使用 char *str 指標。

    結論是:可以!

    服務端的代碼如下:

/* 2012-06-07 - 代碼是在 http://beej.us/guide/bgipc/output/html/multipage/unixsock.html 基礎上修改的。原始的代碼下載 * echos.c -- the echo server for echoc.c; demonstrates unix sockets */#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/un.h>#define SOCK_PATH "/home/tian/mysocket"int main(void){int s, s2, t, len;struct sockaddr_un local, remote;char str[100];if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {perror("socket");exit(1);}local.sun_family = AF_UNIX;strcpy(local.sun_path, SOCK_PATH);unlink(local.sun_path);len = strlen(local.sun_path) + sizeof(local.sun_family);if (bind(s, (struct sockaddr *)&local, len) == -1) {perror("bind");exit(1);}if (listen(s, 5) == -1) {perror("listen");exit(1);}for(;;) {int done, n;printf("Waiting for a connection...\n");t = sizeof(remote);if ((s2 = accept(s, (struct sockaddr *)&remote, &t)) == -1) {perror("accept");exit(1);}printf("Connected.\n");do {memset(str, 0, sizeof(str));n = recv(s2, str, 5, 0);if (n < 0) {perror("recv");break;}else if (n == 0){break;}//printf("echo> %s\n", str);//} while (1);close(s2);}return 0;}

    服務端執行結果:

    用戶端代碼如下:

/* 2012-06-07 - 代碼是在 http://beej.us/guide/bgipc/output/html/multipage/unixsock.html 基礎上修改的。原始的代碼下載** echoc.c -- the echo client for echos.c; demonstrates unix sockets*/#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/un.h>#define SOCK_PATH "/home/tian/mysocket"int main(void){    int s, t, len;    struct sockaddr_un remote;    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {        perror("socket");        exit(1);    }    printf("Trying to connect...\n");    remote.sun_family = AF_UNIX;    strcpy(remote.sun_path, SOCK_PATH);    len = strlen(remote.sun_path) + sizeof(remote.sun_family);    if (connect(s, (struct sockaddr *)&remote, len) == -1) {        perror("connect");        exit(1);    }    printf("Connected.\n");     int num = 5;    char *str = (char*)calloc(5, sizeof(char));        while(1)    {         sprintf(str, "%d", num); //將 num 以10進位格式化到 str//printf("strlen---%d\n", strlen(str)); // 列印 str 的長度//if (send(s, str, strlen(str), 0) == -1)    {        perror("send");        exit(1);    }    printf("num---%d\n", num); // 列印 num,以此判斷 while() 迴圈是否正確執行    num--;    if(num == 0) break;    }    if (str != NULL)    {        str = NULL;        free(str); // 釋放 str 佔用的記憶體空間    }    close(s);    return 0;}

    用戶端執行結果:

     根據服務端與用戶端執行結果,可以判斷出:程式執行正常。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.