C语言 C中非套接字错误的套接字操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25590965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Socket operation on non socket error in C
提问by PrathapB
I am new to socket programming... I tried this server side program
我是套接字编程的新手……我试过这个服务器端程序
#define BUFLEN 512
#define MYPORT 3456
void errorp(char* msg)
{
perror(msg);
exit(1);
}
int main()
{
struct sockaddr_in server, client;
int sock;
int slen = sizeof(server);
int clen = sizeof(client);
char *recvbuf, senbuf[BUFLEN] = {'h','e','l','l','o'};
if((sock = socket(AF_INET, SOCK_DGRAM, 0) == -1))
errorp("Socket creation failed");
printf("To the client: %s, %s", senbuf, " World");
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = MYPORT;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
if(bind(sock, (struct sockaddr*)&server, slen)==-1)
errorp("Socket Bind Failed");
if(recvfrom(sock, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*) &client, &clen) == -1)
errorp("recv from error");
printf("From the client: %s", recvbuf);
if(sendto(sock, senbuf, sizeof(senbuf), 0, (struct sockaddr*) &client, sizeof(client)) == -1)
errorp("Error in sending");
printf("To the client: %s", senbuf);
close(sock);
return 0;
}
There are no compilation errors but the output is
没有编译错误,但输出是
Socket Bind Failed: Socket operation on non-socket
To the client: hello, World
Please help me figure out where the mistake is? and help get rid of it
请帮我找出错误在哪里?并帮助摆脱它
回答by alk
The error message says it all: The socket isn't a (valid) socket.
错误消息说明了一切:套接字不是(有效)套接字。
This should make you look at the code creating the socket:
这应该让您查看创建套接字的代码:
if((sock = socket(AF_INET, SOCK_DGRAM, 0) == -1))
The code above 1st compares the result of the call to socket()to -1and then assigns the result of the comparison to sock. So it's either 0or 1. And the result of the call to socket()is lost.
上述第一代码进行比较的调用的结果socket()来-1,然后将比较的结果分配sock。因此,这两种0或1。并且调用的结果socket()丢失了。
The code shall look like this:
代码应如下所示:
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
as ==binds tighter then =.
因为==绑定得更紧=。
BTW, having used a Yoda-Condititionwould have avoided such kind of "typo":
顺便说一句,使用Yoda-Conditition可以避免这种“错字”:
if (-1 == (sock = socket(AF_INET, SOCK_DGRAM, 0)))
Also at least clenshall be of type socklen_tas its address is passed, to have a value written into it, which will fail miserably if the size of the expected socklen_twould be different from an int(which the code shown passes).
也至少clen应在socklen_t其地址被传递时具有类型,以便将一个值写入其中,如果预期的大小socklen_t与 an int(显示的代码通过)不同,则会失败。
回答by paxdiablo
if((sock = socket(AF_INET, SOCK_DGRAM, 0) == -1))
// \__________________________________/
You have your brackets in the wrong place. It's setting sockto a true/false value because ==is "more binding" than =. It should instead be:
你的括号放错了地方。它设置sock为 true/false 值,因为==它比=. 它应该是:
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
// \_____________________________________/
which sets sockto the return value from socket()and thencompares that to -1.
它设置sock为 from 的返回值socket(),然后将其与-1.
You also have no backing storage for recvbufwhich means your recvfrom(), once it starts working, will almost certainly do something bad.
你也没有后备存储,recvbuf这意味着你的recvfrom(),一旦开始工作,几乎肯定会做一些坏事。

