sockaddr_un中C中未使用的变量

时间:2019-03-18 13:31:52

标签: c sockets

我从多个来源复制了一个代码片段,但是由于某种原因,当我尝试实现时,我得到了saun变量的未使用变量错误。任何指针将不胜感激,谢谢!

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include "gfserver.h"


#include <sys/mman.h>

#define SOCKET_ADDR "sock_addr"
#define MAX_REQ_BUF 523

/* Main ========================================================= */
int main() {


  int sockfd;
  size_t n;
    struct sockaddr_un saun;
    int len;

    if ((sockfd =  socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
        fprintf(stderr, "ERROR opening socket");
        exit(1);
    }

    saun.sun_family = AF_UNIX;
    strcpy(saun.sun_path, SOCKET_ADDR);
    len = sizeof(saun.sun_family) + strlen(saun.sun_path);
    struct sockaddr* sockaddptr;
    sockaddptr = &saun;
    if(connect(sockfd, sockaddptr, len) < 0){
        fprintf(stderr, "ERROR connecting socket");
        exit(1);
    }
    char request_buffer[MAX_REQ_BUF] = {0};
    n = write(sockfd,request_buffer,MAX_REQ_BUF - 1);
    if (n < 0){
        fprintf(stderr, "ERROR writing to socket: %d\n",errno);
        exit(-1);
    }
}

https://users.cs.cf.ac.uk/Dave.Marshall/C/node28.html http://osr507doc.sco.com/en/netguide/dusockT.code_samples.html

上面的代码在一个名为webproxy.c的文件中。它实质上是服务器程序的一部分,该程序将通过IPC检查本地缓存。在另一个文件(simplecached)中,有另一个套接字。出于某种原因,直到我从webproxy.c中删除套接字代码,该相似的代码才会给出相同的错误,然后simplecached代码显示出一个相似的编译错误(对于相同类型的情况,未使用的变量)。我不愿意添加太多代码,以免承担太多的学校作业,因此我对此表示歉意。

编译错误:

make all 
mv gfserver.o gfserver.tmpo 
mv gfserver_noasan.o gfserver_noasan.tmpo
rm -rf *.o webproxy simplecached webproxy_noasan simplecached_noasan
mv gfserver.tmpo gfserver.o
mv gfserver_noasan.tmpo gfserver_noasan.o
cc -c -o webproxy.o -Wall --std=gnu99 -g3 -Werror -fPIC -fsanitize=address -fno-omit-frame-pointer webproxy.c
webproxy.c: In function ‘main’:
webproxy.c:15:23: error: storage size of ‘saun’ isn’t known
    struct sockaddr_un saun;
                       ^~~~
webproxy.c:15:23: error: unused variable ‘saun’ [-Werror=unused-variable]
cc1: all warnings being treated as errors
make: *** [webproxy.o] Error 1
Makefile:39: recipe for target 'webproxy.o' failed
"make all" terminated with exit code 2. Build might be incomplete.

15:21:58 Build Failed. 4 errors, 0 warnings. (took 177ms)

0 个答案:

没有答案