简体   繁体   English

c connect()错误-参数无效

[英]c connect() error - invalid argument

I'm writing a simple UDP client and an error returned as "invalid argument", but I don't know what's wrong. 我正在编写一个简单的UDP客户端,并以“无效参数”返回错误,但我不知道出了什么问题。 It is compiled on linux. 它在linux上编译。 My code is as below. 我的代码如下。

int udp_fd = -1;
struct sockaddr_in addr;
int port = 1701;

udp_fd = socket(AF_PPPOX, SOCK_DGRAM, 0);
if (udp_fd < 0) {
    printf("fail to get UDP socket\n");
    return 0;
}

memset((char *) &addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("31.25.210.118");
addr.sin_port = htons(port);

if(connect(udp_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0 ) {
    printf("UDP connect failed, errno = %s\n", strerror(errno));
    return 0;
}

You are using an AF_INET address with an AF_PPPoX socket. 您正在使用带有AF_PPPoX套接字的AF_INET地址。 This is mixing apples and parakeets. 这是在混合苹果和长尾小鹦鹉。

PPP stands for "point-to-point", there is no such thing as IP address in this domain. PPP代表“点对点”,在此域中没有IP地址之类的东西。 Pick your game. 选择你的游戏。 You either connect over the Internet, and use socket(AF_INET, ... , or connect over PPP and use one of the PPP protocols like PX_PROTO_OLAC or PX_PROTO_OPNS , and a corresponding socket address type ( sockaddr_pppolac or sockaddr_pppopns ) instead of sockaddr_in . 您可以通过Internet连接,并使用socket(AF_INET, ... ,或者通过PPP连接,并使用PPP协议之一,例如PX_PROTO_OLACPX_PROTO_OPNS ,以及相应的套接字地址类型( sockaddr_pppolacsockaddr_pppopns ),而不是sockaddr_in

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM