Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 20th, 2006, 5:08 PM   #1
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 252
Rep Power: 4 Brent is on a distinguished road
C++ webserver trouble

I created a webserver using C++ and winsock.
all its supposed to do is startup and send a message to any internet browser that connects to it.It compiled fine but when I try to connect to it, nothing is displayed on the internet browser, it just continues to load but never loads. Could someone help me sort this out. I am using a open watcom compiler on Windows xp.


here's the code:

#include<winsock2.h>
#include<iostream.h>
#include<fstream.h>
#include<string.h> 
#pragma comment(lib,"wsock32.lib")



SOCKET client;
SOCKET server;	


char file[50];
char send_file[5000];
char *str_file;
char *file_dir;
int a;
ifstream infile;
	
int main()
{
	WORD sockversion;
	WSADATA wsaData;
	int net;
	
	sockversion=MAKEWORD(2,2);
	WSAStartup(sockversion,&wsaData);
	
	

	
	server=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(server==INVALID_SOCKET)
	{
		cout<<"invalid socket\n";
		WSACleanup();
		return 0;
	}
	
	sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(80);
	sin.sin_addr.s_addr=INADDR_ANY;
	
	net=bind(server,(LPSOCKADDR)&sin,sizeof(sin));
	if(net==SOCKET_ERROR)
	{
		cout<<"failed bind\n";
		WSACleanup();
		return 0;
	}
	
	if((net=listen(server,4))!=SOCKET_ERROR)
	{
		while(true)
		{
			client=accept(server,NULL,NULL);
			if(client==INVALID_SOCKET)
			{
				cout<<"error accepting connection\n";
			}
			else
			{
				cout<<"client connected\n";
			}
			
			
			
			

			char string[512]="<html><body><h1>Welcome, webserver is under 

construction</h1></body></html>\n";
				
			send(server,"HTTP/1.1",512,0);
			send(server,string,sizeof(string),0);
			send(server,"Accept: image/jpeg ",512,0);
			send(server,"Accept-Language: es",512,0);
			send(server,"Accept-Encoding: gzip",512,0);
			send(server,"User-Agent: MSIE 6.0",512,0);
			send(server,"Host: 127.0.0.1",512,0);
			send(server,"Server: brentserver (windows)",512,0);
			send(server,"Connection:  close",512,0);
			send(server,"Content-Type: text/html",1024,0);
			send(server,"Content-Length: 1024",512,0);
			
				
					
				
		}
	}
	else
	{
		cout<<"failed listen\n";
		WSACleanup();
		closesocket(client);
		closesocket(server);
	}
	WSACleanup();
	closesocket(client);
	return 0;
}


sorry about the indentations, my code editor is a pain to deal with.
Brent is offline   Reply With Quote
Old May 20th, 2006, 6:42 PM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
First thing to do is get another compiler, I think you are having a problem with your sending in the right order. I don't know it so try a small serach but try something like:
#include <winsock2.h>
#include <iostream>
#pragma comment(lib,"wsock32.lib")

using namespace std;

SOCKET client;
SOCKET server;

int main()
{
	WSADATA wsaData;

	WORD sockversion = MAKEWORD(2,2);
	WSAStartup(sockversion,&wsaData);

	server = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(server == INVALID_SOCKET)
	{
		cout << "Invalid socket\n";
		WSACleanup();
		return 0;
	}

	sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(80);
	sin.sin_addr.s_addr = INADDR_ANY;

	int net = bind(server, (LPSOCKADDR)&sin, sizeof(sin));
	if(net == SOCKET_ERROR)
	{
		cout << "Failed bind\n";
		WSACleanup();
		return 0;
	}

	if((net = listen(server, 4)) != SOCKET_ERROR)
	{
		while(true)
		{
			client = accept(server, NULL, NULL);
			if(client == INVALID_SOCKET)
			{
				cout << "Error accepting connection\n";
			}
			else
			{
				cout << "Client connected\n";
			}

			string message = "<html><body><h1>Welcome, webserver is under construction</h1></body></html>";

			send(server, "HTTP/1.1", 512, 0);
			send(server, "Accept: image/jpeg ", 512, 0);
			send(server, "Accept-Language: en", 512, 0);
			send(server, "Accept-Encoding: gzip", 512, 0);
			send(server, "User-Agent: MSIE 6.0", 512, 0);
			send(server, "Host: 127.0.0.1", 512, 0);
			send(server, "Server: server (windows)", 512, 0);
			send(server, "Connection:  close", 512, 0);
			send(server, "Content-Type: text/html", 1024, 0);
			send(server, "Content-Length: 1024", 512, 0);

		}
	}
	else
	{
		cout << "Failed listen\n";
		WSACleanup();
		closesocket(client);
		closesocket(server);
	}
	WSACleanup();
	closesocket(client);
	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 20th, 2006, 7:53 PM   #3
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 252
Rep Power: 4 Brent is on a distinguished road
ok thanks, i just got Visual C++ express, maybe it will work a little better
Brent is offline   Reply With Quote
Old May 20th, 2006, 9:56 PM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
send(server,"HTTP/1.1",512,0);
This will send "HTTP/1.1" and then 504 bytes of potential garbage. You also need to send back the actual status code and have a carriage return + line feed after each line
send(server, "HTTP/1.x 200 OK\r\n", 17);

You also need to send the HTML after the headers, not in the middle of them. You need to return a blank line after the headers to indicate there is no more headers.
The Dark is online now   Reply With Quote
Old May 21st, 2006, 2:29 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by The Dark
send(server,"HTTP/1.1",512,0);
This will send "HTTP/1.1" and then 504 bytes of potential garbage. You also need to send back the actual status code and have a carriage return + line feed after each line
send(server, "HTTP/1.x 200 OK\r\n", 17);
Rather than hardcoding the length, do something a bit more general purpos, such as;
void SendIt(SOCKET server, const char *line)
{
   if (line != NULL)
   {
         int length = (int)strlen(line);   // do NOT compute sizeof(line) here
         if (length > 0) send(socket, line, length);
   }
}

// and to call it

SendIt(server, "HTTP/1.x 200 OK\r\n");
grumpy is offline   Reply With Quote
Old May 21st, 2006, 11:23 AM   #6
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Grumpy, it's okay to use C++ in the C++ forum. :p

@OP: Here you go:
#include <iostream>
#include <sstream>
#include <winsock2.h>

#pragma comment(lib, "wsock32.lib")

using namespace std;

SOCKET client;
SOCKET server;

void SendIt(SOCKET socket, string line)
{
         int length = (int)line.length();
         if (length > 0)
         	send(socket, line.c_str(), length, 0);
}

int main()
{
	WSADATA wsaData;

	WORD sockversion = MAKEWORD(2, 2);
	WSAStartup(sockversion, &wsaData);

	server = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(server == INVALID_SOCKET)
	{
		cerr << "Invalid socket" << endl;
		WSACleanup();
		return 0;
	}

	sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(80);
	sin.sin_addr.s_addr = INADDR_ANY;

	int net = bind(server, (LPSOCKADDR)&sin, sizeof(sin));
	if(net == SOCKET_ERROR)
	{
		cout << "Failed bind" << endl;
		WSACleanup();
		return 0;
	}

	if((net = listen(server, 4)) != SOCKET_ERROR)
	{
		while(true)
		{
			client = accept(server, NULL, NULL);
			if(client == INVALID_SOCKET)
			{
				cerr << "Error accepting connection" << endl;
			}
			else
			{
				cerr << "Client connected" << endl;
			}

			string message = "<html><body><h1>Welcome, webserver is under construction</h1></body></html>";
			std::ostringstream streamLength;
			streamLength << message.length();

			SendIt(client, "HTTP/1.1 200 OK\r\n");
			SendIt(client, "Date: Sun, 22 May 2006 14:58:00 GMT\r\n");
			SendIt(client, "Server: webserver 0.0001\r\n");
			SendIt(client, "Content-Type: text/html; charset=iso-8859-1\r\n");
			SendIt(client, "Content-Length: ");
			SendIt(client, streamLength.str());
			SendIt(client, "\r\n");
			SendIt(client, "Connection: close\r\n");

			// blank line for end of headers
			SendIt(client, "\r\n");
			SendIt(client, message.c_str());
			closesocket(client);
		}
	}
	else
	{
		cerr << "Failed listen" << endl;
		WSACleanup();
		closesocket(client);
		closesocket(server);
	}
	WSACleanup();
	closesocket(server);
	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates

Last edited by nnxion; May 21st, 2006 at 11:34 AM. Reason: space between comma ;)
nnxion is offline   Reply With Quote
Old May 21st, 2006, 4:51 PM   #7
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 252
Rep Power: 4 Brent is on a distinguished road
wow, thanks for all the help
Brent is offline   Reply With Quote
Old May 21st, 2006, 5:27 PM   #8
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 252
Rep Power: 4 Brent is on a distinguished road
ok, i used the code nnixion gave me, it compiles fine, but the page wont load, and i get this on the server screen:

GET / HTTP/1.1
Accept: */*
Accept-Language: es
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0
.50727)
Host: 127.0.0.1
Connection: Keep-Alive

any suggestions
Brent is offline   Reply With Quote
Old May 21st, 2006, 5:30 PM   #9
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by grumpy
Rather than hardcoding the length, do something a bit more general purpos, such as;
I just wanted to show why his server wasn't returning anything valid, rather than confuse him with a whole new function.
The Dark is online now   Reply With Quote
Old May 21st, 2006, 6:24 PM   #10
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by Brent
any suggestions
Hmm yeah, I was closing the client socket, after I sent the headers and body.
I got a weird effect from it though, in Firefox it worked fine, in Opera it worked most of the time, and in IE it worked some of the time.

I suggest reading this.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:07 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC