Keeping connection alive

This is a discussion on Keeping connection alive within the C++ Programming forums, part of the General Programming Boards category; I was wondering what i am doing to make the connection not stay alive for more than about 30 seconds. ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    Keeping connection alive

    I was wondering what i am doing to make the connection not stay alive for more than about 30 seconds. I am trying to connect to googles IP on port 80 and have the program keep the connection alive for a long time. I also want it to close the socket connection every few minutes and reconnect. I am trying to do this while hiding the command prompt from the user because my friend said it is impossible please help me
    Code:
    #include <iostream>
    #include <windows.h>
    #include <winsock.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string>
    using namespace std;
    int main(int argc,char *argv[])
    
    
        {
    		int reconnect;
    
    	//Hides command prompt from user
        HWND hwnd1 = FindWindow(NULL,argv[0]);
        ShowWindow(hwnd1,SW_HIDE);
    
    
        	string ip;
        	int port;
    
        	//Define ip and port
    		//This is yahoo.com ip on http port right now
        	ip = "72.14.207.99";
        	port = 80;	
        	
        	WSADATA sockData;
        	WORD version;
        	SOCKET conn;
        	
    
        	int error;
        	
    
        	version = MAKEWORD(1,1);
        	
    
        	error = WSAStartup(version, &sockData);
    
        	conn = socket(AF_INET,
        			 	 SOCK_STREAM,
        				 IPPROTO_TCP);
        				 
        	SOCKADDR_IN Winsock;
        	
        	Winsock.sin_family = AF_INET;
        	Winsock.sin_port = htons(port);
        	Winsock.sin_addr.s_addr	= inet_addr(ip.c_str());
            connect:
        	//Connect on the socket
        	if(connect(conn, (SOCKADDR*) &Winsock, sizeof(Winsock)) == SOCKET_ERROR)
    
    
            	{
            		
            		cout << "An exception has occured (" << WSAGetLastError() << ")\n";
            		//Cleanup socket
            		WSACleanup();
            		system("PAUSE");
            		return EXIT_FAILURE;
            	}
    		//cout << "Socket established";
    
            	//Close socket connection and reconnect
    			Sleep(60000);
    			//cout << "Socket reconnecting";
            	closesocket(conn);
            	return EXIT_SUCCESS;
    			system("cls");
    			goto connect;
    		
        }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    that link didnt work lol and i dont know y it wont keep the connection alive

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple forks in one client connection (ftpclient)
    By Dynamo in forum Networking/Device Communication
    Replies: 5
    Last Post: 01-16-2011, 11:41 AM
  2. Check if a host is alive
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 05-05-2009, 11:10 AM
  3. Socket Connection Still Alive!!
    By maven in forum Networking/Device Communication
    Replies: 2
    Last Post: 07-21-2006, 02:14 PM
  4. How do I check if the connection is still alive?
    By jaro in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 01:10 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM

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