gpt4 book ai didi

sockets - 在openSUSE中工作的套接字在Debian中不工作吗?

转载 作者:行者123 更新时间:2023-12-03 11:57:14 24 4
gpt4 key购买 nike

我有一个在OpenSUSE中工作的C/C++ TCP客户端,但在Debian中却没有。
我正在使用nc -l 4242作为服务器。
然后,我在Debian系统(Sid)上与./my_client 127.0.0.1 4242连接,使用连接功能时它将失败。

您可以使用Debian或其他操作系统来确认是否也存在相同的错误吗?
问题是从哪里来的?

这是代码:

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>

void do_server(int s)
{
write(s, "client connected\n", strlen("client connected\n"));
close(s);
}

int main(int ac, char **av)
{
struct protoent *pe;
struct sockaddr_in sin;
int s;

if (ac != 3)
{
std::cerr << "Usage: ./client ip port" << std::endl;
return EXIT_FAILURE;
}
pe = getprotobyname("TCP");
if ((s = socket(AF_INET, SOCK_STREAM, pe->p_proto)) == -1)
{
std::cerr << "Error: socket" << std::endl;
return EXIT_FAILURE;
}
sin.sin_family = AF_INET;
sin.sin_port = htons(atoi(av[2]));
sin.sin_addr.s_addr = inet_addr(av[1]);
if (connect(s, (const struct sockaddr *)&sin, sizeof(sin)) == -1)
{
std::cerr << "Error: connect" << std::endl;
close(s);
return EXIT_FAILURE;
}
std::cout << "client started" << std::endl;
do_server(s);
return EXIT_SUCCESS;
}

最佳答案

这似乎与您选择的netcat风格有关。

使用“传统” netcat(/etc/alternatives/nc链接到/bin/nc.traditional),您必须使用以下语法来指定监听端口:

nc -l -p 4242

“openbsd” netcat也支持此语法(以及您使用的语法),即使它的手册页指出您不能一起使用 -l-p

关于sockets - 在openSUSE中工作的套接字在Debian中不工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15318164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com