Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C/C++;Posix tcp/ip网络客户端,仅连接到本地主机_C++_Sockets_Posix - Fatal编程技术网

C++ C/C++;Posix tcp/ip网络客户端,仅连接到本地主机

C++ C/C++;Posix tcp/ip网络客户端,仅连接到本地主机,c++,sockets,posix,C++,Sockets,Posix,我在Ubuntu 12上编写了这个简单的客户端。它是简单的C/C++连接到服务器的代码。它可以连接到本地主机。但我无法让它连接到外部服务器。每次我连接到不同的主机时,它实际上都会连接到“localhost”。我想这可能是我的/etc/hosts文件的配置方式,但我不知道 #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h&

我在Ubuntu 12上编写了这个简单的客户端。它是简单的C/C++连接到服务器的代码。它可以连接到本地主机。但我无法让它连接到外部服务器。每次我连接到不同的主机时,它实际上都会连接到“localhost”。我想这可能是我的/etc/hosts文件的配置方式,但我不知道

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <errno.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>

int connect(const std::string host, const std::string path) {
    const int port = 80;
    // Setup the msock
    int m_sock;
    sockaddr_in m_addr;
    memset(&m_addr, 0, sizeof(m_addr));
    m_sock = socket(AF_INET, SOCK_STREAM, 0);

    int on = 1;
    if (setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(on)) == -1) {
        return false;
    }

    // Connect //
    m_addr.sin_family = AF_INET;
    m_addr.sin_port = htons(port);
    int status = inet_pton(AF_INET, host.c_str(), &m_addr.sin_addr);

    cout << "Status After inet_pton: " << status << " # " << m_addr.sin_addr.s_addr << endl;
    if (errno == EAFNOSUPPORT) {
        return false;
    }
    status = ::connect(m_sock, (sockaddr *) &m_addr, sizeof(m_addr));
    if (status == -1) {
        return false;
    }
} // End of the function //
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int connect(const std::string主机,const std::string路径){
常数int端口=80;
//设置msock
int m_袜子;
m_addr中的sockaddr_;
memset(&m_addr,0,sizeof(m_addr));
m_sock=socket(AF_INET,sock流,0);
int on=1;
if(setsockopt(m_sock,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on))=-1){
返回false;
}
//连接//
m_addr.sinu family=AF_INET;
m_addr.sin_port=htons(port);
int status=inet\u pton(AF\u inet,host.c\u str(),&m\u addr.sin\u addr);

cout您应该首先从DNS服务器解析域名的IP地址,然后尝试建立连接。
gethostbyname
将给出IP地址列表,该地址通过
hostent
结构从域名解析而来。您可以按如下方式使用它:

struct hostent *hent;
hent = gethostbyname(host.c_str());
然后移动hostent给您的地址列表并测试连接,下面是
hostent
结构和
gethostbyname
定义:

#include <netdb.h>
struct hostent *gethostbyname(const char *name);

struct hostent {
  char  *h_name;            /* official name of host */
  char **h_aliases;         /* alias list */
  int    h_addrtype;        /* host address type */
  int    h_length;          /* length of address */
  char **h_addr_list;       /* list of addresses */
}
#包括
结构hostent*gethostbyname(常量字符*name);
结构宿主{
char*h_name;/*主机的正式名称*/
字符**h_别名;/*别名列表*/
int h_addrtype;/*主机地址类型*/
int h_length;/*地址长度*/
字符**地址列表;/*地址列表*/
}

作为主机发送什么?域还是Ip?inet\u pton仅用于将Ip地址从字符串转换为数字。connect(reddit.com,80);可能这就是问题所在。好的,这很有帮助。您认为是关闭问题还是回答问题?使用:
gethostbyname
首先解析ip地址。如果您能创建有效的答案,我将选择它。