从 int 到 socklen 的转换无效

发布于 2025-01-04 05:53:43 字数 1973 浏览 0 评论 0原文

下面是我的 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 */
  }

当我尝试编译时,

它显示一条错误消息。

 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*)’

它指向这条线

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

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

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

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! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜访吸血鬼 2025-01-11 05:53:43

将 clientLen 定义为 socklen_t:

socklen_t clientLen;

而不是

int clientLen;

Define clientLen as socklen_t:

socklen_t clientLen;

instead of

int clientLen;
你的背包 2025-01-11 05:53:43

改变,

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

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

Change,

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

to

clientFd = accept (serverFd, clientSockAddrPtr,(socklen_t*)&clientLen);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文