Linux 从 int 到 socklen 的无效转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9197689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:28:33  来源:igfitidea点击:

invalid conversion from int to socklen

linuxubuntutcpsocklen-t

提问by Andres

Below is my code for Linux. I am implementing a client/server application and below is the server .cpp file.

下面是我的 Linux 代码。我正在实现一个客户端/服务器应用程序,下面是服务器 .cpp 文件。

int main()
{
 int serverFd, clientFd, serverLen, clientLen;
struct sockaddr_un serverAddress;/* Server address */
struct sockaddr_un clientAddress; /* Client address */
struct sockaddr* serverSockAddrPtr; /* Ptr to server address */
struct sockaddr* clientSockAddrPtr; /* Ptr to client address */

/* Ignore death-of-child signals to prevent zombies */
signal (SIGCHLD, SIG_IGN);

serverSockAddrPtr = (struct sockaddr*) &serverAddress;
serverLen = sizeof (serverAddress);

clientSockAddrPtr = (struct sockaddr*) &clientAddress;
clientLen = sizeof (clientAddress);

/* Create a socket, bidirectional, default protocol */
serverFd = socket (AF_LOCAL, SOCK_STREAM, DEFAULT_PROTOCOL);
serverAddress.sun_family = AF_LOCAL; /* Set domain type */
strcpy (serverAddress.sun_path, "css"); /* Set name */
unlink ("css"); /* Remove file if it already exists */
bind (serverFd, serverSockAddrPtr, serverLen); /* Create file */
listen (serverFd, 5); /* Maximum pending connection length */   

readData();

while (1) /* Loop forever */
  {
    /* Accept a client connection */
    clientFd = accept (serverFd, clientSockAddrPtr, &clientLen);

    if (fork () == 0) /* Create child to send recipe */
      {
        printf ("");
    printf ("\nRunner server program. . .\n\n");
    printf ("Country Directory Server Started!\n");

        close (clientFd); /* Close the socket */
        exit (/* EXIT_SUCCESS */ 0); /* Terminate */
      }
    else
      close (clientFd); /* Close the client descriptor */
  }

}

}

When i tried to compile it displays an error message which shows.

当我尝试编译时,它会显示一条错误消息。

 server.cpp:237:67: error: invalid conversion from ‘int*' to ‘socklen_t*'
server.cpp:237:67: error:   initializing argument 3 of ‘int accept(int, sockaddr*, socklen_t*)'

It points to this line

它指向这条线

clientFd = accept (serverFd, clientSockAddrPtr, &clientLen);

I do not actually know how to solve this problem. Thanks in advance to those who helped! :)

我实际上不知道如何解决这个问题。在此先感谢那些提供帮助的人!:)

采纳答案by Marko Kevac

Define clientLen as socklen_t:

将 clientLen 定义为 socklen_t:

socklen_t clientLen;

instead of

代替

int clientLen;

回答by user3698303

Change,

改变,

clientFd = accept (serverFd, clientSockAddrPtr, &clientLen);

to

clientFd = accept (serverFd, clientSockAddrPtr,(socklen_t*)&clientLen);