Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,386 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,119 people online right now. Registration is fast and FREE... Join Now!




Web server

 
Reply to this topicStart new topic

Web server, simple server to return pages to a www client

GreenBeret87
24 Jan, 2008 - 10:09 AM
Post #1

New D.I.C Head
*

Joined: 15 Jan, 2008
Posts: 8


My Contributions
Hiya, i'm new to c++ and am struggling.

Atm i am doing some tasks to help me learn, up untill now i have been fine but for this one i am completely stuck with no clue where to go. (other than here of course)

i've been given a really cut down snippet of code and the tasks are to make it:
return pages to a real WWW client(only needs to do get requests)
must use its own port
return content type
server should always send a "Connection : close"
return content length and when it was last modified
correct error message where appropriate

If you could point me in the right direction as to how do these requirements that would be great.

Also if you could explain how this code is actually working that would be a great help too.
CODE
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <pthread.h>
#include <iostream>
using namespace std;


extern void *serve(void *);

int main(int argc, char *argv[]){
  int port = htons(atoi(argv[1]));
  char buffer[4096];
  int ssock, connsock, stat, r;

  struct sockaddr_in myaddr;
  ssock = socket(AF_INET, SOCK_STREAM, 0);
  myaddr.sin_family = AF_INET;
  myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  myaddr.sin_port = port;
  stat=bind(ssock,(struct sockaddr *)&myaddr,sizeof(myaddr));
  if(stat < 0) { cerr << "cant bind\n"; exit(1);}
  listen(ssock,5);

  pthread_t th;
  while(true) {
    connsock=accept(ssock,0,0);
    r=pthread_create(&th,0,serve,(void *)connsock);
  }
}
void *serve(void *s) {
  int sock = (int)s, rc;
  char buffer[2048];
  const char *reply="HTTP/1.0 501 Method Not Implemented\r\n\r\n<h1>sorry</h1>\r\n";

  pthread_detach(pthread_self());
  rc = read(sock,buffer,2048);
  write(1,buffer,rc);
  write(sock,reply,strlen(reply));
  close(sock);  pthread_exit(0);
}






User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Web Server
24 Jan, 2008 - 11:05 AM
Post #2

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
http://beej.us/guide/bgnet/output/html/multipage/index.html


The Google Search I used was "c++ network programming" wink2.gif
User is offlineProfile CardPM
+Quote Post

GreenBeret87
RE: Web Server
27 Jan, 2008 - 04:56 AM
Post #3

New D.I.C Head
*

Joined: 15 Jan, 2008
Posts: 8


My Contributions
Thanks alot, the link u gave me is proving very very helpful.
User is offlineProfile CardPM
+Quote Post

GreenBeret87
RE: Web Server
30 Jan, 2008 - 10:23 AM
Post #4

New D.I.C Head
*

Joined: 15 Jan, 2008
Posts: 8


My Contributions
Need help, I'm using a remote login and when i compile the code above i get an error saying that i am losing precision.

It is coming from lines 32 and 33

CODE
void *serve(void *s) {
int sock = (int)s, rc;


I have no idea why it won't compile.

the error message i am getting is

error: cast from 'void*' to 'int' loses precision

Please help
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 04:58PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month