C 使用inet_addr和sendto编译错误

C 使用inet_addr和sendto编译错误,c,windows,C,Windows,我试图编译在中找到的这个开源程序,但它给出了一条错误消息。服务器程序将在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

我试图编译在中找到的这个开源程序,但它给出了一条错误消息。服务器程序将在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;
}

inet_addr只接受一个参数,我猜您得到的示例代码来自不同类型的系统。请参见此处:并将呼叫更改为:

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

至于sendto调用,您可能只需要将参数强制转换为编译器错误中给出的所需类型。

您是在尝试在linux设备上使用windows标头,还是我弄错了?您说该程序将在Ubuntu上运行,但从错误C2660、C2664来看,您似乎是在windows上编译的。如果您从已知在您的目标平台上工作的代码开始,并在该平台上开发它,您将获得更多的运气。如果你手边没有ubuntu的盒子,那就带上VirtualBox和ubuntu livecd图像。设置一个可以用来测试代码的虚拟机非常容易。
if ((si_other.sin_addr = inet_addr(SRV_IP) != INADDR_NONE) {