2011-11-24 47 views
0

我试图编译在http://www.abc.se/~m6695/udp.html发现这个开源的程序,但它给了一个错误消息。服务器程序将在Ubuntu上运行。编译错误与inet_addr和SENDTO

下面是修改代码:

#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

你们是不是要在Linux中使用的窗口标题,或者是我错了吗? – Constantinius

+0

你说这个程序将在Ubuntu上运行,但是从错误('C2660','C2664')看来你正在编译窗口。如果您从已知可在您的目标平台上运行的代码开始,并且在该平台上开发它,那么您将会有更多的运气。如果你没有一个Ubuntu的方便,抓住'VirtualBox'和一个Ubuntu livecd图像。设置可用于测试代码的虚拟机非常简单。 – Mark

回答

0

inet_addr只需一个说法,我想你了示例代码是从一个不同类型的系统。在这里看到:http://linux.die.net/man/3/inet_addr和呼叫更改为类似:

if ((si_other.sin_addr = inet_addr(SRV_IP) != INADDR_NONE) { 

对于sendto调用,你可能只需要参数转换为所需要的类型,在编译器错误给出。