This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

fork() works ?? Server development questions


Hi,

This is a re-post of my 04/06/2002 message
Many thanks to Gerrit and Phil Frisbie, Jr. for messages

But despite Gerrit seems to make it work a little better (not on local
host), it'not enough for me
Thanks to Phil for ideas but pthread are not suitable for me cause my target
project(not sample code below)
should run on systems wich were not pthread friendly !!

So big questions are still there :

Is it possible to run a server like sample code i give ?
If yes could somebody send me sample piece of code wich works ?
Is it possible to rely on fork() in cygwin ?
Is there sockets issue in the way i use it ?

I will be please, to read from Corinna Vinschen seems to be the maintainer
of portion of Cygwin, I question about

Thanks

Here start my original post

I'm in trouble porting a socket base utility from my own between mulptiple
OS.

The goal is to maintain as much as possible the same code, since this
utility
do not use advanced features (I DO NOT WANT TO CODE AN HTTP SERVER !!)

For MS plateform I hope in cygwin environment, but despite the fact it
compiles well
I do not achieve srv programm to work.

Below you will find text for four files srv.c http.c http.h and
response.html which are not the
target code, but permit to see the real problem.

This code compile under AIX whith native C compiler and other unices and run
cleanly.
This is only for test and many things were tricky, for example http HEADER
is hardcoded in the
html file !!

The problem is under cygwin environment 1.3.10 Win2k pro SP2

To test simply compile and launch srv -> gcc -o srv http.c srv.c (for AIX
cc -o srv http.c srv.c)

Then launch your favorite browser and enter those url
http://@IP_host_running_srv:12345/
just remplace "@IP_host_running_srv" by proper address or hostname for you

(Hum just thinking i only test with IE !!)

You may get a page saying hello WORLD !

Just running fine for me when running srv under AIX !

Got nothing when running on Win2k, all server trace appears in console
window but nothing in browser !!

Then I quickly set up a proxy in java based on BufferedReader and
BufferedWriter classes to
see dialog between browser and srv and surprise in  this case it'works !!!

finally here are my questions

1- : Any error in the code ?
2- : If not, what's wrong cygwin, Win2k ?
3- : Is it possible to set up this type of server using cygwin under MS
windows
     (interrested in NT and W2k, forking a  child per client !) ?
4- : What is different when using proxy ?
5- : Signal handlers doesn't not work under cygwin why ?

Thanks for any help and answers !!

8=<---------------------------srv.c----------------------------------
#include <stdio.h>
#include <signal.h>
#include <netinet/in.h>
#include <sys/socket.h>

#include "http.h"

void sigchild(int sig){
  int pid;
  pid = wait(NULL);
  signal(sig, sigchild);
}

int done = 0;
void sigterm(int sig){
  fprintf(stdout, "DEBUG : request to shutdown !");
  done = 1;
}

void handle_http(int sock){
  if (sendHttp(sock, "response.html") == -1){
    perror("sendHttp");
  }
}

int main(void) {
  int listenSock;
  int dialogSock;
  int pid;
  int flag;
  struct sockaddr_in address;
  size_t length;

  signal(SIGCHLD, sigchild);
  signal(SIGTERM, sigterm);
  signal(SIGINT, sigterm);
  signal(SIGHUP, sigterm);

  if ((listenSock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
    perror("socket");
    return(1);
  }

  memset(&address, 0, sizeof(struct sockaddr));
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = htonl(INADDR_ANY);
  address.sin_port = htons(12345);

  flag = 1;
  if (setsockopt(listenSock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int)) <
0){
    perror("setsockopt");
    return(1);
  }

  if (bind(listenSock, (struct sockaddr *) &address, sizeof(address)) < 0){
    perror("bind");
    return(1);
  }

  length = sizeof(struct sockaddr_in);

  if (getsockname(listenSock, (struct sockaddr *) &address, &length) < 0){
    perror("getsockname");
    return(1);
  }

  fprintf(stdout, "DEBUG : Address IP = %s,", inet_ntoa(address.sin_addr));
  fprintf(stdout, " Port = %u \n", ntohs(address.sin_port));

  listen(listenSock, 5);

  while(!done){
    fprintf(stdout, "DEBUG : Server waiting...\n");

    length = sizeof(struct sockaddr_in);

    dialogSock = accept(listenSock, (struct sockaddr *) &address, &length);

    if (dialogSock < 0)
      continue;

    pid = fork();

    switch(pid){
      case 0 :
        close(listenSock);
        handle_http(dialogSock);
        close(dialogSock);
        fprintf(stdout, "DEBUG : child done\n");
        return(0);
      default :
        fprintf(stdout, "DEBUG : fork child %d", pid);
        fprintf(stdout, " for %s:", inet_ntoa(address.sin_addr));
        fprintf(stdout, "%d\n", ntohs(address.sin_port));
        close(dialogSock);
      break;
    }
  }
  close(listenSock);
  return(0);
}

8=<---------------------------http.c--------------------------------
#include <stdio.h>
#include <fcntl.h>
#include <sys/socket.h>

#include "http.h"

int sendHttp(int sock, char * pathToFile){
  int fd;
  char c;
  int rc;

  fd=open(pathToFile, O_RDONLY, 0);
  if (fd == -1){
    perror("http.c open");
    return -1;
  }

  while((rc = read(fd, &c, 1)) > 0){
    if ((rc = write(sock, &c, 1)) == -1) {
      perror("http.c write");
    }else{   write(fileno(stdout), &c, 1);
    }
  }

  close(fd);
  fprintf(stdout, "DEBUG : file forwarded !\n");
  return 0;
}
8=<---------------------------http.h--------------------------------
#ifndef _HTTP_H
#define _HTTP_H
int sendHttp(int sock, char * pathToFile);
#endif
8=<---------------------------response.html-------------------------
HTTP/1.0 200 OK
DATE : Fri, 05 Apr 2002 11:22:12 DFT
Server : DummyWS/0.1
Content-Type : text/HTML
Content-Length : 112
Last-Modified : Thu, 21 Mar 2002 00:11:12 NFT

<html>
<head>
<title>Test page</title>
</head>
<body>
<center>
<h1>Hello WORLD !</h1>
</center>
</body>
</html>
8=<-----------------------------------------------------------------




--






--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]