Please Help! Socket Problem

This is a discussion on Please Help! Socket Problem within the C Programming forums, part of the General Programming Boards category; Hi. I would like to make a c program that will be able to download a webpage, then save it ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    Unhappy Please Help! Socket Problem

    Hi.

    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

    Code:
    /**
     * 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 */
    	if(WSAStartup(MAKEWORD(2,2),&wsaData))
    	{
    		DisplayError("Error Initializing Sockets",GetLastError());
    		return FALSE;
    	}
    
    	/* create a socket descriptor */
    	s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    	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;
    	while(1)
    	{
    		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;
    
    	closesocket(s);
    
    	return TRUE;
    }
    
    void DisplayError(LPCTSTR lpszError, int errornum)
    {
    	char szError[256];
    
    	if(errno)
    		sprintf(szError,"%s\n\nError Number: %d",lpszError,errornum);
    	else
    		sprintf(szError,"%s",lpszError);
    	MessageBox(NULL,szError,"ERROR",MB_OK | MB_ICONERROR);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    #include<window.h>


    #include<winsock.h>

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, 03:46 AM
  3. Problem with network code
    By cornholio in forum Linux Programming
    Replies: 1
    Last Post: 12-20-2005, 12: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, 09:01 AM

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