Back to 10/96 How To: Programming Windows
Up to Table of Contents
Ahead to 10/96 Cover Story: HOT STUFF!

10/96 How To: Programming Windows

Client Cleanup

Here's How to Port the Unix version of our client software to a Win32 version.


Figure 1: UNIX Version of Client

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

main(argc,argv)
int argc;
char **argv;
{
int s;
int len;
struct sockaddr_in sa;
struct hostent *hp;
struct servent *sp;
char buf[BUFSIZ];
char *myname;
char host[60]=
char *document;
char docstr[256];
char *adposition;
char *pubcode;
char *placement;
myname = argv[0];
if(argc != 4){
fprintf(stderr,"Usage: %s # PUB Place\n",myname);
exit(1);
}
sprintf(host, "test.site.net");
adposition = argv[1];
pubcode = argv[2];
placement = argv[3];

if((hp=gethostbyname(host)) == NULL){
fprintf(stderr,
"%s: %s: no such host?\n",myname,host);
exit(1);
}
bcopy((char *)hp->h_addr,
(char *)&sa.sin_addr,hp->h_length);
sa.sin_family = hp->h_addrtype;
if((sp=getservbyname("telnet","tcp")) == NULL){
fprintf(stderr,
"%s: No whois service on this host\n",myname);
exit(1);
}
sa.sin_port = sp->s_port;
if((s=socket(hp->h_addrtype,SOCK_STREAM,0)) < 0){
perror("socket");
exit(1);
}
if(connect(s,(const SOCKADDR *)&sa,(sizeof sa)) < 0){
perror("connect");
exit(1);
}
sprintf(docstr,
"%s %s %s\n",adposition,pubcode,placement);
if(write(s,docstr,strlen(docstr)) != strlen(docstr))
{
fprintf(stderr,"%s: write error\n",myname);
exit(1);
}
while((len = read(s,buf,BUFSIZ)) > 0){
buf[len] = '\0';
fprintf(stdout,"%s",buf);
}
fprintf(stdout,"\n");
close(s);
return(0);
}

Figure 2: Optimized WinSock Version of Client #include "stdio.h"
#include <winsock.h>
#define LookupByName 0
#ifndef ServerName
#define ServerName "srvr"
#endif
#ifndef ServerIP
#define ServerIP 0xcad9a103
#endif
#ifdef _DEBUG
#define Port 8502
#else
#define Port 8501
#endif
#define BufSize 1024
int main(int argc, char *argv[])
{
SOCKET sock;
struct hostent *host;
WORD wVersionRequested;
WSADATA wsaData;
int err;
struct sockaddr_in addr;
char buffer[BufSize];
int gotSome;
if (argc != 4){
printf("Usage: adClient position# pubName pageName\n");
exit(-1);
}
wVersionRequested = MAKEWORD(1,1);
if (err = WSAStartup(wVersionRequested, &wsaData)){
printf("Could not start winsocks\n");
exit(-1);
}
if ((LOBYTE(wsaData.wVersion) != 1) || (HIBYTE(wsaData.wVersion) != 1)){
printf("Wrong version of winsock loaded.\n");
WSACleanup();
exit(-1);
}
#if LookupByName
host = gethostbyname(ServerName);
if (!host){
printf ("Could not find host %s because of %d\n", ServerName,
WSAGetLastError());
WSACleanup();
exit(-1);
}
#endif
if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET){
printf("Could not open socket because of %d\n",
WSAGetLastError());
WSACleanup();
exit(-1);
}
memset((char *)&addr,0,sizeof(struct sockaddr_in));
addr.sin_family=AF_INET;
addr.sin_port=htons((u_short)Port);

#if LookupByName
memcpy((char *)&addr.sin_addr,host->h_addr,
host->h_length);
#else
addr.sin_addr.s_addr = htonl(ServerIP);
#endif

if (connect(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))){
printf("Could not connect on socket because of %x\n", WSAGetLastError());
WSACleanup();
exit(-1);
}
sprintf(buffer, "%s %s %s\n", argv[1], argv[2], argv[3],
getenv("DOCUMENT_URI"), getenv("REMOTE_ADDR"));
if (send(sock, buffer, strlen(buffer), 0) != SOCKET_ERROR){
int length;
gotSome = 0;
while (((length = recv(sock, buffer, BufSize, 0)) !=
SOCKET_ERROR) && ((length > 0) || ((length == 0) &&
!gotSome))){
if (length > 0){
gotSome = 1;
buffer[length] = 0;
printf("%s", buffer);
}
}
}
else{
printf("Could not send because of %d\n",
WSAGetLastError());
}
WSACleanup();
return 0;
}

Back to 10/96 How To: Programming Windows
Up to Table of Contents
Ahead to 10/96 Cover Story: HOT STUFF!