1

这是我的第一个 linux 套接字程序。我正在使用服务器客户端模型。我已阅读教程并创建了服务器和客户端。但是我遇到了服务器端代码的问题。

我在接受行“'int*' 到 'socklen_t* {aka unsigned int*}' 的无效转换”收到此错误消息。
我该如何解决这个问题?下面是服务器代码。

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>

using namespace std;
#define MAX_SIZE 50

int main()
{
    int sock_descriptor, conn_desc;
    struct sockaddr_in serv_addr, client_addr;
    char buff[MAX_SIZE];
    sock_descriptor = socket(AF_INET, SOCK_STREAM, 0);
    if(sock_descriptor < 0)
        cout << "Failed creating socket" << endl;
    bzero((char *)&serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(1234);

    if(bind(sock_descriptor, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
        cout << "Failed to bind" << endl;

    listen(sock_descriptor, 5);
    cout << "Waiting for connection...\n" << endl;

    int size = sizeof(client_addr);
    conn_desc = accept(sock_descriptor, (struct sockaddr *)&client_addr, &size);
    if(conn_desc == -1)
        cout << "Failed accepting connection" << endl;
    else
        cout << "Connected" << endl;

    if(read(conn_desc, buff, sizeof(buff)-1) > 0)
        cout << "Received %s" << buff << endl;
    else
        cout << "Failed receiving" << endl;

    close(conn_desc);
    close(sock_descriptor);

    return 0;
}
4

2 回答 2

4

代替

int size = sizeof(client_addr);

socklen_t size = sizeof(client_addr);
于 2012-09-27T14:41:59.500 回答
0

也许您应该将 size 声明为

socklen_t size;
于 2012-09-27T14:41:59.693 回答