This works fine with IP 127.0.0.1 but fails with my REAL IP.

This is a discussion on This works fine with IP 127.0.0.1 but fails with my REAL IP. within the Networking/Device Communication forums, part of the General Programming Boards category; Works fine with IP 127.0.0.1 but fails to connect with my real IP (that's what I have as an example) ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    76

    This works fine with IP 127.0.0.1 but fails with my REAL IP.

    Works fine with IP 127.0.0.1 but fails to connect with my real IP (that's what I have as an example)
    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;
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    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;
    }

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    Oh?


    Well it says:

    "Failed to establish connection with server."

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    What "Fallacy" is me?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by MK27 View Post
    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.
    Don't knock it till you've tried it - while quite a few ISPs in their ToS disallow "public servers", that doesn't mean one doesn't work. I had private FTP, SSH, and VNC servers that worked fine with my home ISP. Most of the problems people experience are NAT from routers, and inexperience with setting up port forwarding.
    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)

  8. #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.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Cactus_Hugger View Post
    Don't knock it till you've tried it -
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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).
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    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.

  12. #12
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by azjherben View Post
    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.
    So, you remove the router, and hence NAT, from the equation.
    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)

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Raise your hand if you knew it was a router issue as soon as you saw the phrase: "it works fine with ... but..."


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by quzah View Post
    Raise your hand if you knew it was a router issue as soon as you saw the phrase: "it works fine with ... but..."
    As I'm fairly certain you realize, that's not his only problem...
    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)

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    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????)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateFile returns err 5 on 2000, but works fine of XP
    By Yarin in forum Windows Programming
    Replies: 26
    Last Post: 08-10-2008, 09:34 AM
  2. Replies: 4
    Last Post: 03-29-2006, 03:36 PM
  3. It works just fine but
    By jcmhex in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2005, 06:53 PM
  4. Wont run in X, works fine in alt+F1 terminal
    By zmerlinz in forum Linux Programming
    Replies: 5
    Last Post: 04-28-2004, 11:58 AM
  5. My computer works just fine
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 07-02-2002, 08:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21