home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 February
/
CHIPCD_2_2000.iso
/
software
/
kamery
/
webcam
/
webcam32.exe
/
webcam32cgi.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-12-09
|
4KB
|
184 lines
/*
File: client.c - Generic Unix flavor
Project: WebCam32
Author: Neil Kolban
Date: 05/27/97
Function:
This program is a CGI client which receives information from the Web Browser
and attempts to communicate with the back-end server WebCam32 application
using sockets.
*/
#include <errno.h> /* errno extern */
#include <unistd.h> /* gethostname() */
#include <fcntl.h> /* File info */
#include <sys/time.h> /* Required for struct timeval */
#include <sys/select.h> /* Required for select() */
#include <sys/socket.h> /* Sockets constants */
#include <netdb.h>
#include <netinet/in.h>
#define WEBCAM_PORT (8888) /* Deafult Webcam32 port */
static char *headerTEXT="Content-type: text/plain\n\n";
/*
Function: WaitForSocket
Inputs:
o fd - File descriptor on which to wait for data
o seconds - Number of seconds before read timeout.
*/
int WaitForSocket(int fd, int seconds)
{
struct timeval timeout;
fd_set readfds;
int rc;
timeout.tv_sec = seconds;
timeout.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
rc = select(fd+1, &readfds, NULL, NULL, &timeout);
if (rc <= 0)
{
return(0);
}
return(1);
}
/*
Function: WriteHTML
Purpose:
Send HTML output to stdout (file descriptor 1)
*/
void WriteHTML(char *data, int length)
{
write(1, data, length);
return;
}
void WriteErrorText(char *errorMessage)
{
WriteHTML(headerTEXT, strlen(headerTEXT));
WriteHTML(errorMessage, strlen(errorMessage));
return;
}
int main(int args, char *argv[])
{
int rc;
int servfd;
struct sockaddr_in servsockaddr;
long address;
short port;
struct hostent *he;
char hostname[256];
char buffer[64*1024];
char requestMessage[]="WEBCAM32CGI\n";
char *queryString;
char *ptr;
int i;
/*setmode(fileno(stdout), O_BINARY); */
queryString = (char *)getenv("QUERY_STRING");
if (queryString == NULL || strlen(queryString)==0)
{
rc = gethostname(hostname, sizeof(hostname));
if (rc == -1)
{
sprintf(buffer,
"Webcamcgi: Failed to gethostname(): %d\n",
errno);
WriteErrorText(buffer);
return(0);
}
port = WEBCAM_PORT;
}
else
{
ptr = queryString;
for (i=0; *ptr != ',' && *ptr != '\0'; i++, ptr++)
{
if (*ptr != ' ' && *ptr !='\t')
{
hostname[i] = *ptr;
}
}
hostname[i]='\0';
if (*ptr == '\0')
{
port = WEBCAM_PORT;
}
else
{
port = atoi(ptr+1);
}
}
servfd = socket(AF_INET, SOCK_STREAM, 0);
if (servfd < 0)
{
printf("Failed to socket()\n");
return(0);
}
address = inet_addr(hostname);
if (address == INADDR_NONE)
{
he = gethostbyname(hostname);
if (he == 0)
{
sprintf(buffer, "Webcamcgi: Failed to gethostbyname(%s)", hostname);
WriteErrorText(buffer);
return(0);
}
address = *(long *)he->h_addr;
}
servsockaddr.sin_port = htons(port);
servsockaddr.sin_family = AF_INET;
servsockaddr.sin_addr.s_addr = address;
rc = connect(servfd,
(struct sockaddr *)&servsockaddr,
sizeof(servsockaddr));
if (rc < 0)
{
return(0);
}
send(servfd, requestMessage, strlen(requestMessage), 0);
do
{
if (WaitForSocket(servfd, 20)==0)
{
break;
}
rc = recv(servfd, buffer, sizeof(buffer), 0);
if (rc > 0)
{
WriteHTML(buffer, rc);
}
} while(rc >0);
close(servfd);
return(0);
}
/* EOF */