*BSD News Article 43914


Return to BSD News archive

Xref: sserve comp.infosystems.www.browsers.misc:164 comp.unix.questions:64221 comp.unix.bsd.freebsd.misc:1062 comp.unix.bsd:16579 comp.lang.c:100187
Path: sserve!newshost.anu.edu.au!harbinger.cc.monash.edu.au!news.uwa.edu.au!classic.iinet.com.au!news.uoknor.edu!news.ecn.uoknor.edu!paladin.american.edu!howland.reston.ans.net!sol.ctr.columbia.edu!startide.ctr.columbia.edu!wpaul
From: wpaul@ctr.columbia.edu (Bill Paul)
Newsgroups: comp.infosystems.www.browsers.misc,comp.unix.questions,comp.unix.bsd.freebsd.misc,comp.unix.bsd,comp.lang.c
Subject: Re: Simple socket/telnet library?
Followup-To: comp.infosystems.www.browsers.misc,comp.unix.questions,comp.unix.bsd.freebsd.misc,comp.unix.bsd,comp.lang.c
Date: 11 May 1995 18:52:41 GMT
Organization: Columbia University Center for Telecommunications Research
Lines: 88
Message-ID: <3otmdp$klo@sol.ctr.columbia.edu>
References: <3osqe0$5ei@gate.sinica.edu.tw>
NNTP-Posting-Host: startide.ctr.columbia.edu
X-Newsreader: TIN [version 1.2 PL2]

Daring to challenge the will of the almighty Leviam00se, Brian Tao
(taob@gate.sinica.edu.tw) had the courage to say:

:     I would like to write an extremely simple and brain-dead Web
: browser for as part of an HTTP server benchmark.  Is there a library
: of C routines someone has already put together that allows one to open
: a connection to a host at a specific port and then send/receive text?
: My knowledge of UNIX socket and TCP/IP programming is exactly zero.  :(
: For example:

: char *gethtml(char *hostname, int port, char *request, int bytes)
: {
:     int *open_socket;
:     char *html;

:     html = (char *)(malloc(bytes));

:     if(!(open_socket=tcpopen(hostname, port)))
:     {
:         fprintf(stderr, "Could not connect to host!\n");
:         exit(1);
:     }

:     tcpwrite(open_socket, request);
:     tcpread(open_socket, html, bytes);
:     tcpclose(open_socket);
:     return(html);
: }

With a little extra work, you could just use the standard sockets
interface to do this. Go out and pick up a copy of _UNIX Network
Programming_ by W. Richard Stevens and guard it with your life.
This book should help you do what you need. It has plenty
of examples and sample source code. 

Also, you might want to pick up the sources for the ttcp utility.
This is a little gem of a program that can be used to transmit and
receive data across TCP/IP connections. I use it sometimes to
bypass my printer queues and ram data right down the throats of
my networked HP Laserjet 4si printers:

% cat somefile.ps | ttcp -t -p 9100 printer1

The -t flag means transmit. The -p option specifies the port to use
(the HP JetDirect interface listens on ports 9100 and 9099). printer1
is the hostname of the printer in question. You can also do this:

machine1% ttcp -r machine2 > somefile.ps
machine2% cat somefile.ps | ttcp -t machine1

The ttcp -r process on machine1 waits for a connection from machine2,
then copies the data it receives to somefile.ps. In effect, you
can use create pipes between two machines and shuffle data between
them just like you can do with pipes in ordinary shell commands.

It's really quite a handy program.

:     Right now I'm forking off a "lynx -source -dump" process for each
: request, which gets expensive when you have ten or more of these
: things going on one machine.  If there is a ready-made library with
: functions akin to tcpopen(), tcpwrite(), tcpread() and tcpclose() that
: works with a 4.4BSD system (FreeBSD 2.0, in my case), that would be
: fantastic.  Source code fragments, tutorial files, primers, etc. also
: gratefully accepted.  Thanks.

The Stevens book is the bible in this area. It's based on 4.3 BSD, but
that shouldn't matter for what you're doing. 

:     BTW, is it possible to create a special file that is connected to
: a host at a particular port, and be able to do something like "echo
: 'expn somealias' > special-file" and then "cat special-file" to read
: from the socket?
: -- 
: Brian ("Though this be madness, yet there is method in't") Tao
: taob@gate.sinica.edu.tw <-- work ........ play --> taob@io.org

Yes: using the ttcp program. :)

-Bill

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~T~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-Bill Paul            (212) 854-6020 | System Manager
Work:         wpaul@ctr.columbia.edu | Center for Telecommunications Research
Home:  wpaul@skynet.ctr.columbia.edu | Columbia University, New York City
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The Møøse Illuminati: ignore it and be confused, or join it and be confusing!
~~~~~~~~~ FreeBSD 2.1:  "We can kick your operating system's ass!" ~~~~~~~~~~