这是我的第一个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;
}
答案 0 :(得分:2)
替换
int size = sizeof(client_addr);
与
socklen_t size = sizeof(client_addr);
答案 1 :(得分:0)
也许您应该将尺寸声明为
socklen_t size;