Winsock Webserver Problem

This is a discussion on Winsock Webserver Problem within the Networking/Device Communication forums, part of the General Programming Boards category; I am writing a webserver in C++, using Winsock. It is complete, except for one thing. Global: char HtmlWhole[1000]; Code: ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    5

    Winsock Webserver Problem

    I am writing a webserver in C++, using Winsock. It is complete, except for one thing.

    Global:
    char HtmlWhole[1000];

    Code:
    int SendFile(char *FileName)
    {
    	HANDLE hFile;
    	DWORD dwNumRead;
    	BOOL bFile;
    	char FilePath[1000];
    	char Response[1000];
    	sprintf(FilePath, "c:\\webserver\\%s", FileName);
    
    	hFile = CreateFile(FilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    	bFile = ReadFile(hFile, HtmlWhole, sizeof(DWORD)*256, &dwNumRead,NULL);
    	bFile = CloseHandle(hFile);
    
    	sprintf(Response, "HTTP/1.1 200 OK\r\nDate: Sun, 05 Jun 2005 22:15:06 GMT\r\nContent-Length: %d\r\nContent-Type: text/html\r\nCache-Control: private\r\nServer: Server/1.0\r\n\r\n", strlen(HtmlWhole));
    
    	printf(Response);
    
    	send(httpClient, Response, sizeof(Response), 0);
    	send(httpClient, HtmlWhole, sizeof(HtmlWhole), 0);
    
    	return 0;
    }
    This code sends the requested page/file to the client. The problem is, that every file I send using that function comes out like this:
    http://img296.echo.cx/img296/5924/html1ne.jpg

    When I printf the File Output, it printf the HTML code fine. But when I send it to the client, it shows up like that.

    TIA..

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    int SendFile(char *FileName)
    {
    	HANDLE hFile;
    	DWORD dwNumRead;
    	BOOL bFile;
    	char FilePath[1000];
    	char Response[1000];
    	sprintf(FilePath, "c:\\webserver\\%s", FileName);
    
    	hFile = CreateFile(FilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    	bFile = ReadFile(hFile, HtmlWhole, sizeof(DWORD)*256 (1), &dwNumRead,NULL);
    	bFile = CloseHandle(hFile);
    
    	sprintf(Response, "HTTP/1.1 200 OK\r\nDate: Sun, 05 Jun 2005 22:15:06 GMT\r\nContent-Length: %d\r\nContent-Type: text/html\r\nCache-Control: private\r\nServer: Server/1.0\r\n\r\n",  strlen(HtmlWhole)(2));
    
    	printf(Response);
    
    	send(httpClient, Response, sizeof(Response) (3), 0);
    	send(httpClient, HtmlWhole, sizeof(HtmlWhole) (4), 0);
    
    	return 0;
    }
    1. This argument specifies the number of bytes to read. In this case, you should provide the value sizeof(HtmlWhole) - 1
    2. It is unlikely that the file contained a nul terminating character. Therefore, you can not use strlen or other string functions on the buffer at this point. You can either explicitly nul terminate the buffer first:
      Code:
      HtmlWhole[dwNumRead] = '\0';
      or use the dwNumRead variable directly to indicate the length of the string.
    3. sizeof(Response) will return the size of the array in bytes (1000 in this case) rather than the length of the string in the buffer. Therefore, you are sending a lot of garbage characters after your response. This is what you are seeing in the webbrowser. You should use strlen(Response).
    4. Same problem. You should use either dwNumRead here or strlen(HtmlWhole) if you have nul terminated the response string.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    5
    Thanks, but when I use:

    Code:
    send(httpClient, Response, strlen(Response), 0);
    send(httpClient, HtmlWhole, strlen(HtmlWhole), 0);
    It just times out.



    When someone requests a file, for eg: hello.html (Which contains the following):
    Code:
    <title>title</title>
    <b>test</b>
    ...the console would output:
    Code:
    <title>title</title><b>test</b>                                     <The html file input>
    
    HTTP/1.1 200 OK                                                           <The response>
    Date: Sun, 05 Jun 2005 22:15:06 GMT
    Content-Length: 31
    Content-Type: text/html
    Cache-Control: private
    Server: Server/1.0
    Then would send the garbage...









    ...But when I do this:

    Code:
    //Globals
    char TestResponse[] = { "HTTP/1.1 200 OK\r\n"
    			"Date: Sun, 05 Jun 2005 13:15:06 GMT\r\n"
    			"Content-Length: 10\r\n"
    			"Content-Type: text/html\r\n"
    			"Cache-Control: private\r\n"
    			"Server: BoNlol/1.0\r\n"
    			"\r\n" };
    char TestHTML[] = { "<b>hi</b>" };
    Then:
    Code:
    send(httpClient, TestResponse, sizeof(TestResponse), 0);
    send(httpClient, TestHTML, sizeof(TestHTML), 0);
    It would work fine. ( http://images5.theimagehosting.com/html2.JPG )

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    1) It's not the cause of your problem, but you should do some error checking when opening and reading from files then send an appropriate response if unsuccessful.
    2) Post the updated code of your function so there is no question what you're currently working with.
    3) You should understand that it is a waste of valuable bandwidth (as slight as it may be) to send sizeof(...) equal to 1000 bytes when the data you want to send is a mere 31 bytes, in your example.
    4) What is the output of strlen(Response) and strlen(HtmlWhole) when you send?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    5
    Thanks for the replies..

    OK, well this is my current function:

    Code:
    int SendFile(char *FileName)
    {
    	HANDLE hFile;
    	DWORD dwNumRead;
    	BOOL bFile;
    	char FilePath[1000];
    	char Response[1000];
    	sprintf(FilePath, "c:\\webserver\\%s", FileName);
    
    	hFile = CreateFile(FilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    	bFile = ReadFile(hFile, HtmlWhole, sizeof(DWORD)*256, &dwNumRead,NULL);
    	bFile = CloseHandle(hFile);
    
    	printf(HtmlWhole);
    
    	sprintf(Response, "HTTP/1.1 200 OK\r\nDate: Sun, 05 Jun 2005 22:15:06 GMT\r\nContent-Length: %d\r\nContent-Type: text/html\r\nCache-Control: private\r\nServer: Server/1.0\r\n\r\n", strlen(HtmlWhole);
    
    	printf(Response);
    
    	send(theClient, Response, sizeof(Response), 0);
    	send(theClient, HtmlWhole, sizeof(Response), 0);
    
    	return 0;
    }
    When I attempt to send 'test.html':
    Code:
    <title>title</title>
    <b>test</b>
    The value of strlen(HtmlWhole) is 31, and strlen(Response) is 145.

    I know about the bandwidth problem, but I will be fixing that after I get the sending working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 12:11 PM
  2. Winsock packet problem
    By Rare177 in forum Windows Programming
    Replies: 7
    Last Post: 10-05-2004, 05:53 PM
  3. Winsock Problem
    By Noxir in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2004, 11:50 AM
  4. WinSock Problem
    By loobian in forum C++ Programming
    Replies: 1
    Last Post: 02-09-2002, 11:25 AM
  5. Small Winsock problem...
    By SyntaxBubble in forum C++ Programming
    Replies: 0
    Last Post: 02-09-2002, 10:09 AM

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