日期:2014-05-16  浏览次数:20568 次

unix域套接口用法
C/C++ code

#define MAXLINE 4096
int main()
{
    int unfd;
    char line[MAXLINE + 1];
    
    bzero(&unaddr, sizeof unaddr);
    unaddr.sun_family = AF_LOCAL;
    strcpy(unaddr.sun_path, "/tmp/sinfor");
    
#if 1
    if ((unfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0)
        err_sys("socket error\n");
#ifdef CLI
    if (connect(unfd, (SA*)&unaddr, sizeof unaddr) < 0)
        err_sys("connect error\n");//输出参数字符串然后exit
    printf("established\n");
    recv(unfd, line, MAXLINE, 0);
    printf("%s\n", line);
#elif defined(SERV)
    if (bind(unfd, (struct sockaddr*)&unaddr, sizeof unaddr) < 0)
        err_sys("tcp bind error\n");
    if (listen(unfd, LISTENQ) < 0)
        err_sys("listen error\n");
    //for(;;){
        connfd = accept(unfd, (struct sockaddr*)&unaddr, &len);
        printf("established\n");
        fgets(line, MAXLINE, stdin);
        send(unfd, line, strlen(line), 0);
    //}
#endif
    printf("closing\n");
    close(unfd);
#endif
}


#define CLI/编译客户程序
#define SERV//编译服务器程序
这段代码为什么在服务器端send去的字符串在客户端读不到,还是unix域套接口的用法不是这样,我还不是很懂unix域套接口的用法


------解决方案--------------------
探讨

引用:

引用:

send(unfd, line, strlen(line), 0);应该是send(connfd, line, strlen(line), 0);


这么改后,也不好用吗?

我就奇怪connfd这个变量是哪来的?