我正在制作一个简单的UDP白天客户端。当我尝试编译时,我得到了这个。我已经尝试过这些,看看它们是否会起作用,但这并不能带来任何好处。我应该如何用我所拥有的东西(或者我错过了应该放在那里的东西)来输入这些参数?
UDPday2.cpp:57:86: error: invalid conversion from ‘int*’ to ‘socklen_t* {aka unsigned int*}’ [-fpermissive]
n = recvfrom(sockfd, buf,(int) sizeof(buf), 0, (sockaddr*)&serveraddr, &serverlen);
^
In file included from UDPday2.cpp:6:0:
/usr/include/i386-linux-gnu/sys/socket.h:174:16: error: initializing argument 6 of ‘ssize_t recvfrom(int, void*, size_t, int, sockaddr*, socklen_t*)’ [-fpermissive]
extern ssize_t recvfrom (int __fd, void *__restrict __buf, size_t __n,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define BUFSIZE 1024
void error(char *msg) {
perror(msg);
exit(0);
}
int main(int argc, char **argv) {
int sockfd, portno, n;
int serverlen;
struct sockaddr_in serveraddr;
struct hostent *server;
char *hostname;
char buf[BUFSIZE];
portno = 13;
if(argc == 1)
hostname = "localhost";
else
hostname = argv[1];
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
error("socket");
server = gethostbyname(hostname);
if (server == NULL) {
fprintf(stderr," no host %s\n", hostname);
exit(0);
}
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serveraddr.sin_addr.s_addr, server->h_length);
serveraddr.sin_port = htons(portno);
serverlen = sizeof(serveraddr);
n = sendto(sockfd, buf,(int)strlen(buf)+1, 0,(sockaddr*)&serveraddr , serverlen);
if (n < 0)
error("ERROR send");
n = recvfrom(sockfd, buf,(int) sizeof(buf), 0, (sockaddr*)&serveraddr, &serverlen);
if (n < 0)
error("ERROR recvfrom");
printf(" %s", buf);
return 0;
}
答案 0 :(得分:1)
serverlen
需要声明为unsigned
或socklen_t
。您应该能够从错误消息中推断出它。