如何让我的服务器应用程序使用我的 IP 地址而不是 localhost
这可能是微不足道的,但我需要帮助让我的服务器侦听我的 ISP IP 地址,而不是我的客户端服务器消息程序中的本地主机。我的服务器和客户端如下。
服务器客户
/*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中的服务器来指定客户端,以便使其连接到我的IP地址XXX.XX.XXX.XX,但我不确定如何获取服务器听听这个?
this is probably trivial but I need help in making my server listen on my ISP IP address rather than the localhost in my client-server messager program. My server and client is as follows.
server
/*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
/*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;
}
I know how (i think) to specify the client in order to make it connect to my IP address XXX.XX.XXX.XX by changing the SERVER in client.cpp, But I'm not sure how to get the server to listen on this?.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这里:
C++奇怪的套接字数据
这一行:
指定任何地址。请参阅此处:
的
getaddrinfo 如果您想使用特定的 IP 或主机名而不是任何主机名。
Take a look here:
C++ strange socket data
This line:
specifies any address. See here:
for
getaddrinfo
if you want to use a specific IP or a hostname instead of any.getaddrinfo
将服务器的身份作为第一个选项 - 这可以是您可以在 dns 中查找的名称或 IP 地址。当您在“服务器”端指定 null 时,它意味着 localhost,在客户端上您专门设置本地主机。
MSDN 文档
您使用的功能是要报告可能的地址信息列表,您恰好绑定了第一个。
现在你正在暗示你想要一个流。其余的取决于解析器,因此您将获得本地主机的所有 ip(环回、无线、以太网、WAN)以及 ipv4 和 ipv6 上的任何端口上的内容。
您需要更具体地说明您的提示,或者可能跳过此功能并直接指定端口和地址。
getaddrinfo
takes as first option the identity of the server - that could be a name you can lookup in the dns or an ip address.when you specify null on your 'server' side it implies localhost, on the client you specifically set local host.
The MSDN doc
The function your using is going to report back a list of possible addrinfo, you happen to be binding the first one.
At the moment you are hinting you want a stream. the rest is up to the resolver, so you will get stuff on all ips for localhost (loopback, wireless, ether, wan), and any port, on ipv4 and ipv6.
You need to be more specific about your hints, or perhaps skip this function and specify the port and address directly.