Thread: Please Help! Socket Problem

  1. #1
    Registered User
    Join Date
    Mar 2005

    Unhappy Please Help! Socket Problem


    I would like to make a c program that will be able to download a webpage, then save it the the harddisk, through using sockets.
    For example, it will download a webpage onto the disk as an HTML file .

    I am finding this very difficult,

    I have found the following code, but when i compile it with gcc it comes up with about 20 errors

    I would be grateful for any help at all

     * This function sends the http headers to the specified server.  The result is then
     * copied into a passed buffer.
     *	lpszServer - The webserver address
     *	lpszHttp - The http request
     *	data - buffer to hold the html returned
     *	datasize - the size of the data buffer
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    BOOL SendHttp(LPCTSTR lpszServer, LPCTSTR lpszHttp, LPTSTR data, UINT datasize)
    	SOCKET s;
    	WSADATA wsaData;
    	struct sockaddr_in hostaddr;
    	struct hostent *serverent;
    	char *serverip;
    	char buff[1024];
    	int i,bytes;
    	/* Initialize sockets */
    		DisplayError("Error Initializing Sockets",GetLastError());
    		return FALSE;
    	/* create a socket descriptor */
    	if(s == INVALID_SOCKET)
    		DisplayError("Error creating socket.",GetLastError());
    		return FALSE;
    	/* Get a hostent structure from the domain name */
    	if(!(serverent = gethostbyname(lpszServer)))
    		DisplayError("Could not resolve host name.",GetLastError());
    		return FALSE;
    	/* Get the ip address from the hostent structure */
    	if(!(serverip = inet_ntoa(*(struct in_addr *)*serverent->h_addr_list)))
    		DisplayError("Call to inet_ntoa failed",0);
    		return FALSE;
    	memset(&hostaddr,0,sizeof(struct sockaddr_in));
    	hostaddr.sin_family = AF_INET;
    	hostaddr.sin_addr.s_addr = inet_addr(serverip);
    	hostaddr.sin_port = htons(80);
    	/* Connect to the server */
    	if(connect(s,(struct sockaddr*)&hostaddr,sizeof(struct sockaddr)))
    		DisplayError("Unable to connect to server.",GetLastError());
    		return FALSE;
    	/* Send the http headers */
    	if(send(s,lpszHttp,strlen(lpszHttp),0) == SOCKET_ERROR)
    		DisplayError("Error Sending HTTP data.",GetLastError());
    		return FALSE;
    	/* Receive a response */
    	i = 0;
    		bytes = recv(s,buff,sizeof(buff),0);
    		if(bytes <= 0) break;
    		if( (bytes + i + 1) > datasize) break; /* dont overflow the buffer */
    		memcpy(data + i, buff,bytes);
    		i += bytes;
    	data[i] = 0;
    	return TRUE;
    void DisplayError(LPCTSTR lpszError, int errornum)
    	char szError[256];
    		sprintf(szError,"%s\n\nError Number: %d",lpszError,errornum);
    	MessageBox(NULL,szError,"ERROR",MB_OK | MB_ICONERROR);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > I have found the following code, but when i compile it with gcc it comes up with about 20 errors
    I'm not surprised - it begin by #including unix-style headers, and ends up calling microsoft winsock functions.

    Also, saying "gcc" is your compiler isn't specific enough - there are many different ports for many different operating systems and processors. In short be specific.

    Pasting a few error messages wouldn't go amiss either.
    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.

  3. #3
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 02:15 AM
  2. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 04:46 AM
  3. Problem with network code
    By cornholio in forum Linux Programming
    Replies: 1
    Last Post: 12-20-2005, 01:21 AM
  4. sockets problem programming
    By kavejska in forum C Programming
    Replies: 0
    Last Post: 07-25-2005, 07:01 AM
  5. Client/Server Socket Receive Problem
    By mariabair in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-25-2003, 10:01 AM