连接到 IP 地址而不是本地主机?

发布于 2024-10-03 18:17:40 字数 5345 浏览 4 评论 0原文

这是我的第二次尝试,我编写了一个服务器应用程序,如下所示,它输出客户端发送的任何内容。我有一个 server.exe 版本,它使用 INADDR_ANY 监听任何内容,完成此操作后,如果我指定客户端连接到本地主机,我可以使用我的客户端并连接到服务器(一切正常)。

但是如果我使用自己的IP地址而不是本地主机作为客户端,我无法连接到服务器?我不应该能够以这种方式连接到服务器吗?

我使用了另一个 server.exe,它被硬编码为使用我的 IP 地址,但没有 INADDR_ANY,但它无法 bind()listen() 传入连接。我对如何继续感到困惑。

上运行服务器和客户端,这是一个问题吗?

(我在同一台机器

#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
using namespace std;

#define PORT 3490
#define IP "118.93.0.164"
#define BACKLOG 10

const int winsockversion = 2;

int main(void){
    WSADATA wsadata;
    if ( (WSAStartup(MAKEWORD(winsockversion,0),&wsadata)) == 0){
        cout<<"-WSAStartup Initialized." << endl;

        struct sockaddr_in serv;
        memset(&serv,0,sizeof serv);
        serv.sin_family = AF_INET;
        serv.sin_port = htons(PORT);
        serv.sin_addr.s_addr = inet_addr(IP);//INADDR_ANY;//inet_addr(IP);
        //---------------------------------------
        struct addrinfo serv_addrinfo;
        serv_addrinfo.ai_family = AF_INET;
        serv_addrinfo.ai_socktype = SOCK_STREAM;
        serv_addrinfo.ai_protocol = IPPROTO_TCP;
        serv_addrinfo.ai_addrlen = sizeof(serv);
        serv_addrinfo.ai_addr = (sockaddr*)&serv;


        //---------------------------------------
        SOCKET serv_con;
        serv_con = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
        if (serv_con != INVALID_SOCKET){
            cout<<"-Server Socket created." << endl;
        }

        if (bind(serv_con,serv_addrinfo.ai_addr,serv_addrinfo.ai_addrlen) != -1){
            cout<<"-Binding Successful." << endl;
        }

        if( listen(serv_con,BACKLOG) != -1){
            cout<<"-Listening for incoming connections." << endl;
        }
        //--------------------------------------------------------------------
        SOCKET recv_socket;
        struct sockaddr_in client_info;
        int client_info_size = sizeof(client_info);

        char *con_addr = inet_ntoa(client_info.sin_addr);




        recv_socket = accept(serv_con,(sockaddr*)&client_info,&client_info_size);

        if( recv_socket != INVALID_SOCKET ){
            cout<<"-Connection Established!. " << endl;
            cout<<"-Connected to: [" << con_addr << "] " << endl;
        //----------------------------------------------------------------------
            char buffer[80];
            int bytes_in;
            while(true){

                bytes_in = recv(recv_socket,buffer,sizeof(buffer),0);
                if ( bytes_in > 0 ){
                    cout<<"[" << con_addr << "]" << buffer << endl;
                }

                if (bytes_in == 0 ){
                    cout<<"[" << con_addr << "] has disconnected." << endl;
                    break;
                }
                if (bytes_in == -1 ){
                    cout<<"-Possible Abrupt disconnecton from [" << con_addr << "]" << endl;
                    break;
                }
            }

            closesocket(recv_socket);
            closesocket(serv_con);

        }

    }else{
        cout<<"-WSAStartup Initialization failed." << endl;
    }

    if( WSACleanup()!= -1){
        cout<<"-WSACleanp Successful." << endl;
    }

    WSAGetLastError();


    return 0;
}

/*client*/
#define _WIN32_WINNT 0x501
#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
using namespace std;

#define PORT "3490"
#define SERVER "118.93.0.164" // and localhost 
const int winsockVersion = 2;


int main(void){

    WSADATA wsadata;
    if ( (WSAStartup(MAKEWORD(2,0),&wsadata)) == 0){
        cout<<"-WSAStartup Initialized." << endl;

        struct addrinfo hints, *res;
        int sockfd;

        memset(&hints,0,sizeof hints);
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;

        if (getaddrinfo(SERVER,PORT,&hints,&res) != 0){
            cout<<"-getaddrinfo unsuccessful." << endl;
        }

        if ( (sockfd = socket(res->ai_family,res->ai_socktype,res->ai_protocol)) == -1 ){
            cout<<"-Unable to create socket." << endl;
        }

        if ( (connect(sockfd,res->ai_addr,res->ai_addrlen)) != -1 ){
            cout<<"-Connection Established." << endl;
        }

        cout<<"-Client connecting to: " << res->ai_addr << endl;

        while(true){
            string text_buff;
            cout<<"Enter text: ";
            getline(cin,text_buff);
            if( (send(sockfd,text_buff.c_str(),text_buff.length()+1,0)) != -1 ){
                cout<<"-text_buff sent!." << endl;
            }

        }

    }else{
        cout<<"-WSAStartup Initialization failed." << endl;
        if(WSACleanup()!=0){
            cout<<"-WSACleanup Successful." << endl;
        }else{
            cout<<"-WSACleanup Failed." << endl;
        }
    }

    return 0;
}

this is my second go at this, I've written a server application as below which outputs whatever the client sends. I have one server.exe version where it listens to anything using INADDR_ANY, Having done that I can use my client and connect to the server if I specify the client connect to localhost (Which all works fine).

but if I use my own IP address instead of localhost for the client, I cannot connect to the server?. Shouldn't I be able to connect to the server this way?.

I used another server.exe which was hard-coded to use my IP address without having INADDR_ANY but it wasn't able to bind() or listen() to incoming connections. I'm confused as to how to proceed.

(I'm running the server and client on the same machine, is that an issue?)

server

#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
using namespace std;

#define PORT 3490
#define IP "118.93.0.164"
#define BACKLOG 10

const int winsockversion = 2;

int main(void){
    WSADATA wsadata;
    if ( (WSAStartup(MAKEWORD(winsockversion,0),&wsadata)) == 0){
        cout<<"-WSAStartup Initialized." << endl;

        struct sockaddr_in serv;
        memset(&serv,0,sizeof serv);
        serv.sin_family = AF_INET;
        serv.sin_port = htons(PORT);
        serv.sin_addr.s_addr = inet_addr(IP);//INADDR_ANY;//inet_addr(IP);
        //---------------------------------------
        struct addrinfo serv_addrinfo;
        serv_addrinfo.ai_family = AF_INET;
        serv_addrinfo.ai_socktype = SOCK_STREAM;
        serv_addrinfo.ai_protocol = IPPROTO_TCP;
        serv_addrinfo.ai_addrlen = sizeof(serv);
        serv_addrinfo.ai_addr = (sockaddr*)&serv;


        //---------------------------------------
        SOCKET serv_con;
        serv_con = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
        if (serv_con != INVALID_SOCKET){
            cout<<"-Server Socket created." << endl;
        }

        if (bind(serv_con,serv_addrinfo.ai_addr,serv_addrinfo.ai_addrlen) != -1){
            cout<<"-Binding Successful." << endl;
        }

        if( listen(serv_con,BACKLOG) != -1){
            cout<<"-Listening for incoming connections." << endl;
        }
        //--------------------------------------------------------------------
        SOCKET recv_socket;
        struct sockaddr_in client_info;
        int client_info_size = sizeof(client_info);

        char *con_addr = inet_ntoa(client_info.sin_addr);




        recv_socket = accept(serv_con,(sockaddr*)&client_info,&client_info_size);

        if( recv_socket != INVALID_SOCKET ){
            cout<<"-Connection Established!. " << endl;
            cout<<"-Connected to: [" << con_addr << "] " << endl;
        //----------------------------------------------------------------------
            char buffer[80];
            int bytes_in;
            while(true){

                bytes_in = recv(recv_socket,buffer,sizeof(buffer),0);
                if ( bytes_in > 0 ){
                    cout<<"[" << con_addr << "]" << buffer << endl;
                }

                if (bytes_in == 0 ){
                    cout<<"[" << con_addr << "] has disconnected." << endl;
                    break;
                }
                if (bytes_in == -1 ){
                    cout<<"-Possible Abrupt disconnecton from [" << con_addr << "]" << endl;
                    break;
                }
            }

            closesocket(recv_socket);
            closesocket(serv_con);

        }

    }else{
        cout<<"-WSAStartup Initialization failed." << endl;
    }

    if( WSACleanup()!= -1){
        cout<<"-WSACleanp Successful." << endl;
    }

    WSAGetLastError();


    return 0;
}

client

/*client*/
#define _WIN32_WINNT 0x501
#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
using namespace std;

#define PORT "3490"
#define SERVER "118.93.0.164" // and localhost 
const int winsockVersion = 2;


int main(void){

    WSADATA wsadata;
    if ( (WSAStartup(MAKEWORD(2,0),&wsadata)) == 0){
        cout<<"-WSAStartup Initialized." << endl;

        struct addrinfo hints, *res;
        int sockfd;

        memset(&hints,0,sizeof hints);
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;

        if (getaddrinfo(SERVER,PORT,&hints,&res) != 0){
            cout<<"-getaddrinfo unsuccessful." << endl;
        }

        if ( (sockfd = socket(res->ai_family,res->ai_socktype,res->ai_protocol)) == -1 ){
            cout<<"-Unable to create socket." << endl;
        }

        if ( (connect(sockfd,res->ai_addr,res->ai_addrlen)) != -1 ){
            cout<<"-Connection Established." << endl;
        }

        cout<<"-Client connecting to: " << res->ai_addr << endl;

        while(true){
            string text_buff;
            cout<<"Enter text: ";
            getline(cin,text_buff);
            if( (send(sockfd,text_buff.c_str(),text_buff.length()+1,0)) != -1 ){
                cout<<"-text_buff sent!." << endl;
            }

        }

    }else{
        cout<<"-WSAStartup Initialization failed." << endl;
        if(WSACleanup()!=0){
            cout<<"-WSACleanup Successful." << endl;
        }else{
            cout<<"-WSACleanup Failed." << endl;
        }
    }

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-10-10 18:17:40

如果您有路由器,则必须设置端口转发。这实际上取决于您拥有哪种路由器,因此您必须查找适合您的特定路由器。

您无法绑定到 ISP 的 IP 地址,因为您不拥有该地址(路由器拥有)。您必须绑定到您的计算机实际拥有的IP地址,该地址可以通过Windows中的ipconfig /all找到。然后确保将路由器中服务器端口号的流量转发到计算机拥有的内部 IP 地址。

If you have a router you will have to set up Port forwarding. It really depends on what kind of router you have, so you will have to look it up for your specific one.

You cannot bind to your ISP's IP address, since you do not own it (the router does). You have to bind to an IP address that your computer actually owns, which is found via ipconfig /all in windows. Then make sure to forward traffic in your router for your servers portnumber, to the internal ipaddress that your computer has.

七度光 2024-10-10 18:17:40

当您尝试连接回服务器时,您使用的是 localhost/127.0.0.1 还是您正在侦听的 IP 地址?

尝试连接到“localhost”可能会尝试使用环回接口,而您的程序没有侦听该接口。我不是 100% 确定它在 Windows 中是如何工作的。

我可以问一个更重要的问题,可能会促使你改变你的设计:为什么你想绑定到特定的IP(和本地主机)?

When you're trying to connect back to the server, are you using localhost/127.0.0.1, or the IP address you're listening on?

It may be that trying to connect to 'localhost' attempts to use the loopback interface, which your program isn't listening on. I'm not 100% sure how it works in Windows.

Can I ask a more important question that may prompt you to change your design: why do you want to bind to a particular IP (and localhost)?

淡莣 2024-10-10 18:17:40

您可以尝试在客户端使用cmd ping 服务器IP,看看是否是连接问题。

此外,我认为这

#define PORT "3490"

在客户端是错误的,因为端口应该是整数。

you may try using cmd to ping to server IP from your client side to see whether it is a connection problem.

Furthermore, i think this

#define PORT "3490"

in client side is wrong since port should be an integer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文