C++ simple chat not working with my IP address
I have written a little simple chat program with client and a Server. When I use a local IP like 192.168.1.1 or 127.0.0.1, the program works, but if I try to use my internet IP, it won't work. I have turned the firewall on my computer off, and I have port forwarded the port 17000 (tried other ports too), but still it didn't work. What could be the problem?
Here is the code for the client:
#define _WINSOCK_
#include <WinSock2.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
#define SERVER_ADDRESS "127.0.0.1"
#define SERVER_PORT 17000
#define MESSAGE_SIZE 256
WSADATA Winsock;
SOCKET Socket;
sockaddr_in ServerAddress;
char Buffer[MESSAGE_SIZE];
int main()
{
WSAStartup(MAKEWORD(2,2),&Winsock);
if(LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2)
{
WSACleanup();
return 1;
}
Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
ZeroMemory(&ServerAddress, sizeof(ServerAddress));
ServerAddress.sin_family = AF_INET;
ServerAddress.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);
ServerAddress.sin_port = SERVER_PORT;
while(true)
{
gets(Buffer);
sendto(Socket, Buffer, MESSAGE_SIZE, 0, (sockaddr*)&ServerAddress, sizeof(ServerAddress));
if(Buffer[0]== ' ')
{
break;
}
}
WSACleanup();
return 0;
}
And the code for the Server:
#define _WINSOCK_
#include <WinSock2.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
#define SERVER_ADDRESS "127.0.0.1"
#define SERVER_PORT 17000
#define MESSAGE_SIZE 256
WSADATA Winsock;
SOCKET Socket;
char Buffer[MESSAGE_SIZE];
sockaddr_in ServerAddress;
int SizeInt = sizeof(ServerAddress);
int main()
{
WSAStartup(MAKEWORD(2,2), &Winsock);
if(LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2)
{
WSACleanu开发者_如何学编程p();
return 1;
}
Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
ZeroMemory(&ServerAddress, sizeof(ServerAddress));
//ServerAddress.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);
ServerAddress.sin_family = AF_INET;
ServerAddress.sin_port = SERVER_PORT;
bind(Socket, (sockaddr*)&ServerAddress, sizeof(ServerAddress));
while(true)
{
if(recvfrom(Socket, Buffer, MESSAGE_SIZE, 0, (sockaddr*)&ServerAddress, &SizeInt))
{
Buffer[MESSAGE_SIZE - 1] = '\0';
printf("RECEIVED: ");
printf(Buffer);
printf("\n");
if(Buffer[0] == ' ')
{
break;
}
}
}
WSACleanup();
return 0;
}
Unless your computer's network interface directly assigned your Internet-facing IP, you should always bind to the LAN IP. The bound IP address just defines which network interfaces the program will use to communicate.
From the WinSock bind()
documentation, it seems you can bind to INADDR_ANY
to listen on all interfaces.
Possible configurations:
Client on the same computer. Bind the server to localhost (127.0.0.1), and connect to localhost with the client.
Client on the same network. Bind the server to your LAN IP (something like 192.168.1.10), and connect to the LAN IP with the client.
Client over the Internet. Bind the server to your LAN IP, forward the port from your router, and connect to the WAN/Internet IP with the client.
Clients both over the Internet and LAN. Bind the server to your LAN IP and forward the port from your router. You can connect to the LAN IP from LAN, and the WAN/Internet IP from Internet clients.
For option 4, some networks will allow you to connect to the WAN IP from inside the LAN, simplifying configuration. On a simple home router, this can potentially be enabled via an option like "NAT Loopback" in your router settings. It's often near settings like "allow ping/ICMP on internet", "SPI firewall", and "block ident port".
精彩评论