BSD 套接字连接时参数无效
当我尝试将客户端连接到服务器时,我不断收到无效参数错误。网上有几个线程说当 addrlen 不正确时可能会发生这种情况,但我尝试在计算长度后将其更改为文字值,但这不起作用。我也尝试过 strlen() 但没有成功。无论如何,相关代码-
服务器-
struct sockaddr name;
int main(int agrc, char** argv) {
int sock, new_sd, adrlen; //sock is this socket, new_sd is connection socket
name.sa_family = AF_INET;
strcpy(name.sa_data, "127.0.0.1");
adrlen = strlen(name.sa_data) + sizeof(name.sa_family);
//make socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
printf("\nBind error %m", errno);
exit(1);
}
//unlink and bind
unlink("127.0.0.1");
if(bind (sock, &name, adrlen) < 0)
printf("\nBind error %m", errno);
//listen
if(listen(sock, 5) < 0)
printf("\nListen error %m", errno);
//accept
new_sd = accept(sock, &name, (socklen_t*)&adrlen);
if( new_sd < 0) {
printf("\nAccept error %m", errno);
exit(1);
}
客户端-
int main(int agrc, char** argv) {
int sock, new_sd, adrlen;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
printf("\nserver socket failure %m", errno);
exit(1);
}
//stuff for server socket
name.sa_family = AF_INET;
strcpy(name.sa_data, "127.0.0.1");
adrlen = strlen(name.sa_data) + sizeof(name.sa_family);
cout<<"\nadrlen: "<<adrlen<<"\n";
if(connect(sock, &name, adrlen) < 0) {
printf("\nclient connection failure %m", errno);
exit(1);
}
我没有看到任何可能错误的地方,但我想我可能只是忽略了某些东西或没有意识到某些东西。任何帮助表示赞赏。
I keep getting an invalid argument error when I try to connect the client to the server. A couple threads online said this can happen when addrlen is not right, but I tried changing it to a literal value after counting the length and that did not work. I also tried just strlen() with no luck. Anyways, relevant code -
server -
struct sockaddr name;
int main(int agrc, char** argv) {
int sock, new_sd, adrlen; //sock is this socket, new_sd is connection socket
name.sa_family = AF_INET;
strcpy(name.sa_data, "127.0.0.1");
adrlen = strlen(name.sa_data) + sizeof(name.sa_family);
//make socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
printf("\nBind error %m", errno);
exit(1);
}
//unlink and bind
unlink("127.0.0.1");
if(bind (sock, &name, adrlen) < 0)
printf("\nBind error %m", errno);
//listen
if(listen(sock, 5) < 0)
printf("\nListen error %m", errno);
//accept
new_sd = accept(sock, &name, (socklen_t*)&adrlen);
if( new_sd < 0) {
printf("\nAccept error %m", errno);
exit(1);
}
client -
int main(int agrc, char** argv) {
int sock, new_sd, adrlen;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
printf("\nserver socket failure %m", errno);
exit(1);
}
//stuff for server socket
name.sa_family = AF_INET;
strcpy(name.sa_data, "127.0.0.1");
adrlen = strlen(name.sa_data) + sizeof(name.sa_family);
cout<<"\nadrlen: "<<adrlen<<"\n";
if(connect(sock, &name, adrlen) < 0) {
printf("\nclient connection failure %m", errno);
exit(1);
}
I don't see anything that could be wrong, but I guess I might just be overlooking something or unaware of something. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
真的吗?地址应该是 32 位 IPv4 地址,而不是字符串。
这是 AF_INET 的结构(来自此处):
Really? The address should be the 32 bit IPv4 address, not a string.
This is the structure for AF_INET (from here):
那么你的大问题是完全误解了 sockaddr 结构!
首先使用sockaddr_in。
然后,您将编写类似于以下的代码:
注意:htons 将短值从主机格式转换为网络格式。主机可以是大端或小端。网络是大端字节序。
Well your big problem is completely misunderstanding the sockaddr struct!
Firstly use sockaddr_in.
You would then write code more like the following:
Note: htons converts a short value from host to network format. Host could be big or little endian. Network is big endian.