![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 252
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#3 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 252
Rep Power: 4
![]() |
ok thanks, i just got Visual C++ express, maybe it will work a little better
|
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
send(server,"HTTP/1.1",512,0); 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. |
|
|
|
|
|
#5 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
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"); |
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 ;) |
|
|
|
|
|
#7 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 252
Rep Power: 4
![]() |
wow, thanks for all the help
|
|
|
|
|
|
#8 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 252
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#9 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#10 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|