简体   繁体   English

iOS中的UDP套接字errno

[英]UDP socket errno in iOS

I am creating a UDP socket, and attempting to send to an existing server in the code below: 我正在创建UDP套接字,并尝试通过以下代码发送到现有服务器:

struct sockaddr_in servAddr;
memset(&servAddr, 0, sizeof(servAddr));  
servAddr.sin_family = AF_INET;              
servAddr.sin_addr.s_addr = inet_addr(SERVER IP ADDRESS GOES HERE);
servAddr.sin_port   = htons(port);

int testSock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

unsigned char byteData;
int sent;
unsigned int servSize = sizeof(servAddr);

if((sent = sendto(testSock, &byteData, 1, 0, (struct sockaddr *)&servAddr,  (socklen_t)&servSize)) < 0){
    NSLog(@"Error sending to server: %d %d", errno, sent);
}

Every time "sendto" returns -1, and errno is set to 63. I have never encountered this error before. 每次“ sendto”返回-1,并且errno设置为63。以前我从未遇到过此错误。

I can say with complete confidence that there is nothing wrong with the server, or the IP address or port provided. 我完全可以肯定地说服务器或提供的IP地址或端口没有问题。 It has to be client-side. 它必须在客户端。

63 is 'filename too long'. 63是“文件名太长”。 In this case it is the sockaddr that appears too long to the kernel, and that is because you are passing a pointer as the length, instead of the actual length. 在这种情况下,sockaddr在内核中显得太长,这是因为您要传递一个指针作为长度,而不是实际长度。 The final parameter to sendto() isn't a pointer, it is a value. sendto()的最后一个参数不是指针,而是一个值。 Remove the '&'. 去除 '&'。

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

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