我尝试编译http://www.abc.se/~m6695/udp.html中找到的这个开源程序,但是它给出了一条错误消息。服务器程序将在Ubuntu上运行。
以下是修改后的代码:
#include "stdafx.h"
//#include <windows.h>
#include <process.h>
#include <stdio.h>
#include "winsock2.h"
#pragma comment(lib, "Ws2_32.lib")
#define BUFLEN 512
#define NPACK 10
#define PORT 9980
#define SRV_IP "999.999.999.999"
void diep(char *s)
{
perror(s);
exit(1);
}
int main(void) {
struct sockaddr_in si_other;
int s, i, slen = sizeof(si_other);
char buf[BUFLEN];
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
diep("socket");
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
//inet_addr (const) char *SRV_IP);
if (inet_addr(SRV_IP, &si_other.sin_addr) == 0) {
//if (inet_aton(SRV_IP, &si_other.sin_addr) == 0) {
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
for (i = 0; i < NPACK; i++) {
printf("Sending packet %d\n", i);
sprintf(buf, "This is packet %d\n", i);
if (sendto(s, buf, BUFLEN, 0, &si_other, slen) == -1)
diep("sendto()");
}
closesocket(s);
return 0;
}
它会出现以下错误:
Error 1 error C2660: 'inet_addr' : function does not take 2 arguments [filepath]
Error 2 error C2664: 'sendto' : cannot convert parameter 5 from 'sockaddr_in *' to 'const sockaddr *' [filepath]
答案 0 :(得分:0)
inet_addr只接受一个参数,我想你得到的示例代码来自不同类型的系统。请参阅此处:http://linux.die.net/man/3/inet_addr并将调用更改为:
if ((si_other.sin_addr = inet_addr(SRV_IP) != INADDR_NONE) {
对于sendto调用,您可能只需要将参数强制转换为编译器错误中给出的所需类型。