Thread: Help please explain what code does

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Help please explain what code does

    Can you guys please help me with what the code does?I think its part of a program.Im really new to C (a week of studying) so Id be really glad if u helped.Also Im sorry if this isn't the right place to post this.
    This is the code :

    Code:
    SOCKADDR_IN address;
    	SOCKET client;
    	WSADATA data;
    
    	if (WSAStartup(MAKEWORD(1,1), &data) != 0) {
    		throw( "HttpCommunicator00131" );
    		return;
    	}
    
    	struct hostent *host = gethostbyname( HOST );
    	if (host == NULL) {
    		WSACleanup();
    		throw( "HttpCommunicator00132" );
    		return;
    	}
    
    	client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if (client == INVALID_SOCKET) {
    		WSACleanup();
    		throw( "HttpCommunicator00133" );
    		return;
    	}
    
    	memset(&address, 0, sizeof(address));
    	address.sin_addr.s_addr = *((unsigned long*)host->h_addr);
    	address.sin_family = AF_INET;
    	address.sin_port = htons( PORT );
    
    	if (connect(client, (LPSOCKADDR)&address, sizeof(address)) != 0) {
    		closesocket( client );
    		WSACleanup();
    		throw( "HttpCommunicator00134" );
    		return;
    	}
    
    	char parameters[ HTTP_PARAMETERS_BUFFER_SIZE ] = "";
    	sprintf(parameters, "symbol=%s&period=%d", symbol, period);
    	sprintf(buffer, "POST %s HTTP/1.0\nHost: %s\r\nContent-Length: %d\r\nConnection: Keep-Alive\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n%s\r\n", LOAD_TRAINING_SET_SCRIPT, HOST, strlen(parameters), parameters);
    	if (send(client, buffer, strlen(buffer), 0) == SOCKET_ERROR) {
    		closesocket( client );
    		WSACleanup();
    		throw( "HttpCommunicator00135" );
    		return;
    	}
    
    	int numberOfBytes = recv(client, buffer, BUFFER_SIZE-1, MSG_WAITALL);
    	if (numberOfBytes == SOCKET_ERROR) {
    		closesocket( client );
    		WSACleanup();
    		throw( "HttpCommunicator00136" );
    		return;
    	}
    	buffer[ numberOfBytes ] = '\0';

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by cybersasho
    I think its part of a program.
    Madness!

    Looks like part of an HTTP server. If I'm reading this right, your code establishes a HTTP session in response to a remote host connecting and waits for further communications from it. Where did you copy this from?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Well in one of the OPs cross-posts it's described as homework. The other cross-post does not mention it's homework.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well letīs fix one of them!
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone explain a line of code for me please?
    By drkidd22 in forum C Programming
    Replies: 8
    Last Post: 12-09-2009, 10:36 PM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM

Tags for this Thread