This doesn't do any port forwarding, it just updates my ip record in the server for resolving the right ip address. It didn't cause my program to receive data sent to my external ip address.
Printable View
This doesn't do any port forwarding, it just updates my ip record in the server for resolving the right ip address. It didn't cause my program to receive data sent to my external ip address.
All DynDNS.com does is constantly monitor your "external" ip address and assign it to the stable URL that you have selected.
Thus, you'll have to use your stable URL in your remote client which in turn will use the gethostbyname function to convert the URL into your "external" ip address to establish a connection.
Also, the "server" code you posted implies that it being run on a non networked local machine by the use of a loopback address of 127.0.0.1
You may want to change that to:
INADDR_ANY gets the ip address of the server computer.Code:service.sin_addr.s_addr = inet_addr = INADDR_ANY;
My program still doesn't receive data sent to external ip address.
And did you mean:
???Code:service.sin_addr.s_addr = INADDR_ANY;
Hopefully, you can humor me a little bit and compile/run the following sample apps. Please note that the command line input for the client is the static URL that you have created in your DynDNS account. Also, the port number must be the same on both client and server. BTW, I have tested this minimal client/server app with DynDNS and it works without any problems.
Code:// Usage Server.exe 27015
#include <stdio.h>
#include <winsock.h>
void Server(int iPort);
int main(int argc, char **argv)
{
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int iPort;
if (argc != 2)
{
printf("Usage: %s PortNumber\n", argv[0]);
return -1;
}
iPort = atoi(argv[1]);
WSAStartup(wVersionRequested, &wsaData);
Server(iPort);
WSACleanup();
return 0;
}
void Server(int iPort)
{
int iRet;
int iLen;
char szBuf[256] = {0};
SOCKET listenSocket;
SOCKET remoteSocket;
SOCKADDR_IN ServerSa;
listenSocket = socket(AF_INET,
SOCK_STREAM,
IPPROTO_TCP);
if (listenSocket == INVALID_SOCKET)
{
printf("socket()error \n");
return;
}
ServerSa.sin_family = AF_INET;
ServerSa.sin_addr.s_addr = INADDR_ANY;
ServerSa.sin_port = htons(iPort);
iRet = bind(listenSocket,
(LPSOCKADDR)&ServerSa,
sizeof(struct sockaddr));
if (iRet == SOCKET_ERROR)
{
printf("bind()\n");
closesocket(listenSocket);
return;
}
iLen = sizeof(SOCKADDR);
iRet = gethostname(szBuf, sizeof(szBuf));
if (iRet == SOCKET_ERROR)
{
printf("gethostname()\n");
closesocket(listenSocket);
return;
}
printf("\nServer named %s waiting on port %d\n",
szBuf, iPort);
printf("\nlisten()");
iRet = listen(listenSocket,
SOMAXCONN);
if (iRet == SOCKET_ERROR)
{
printf("listen() error\n");
closesocket(listenSocket);
return;
}
printf("\nBlocking at accept()");
remoteSocket = accept(listenSocket,
NULL,
NULL);
if (remoteSocket == INVALID_SOCKET)
{
printf("accept() error\n");
closesocket(listenSocket);
return;
}
memset(szBuf, 0, sizeof(szBuf));
iRet = recv(remoteSocket,
szBuf,
sizeof(szBuf),
0);
if (iRet == INVALID_SOCKET)
{
printf("recv()\n");
closesocket(listenSocket);
closesocket(remoteSocket);
return;
}
printf("\nData received from client: %s", szBuf);
strcpy(szBuf, "This came from the Server");
iRet = send(remoteSocket,
szBuf,
strlen(szBuf),
0);
closesocket(remoteSocket);
closesocket(listenSocket);
return;
}
Code:// Usage: Client.exe www.maxurl.com 27015
//
#include <stdio.h>
#include <winsock.h>
void Client(char *szServer, int iPort);
int main(int argc, char **argv)
{
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int iPort;
if (argc != 3)
{
printf("Usage: %s UrlName PortNumber\n", argv[0]);
return - 1;
}
iPort = atoi(argv[2]);
WSAStartup(wVersionRequested, &wsaData);
Client(argv[1], iPort);
WSACleanup();
return 0;
}
void Client(char *szServer, int iPort)
{
LPHOSTENT lpHostEntry;
int iRet;
char szBuf[256] = {0};
printf("\nStream Client connecting to server: %s on port: %d",
szServer, iPort);
lpHostEntry = gethostbyname(szServer);
if (lpHostEntry == NULL)
{
printf("gethostbyname()error\n");
return;
}
SOCKET ClientSocket;
ClientSocket = socket(AF_INET,
SOCK_STREAM,
IPPROTO_TCP);
if (ClientSocket == INVALID_SOCKET)
{
printf("socket() error\n");
return;
}
SOCKADDR_IN saServer;
saServer.sin_family = AF_INET;
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
saServer.sin_port = htons(iPort);
iRet = connect(ClientSocket,
(LPSOCKADDR)&saServer,
sizeof(struct sockaddr));
if (iRet == SOCKET_ERROR)
{
printf("socket()\n");
closesocket(ClientSocket);
return;
}
strcpy(szBuf, "This came from the Client");
iRet = send(ClientSocket,
szBuf,
strlen(szBuf),
0);
if (iRet == SOCKET_ERROR)
{
printf("send()error\n");
closesocket(ClientSocket);
return;
}
iRet = recv(ClientSocket,
szBuf,
sizeof(szBuf),
0);
if (iRet == SOCKET_ERROR)
{
printf("recv() error\n");
closesocket(ClientSocket);
return;
}
printf("\nData received from Server: %s", szBuf);
closesocket(ClientSocket);
return;
}
DynDNS provides a means of attaching a domain name to your IP, even if your IP changes. It does not equal port forwarding.
Say he has a network, with two machines behind a router. Both are running servers, on the same port. If data from the Internet arrives at the router, bound for a server on that port, how is the router supposed to know who to send it to? That's what port forwarding does, and is configured on the router.
For those two computers behind the router, to the outside world, they have the same IP address - that of the router.
Changing your code to INADDR_ANY should help. All the examples I seen / used have run the port through htons(), and some run INADDR_ANY through htonl(). (Not sure if it's recommended to htonl(INADDR_ANY), but since it's defined as 0 on my system, it won't make a difference. This gives an example that does.)
Finally, if your code still doesn't work, figure out if it's port forwarding or your code. Try forwarding some other server and connecting to it using your external IP briefly to see if you're setting up port forwarding correctly.