2012-02-08 86 views
4

下面是我的Linux代碼。 我正在實施客戶端/服務器應用程序,下面是服務器.cpp文件。從int到socklen的無效轉換

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); 

我真的不知道該如何解決這個問題。 在此先感謝那些幫助! :)

回答

7

定義clientLen爲socklen_t的:

socklen_t clientLen; 

,而不是

int clientLen; 
2

變化,

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

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

您可能想閱讀[我如何編寫一個很好的答案](http://stackoverflow.com/help/how-to-answer)。 *爲什麼*和* how *一樣重要。 ;) – 2015-09-15 09:28:54