如何使我的服务器应用程序使用我的IP地址而不是localhost

时间:2010-11-23 03:48:44

标签: c++ gcc networking client-server winsock

这可能是微不足道的,但我需要帮助让我的服务器监听我的ISP IP地址而不是我的客户端 - 服务器消息程序中的localhost。我的服务器和客户端如下。

服务器

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

const int winsock_version = 2;
#define PORT "3490"
#define MAX_NUM_CONNECTIONS 10

int main(void){

    WSADATA wsadata;

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

        struct addrinfo hints,*res;
        int sock_fd;

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

        if (getaddrinfo(NULL,PORT,&hints,&res) != 0){
            cout<<"-Call to getaddress was unsucceful." << endl;
        }

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

        if ( (bind(sock_fd, res->ai_addr, res->ai_addrlen)) != -1 ){
            cout<<"-binding successful." << endl;
        }

        if ( (listen(sock_fd,MAX_NUM_CONNECTIONS)) != -1){
            cout<<"-Listening for incoming connections." << endl;
        }
        //-------------------------------------------------------------

        struct sockaddr_storage incming_info;
        socklen_t sin_size;
        sin_size = sizeof incming_info;

        int new_fd;

        new_fd = accept(sock_fd, (struct sockaddr*)&incming_info,&sin_size);
        if (new_fd == -1){
            cout<<"-Accepting error." << endl;
        }
        if(new_fd == INVALID_SOCKET){
            cout<<"-INVALID SOCKET ERROR." << endl;
        }
        char buffer[128];
        while(true){
            int ret_val;

            ret_val = recv(new_fd,buffer,sizeof(buffer),0);
            if(ret_val == -1){
                cout<<"Receiving Error." << endl;
                break;
            }else if(ret_val == 0){
                cout<<"Connection has been closed!." << endl;
                break;
            }else{
                cout<<"Server: " << buffer<< endl;
            }
        }
        cout<<"-Closing connection" << endl;
        closesocket(new_fd);

    }else{
        cout<<"-WSAStartup Initialization failed." << endl;
        if(WSACleanup()!=0){
            cout<<"-WSACleanup Successful." << endl;
        }else{
            cout<<"-WSACleanup Failed." << endl;
        }
    }
    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 "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;
}

我知道如何(我认为)指定客户端,以便通过更改client.cpp中的SERVER来连接到我的IP地址XXX.XX.XXX.XX,但我不知道如何获取服务器听这个?。

2 个答案:

答案 0 :(得分:1)

看看这里:

C++ strange socket data

这一行:

addrinfo.sin_addr.s_addr = INADDR_ANY;

指定任何地址。见这里:

表示getaddrinfo,如果您想使用特定的IP或主机名而不是任何。

答案 1 :(得分:0)

getaddrinfo将服务器的标识作为第一选项 - 可以是您可以在dns或IP地址中查找的名称。

当你在'服务器'端指定null时,它暗示localhost,在你专门设置本地主机的客户端上。

The MSDN doc

您使用的功能将报告可能的addrinfo列表,您碰巧绑定了第一个。
目前你暗示你想要一个流。其余的由解析器决定,所以你将在ipv4和ipv6上获得localhost(环回,无线,以太,wan)和任何端口的所有内容。

您需要更具体地了解您的提示,或者可能跳过此功能并直接指定端口和地址。