![]() |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 73
| This works fine with IP 127.0.0.1 but fails with my REAL IP. I don't think I have bad firewalls and I have windows Vista OR XP depending on witch computer(s) I use. I belive this is the Reciving program's code: Code: #include <iostream>
#include <winsock2.h>
#pragma comment(lib,"wsock32.lib")
int main(void)
{
WSADATA WsaDat;
if(WSAStartup(MAKEWORD(2,0),&WsaDat)!=0)
{
std::cout<<"Winsock error - Winsock initialization failed\r\n";
WSACleanup();
system("PAUSE");
return 0;
}
// Create our socket
SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(Socket==INVALID_SOCKET)
{
std::cout<<"Winsock error - Socket creation Failed!\r\n";
WSACleanup();
system("PAUSE");
return 0;
}
/*
// Resolve IP address for hostname
struct hostent *host;
if((host=gethostbyname("localhost"))==NULL)
{
std::cout<<"Failed to resolve hostname.\r\n";
WSACleanup();
system("PAUSE");
return 0;
}
*/
// Setup our socket address structure
SOCKADDR_IN SockAddr;
SockAddr.sin_port=htons(8888);
SockAddr.sin_family=AF_INET;
//SockAddr.sin_addr.s_addr=*((unsigned long*)host->h_addr);
SockAddr.sin_addr.s_addr=inet_addr("74.76.143.181");
// Attempt to connect to server
if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr))!=0)
{
std::cout<<"Failed to establish connection with server\r\n";
WSACleanup();
system("PAUSE");
return 0;
}
// If iMode!=0, non-blocking mode is enabled.
u_long iMode=1;
ioctlsocket(Socket,FIONBIO,&iMode);
// Main loop
for(;;)
{
// Display message from server
char buffer[1000];
memset(buffer,0,999);
int inDataLength=recv(Socket,buffer,1000,0);
std::cout<<buffer;
int nError=WSAGetLastError();
if(nError!=WSAEWOULDBLOCK&&nError!=0)
{
std::cout<<"Winsock error code: "<<nError<<"\r\n";
std::cout<<"Server disconnected!\r\n";
// Shutdown our socket
shutdown(Socket,SD_SEND);
// Close our socket entirely
closesocket(Socket);
break;
}
Sleep(1000);
}
WSACleanup();
system("PAUSE");
return 0;
}
|
| azjherben is offline | |
| | #2 |
| Registered User Join Date: Mar 2009
Posts: 73
| Sending program's code: Code: #include <iostream>
#include <winsock2.h>
#pragma comment(lib,"wsock32.lib")
int main()
{
WSADATA WsaDat;
if(WSAStartup(MAKEWORD(2,0),&WsaDat)!=0)
{
std::cout<<"WSA Initialization failed!\r\n";
WSACleanup();
system("PAUSE");
return 0;
}
SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(Socket==INVALID_SOCKET)
{
std::cout<<"Socket creation failed.\r\n";
WSACleanup();
system("PAUSE");
return 0;
}
SOCKADDR_IN serverInf;
serverInf.sin_family=AF_INET;
serverInf.sin_addr.s_addr=INADDR_ANY;
serverInf.sin_port=htons(8888);
if(bind(Socket,(SOCKADDR *)(&serverInf),sizeof(serverInf))==SOCKET_ERROR)
{
std::cout<<"Unable to bind socket!\r\n";
WSACleanup();
system("PAUSE");
return 0;
}
listen(Socket,1);
SOCKET TempSock=SOCKET_ERROR;
while(TempSock==SOCKET_ERROR)
{
std::cout<<"Waiting for incoming connections...\r\n";
TempSock=accept(Socket,NULL,NULL);
}
// If iMode!=0, non-blocking mode is enabled.
u_long iMode=1;
ioctlsocket(Socket,FIONBIO,&iMode);
Socket=TempSock;
std::cout<<"Client connected!\r\n\r\n";
// Main loop
for(;;)
{
char *szMessage="Welcome to the server!\r\n";
send(Socket,szMessage,strlen(szMessage),0);
int nError=WSAGetLastError();
if(nError!=WSAEWOULDBLOCK&&nError!=0)
{
std::cout<<"Winsock error code: "<<nError<<"\r\n";
std::cout<<"Client disconnected!\r\n";
// Shutdown our socket
shutdown(Socket,SD_SEND);
// Close our socket entirely
closesocket(Socket);
break;
}
Sleep(1000);
}
WSACleanup();
system("PAUSE");
return 0;
}
|
| azjherben is offline | |
| | #3 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,657
| Well you could be more specific than "it doesn't work". Also, you're assuming a hell of a lot about how information is transmitted from one node to another. The Eight Fallacies of Distributed Computing 127.0.0.1 just covers up the problems in your code. |
| Salem is offline | |
| | #4 |
| Registered User Join Date: Mar 2009
Posts: 73
| Oh? Well it says: "Failed to establish connection with server." |
| azjherben is offline | |
| | #5 |
| Registered User Join Date: Mar 2009
Posts: 73
| What "Fallacy" is me? |
| azjherben is offline | |
| | #6 |
| mastering the obvious Join Date: Jul 2008 Location: SE Queens
Posts: 5,131
| If you mean the IP assigned to you by your ISP you are out of luck. There is no way they will allow you to operate any kind of server. This is for "outgoing calls only", if you get the point. You can only do client/server stuff at home on a LAN (that is, the circle of computers connected to your router). You cannot do it over the internet. |
| MK27 is offline | |
| | #7 | |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 902
| Quote:
__________________ long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) | |
| Cactus_Hugger is offline | |
| | #8 |
| Registered User Join Date: Apr 2009
Posts: 10
| I've got a question about the same thing, so I won't make a new topic. I use my server on a local IP and port forward to it. When I give my client application the real IP of mine, should it work from outside my home network? In my home network it worked, although I didn't use local IP for my client application. |
| moment is offline | |
| | #9 |
| mastering the obvious Join Date: Jul 2008 Location: SE Queens
Posts: 5,131
| Thanks for that. I was not aware of this, so probably you are right and I am wrong. Very likely, in fact, since in reflection, I doubt the ISP is going to bothered filtering incoming packets. Last edited by MK27; 05-17-2009 at 10:29 AM. |
| MK27 is offline | |
| | #10 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,758
| If you're serious about running a server, I'd stop fretting about ISP rules and just buy a virtual host on any one of the reputable providers. I pay around $30 a month (I won't name the service to avoid spamming) and get full root access to a Debian machine which runs three domains, one of which is my wife's online business. On top of that, automated backups and awesome support (for a reasonable fee, they'll take care of any server admin tasks that you need done).
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is offline | |
| | #11 |
| Registered User Join Date: Mar 2009
Posts: 73
| I sorta solved it... If I take the wire the goes to my wireless router (uplink) and plug it right into my laptop, then it works fine, whatever ip/port I want. I have an idea for a program btw. So I will be going somewhere with this. |
| azjherben is offline | |
| | #12 | |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 902
| Quote:
Are you sure you had port forwarding setup correctly earlier? The idea is essentially this: You computer is on a private network, and has an IP that probably begins with 192.168.1.? (there are other IPs). Your router, on the other hand, appears to you as something like 192.168.1.1, but to the Internet as another IP entirely (I'll call this your WAN IP) If someone tries to connect to your server with the WAN IP, what they get is the router. The router doesn't have your server running on it, so it can't use the connection, and there could be multiple computers connected to it, so it doesn't know to which computer it should send the connection. This is where port forwarding comes in - you manually tell the router "If you get a connection at this port, send it to this IP." How you do this is router-specific... so RTFM. & Google it. Also, having your computer connected directly to the Internet, with Windows... probably not a great idea. I'd imagine that a router would filter a fair bit of crap...
__________________ long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) | |
| Cactus_Hugger is offline | |
| | #13 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,622
| Raise your hand if you knew it was a router issue as soon as you saw the phrase: "it works fine with ... but..." Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #14 | |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 902
| Quote:
1) The leaked socket 2) The random \r's in the output? 3) Are we using \r\n to denote the end of a message? 4) The assumption that send()... sends. 5) The odd call to memset() with 999? 6) The buffer overflow possibility involving the recv().
__________________ long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) | |
| Cactus_Hugger is offline | |
| | #15 |
| Registered User Join Date: Mar 2009
Posts: 73
| I now have a good (first version but, a concept program) here: azjherben.org/usr.exe But it has mysterious crashes (on MY pc, not yours) after viewing a few pages. It also olny goes one connect at a time. (Olny one person can use at a time, in the world????) |
| azjherben is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| CreateFile returns err 5 on 2000, but works fine of XP | Yarin | Windows Programming | 26 | 08-10-2008 09:34 AM |
| Prints IP string out to console ok, But wont fwrite() them | cruxis | C Programming | 4 | 03-29-2006 04:36 PM |
| It works just fine but | jcmhex | C++ Programming | 3 | 04-24-2005 06:53 PM |
| Wont run in X, works fine in alt+F1 terminal | zmerlinz | Linux Programming | 5 | 04-28-2004 11:58 AM |
| My computer works just fine | Betazep | A Brief History of Cprogramming.com | 10 | 07-02-2002 08:51 AM |