在openSUSE中工作的套接字在Debian中不起作用? [英] Sockets working in openSUSE do not work in Debian?

查看:128
本文介绍了在openSUSE中工作的套接字在Debian中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C / C ++ TCP客户端在OpenSUSE中工作,但不在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\\\
,strlen(client connected\\\
));
关闭;
}

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

if(ac!= 3)
{
std :: cerr<< 用法:./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;
关闭;
return EXIT_FAILURE;
}
std :: cout<<< 客户端开始<< std :: endl;
do_server(s);
return EXIT_SUCCESS;
}


解决方案

这似乎要做与您选择的netcat风味。



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

  nc -l -p 4242 

openbsdnetcat还支持语法(以及您使用的语法),即使它的手册页表示您不能使用 -l <​​/ code>和 -p 在一起。


I have a C/C++ TCP client working in OpenSUSE but not in Debian. I'm using nc -l 4242 for the server. Then I connect with ./my_client 127.0.0.1 4242 on my Debian system (Sid) and it will fail when using the connect function.

Can you confirm if you have the same error too, using Debian or maybe another OS? Where does the problem come from?

Here's the code:

#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;
}

解决方案

This seems to have to do with the netcat flavor you have selected.

With the 'traditional' netcat (/etc/alternatives/nc links to /bin/nc.traditional) you have to use this syntax to specify the listening port:

nc -l -p 4242

The 'openbsd' netcat also supports this syntax (as well as the one you used) even though it's man page says you can't use -l and -p together.

这篇关于在openSUSE中工作的套接字在Debian中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆