[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] MacPerl, sockets, and servers



Recently, someone posted some stuff about connecting to a
web site via sockets.  I liked the idea and the code looked
fairly simple so I downloaded it.  I'm glad I did because
I'm now using a similar script in a MacPerl program which
will eventually be moved to a Unix box and IBMs.

Basically, the program opens a file and then sends it as
hex to the web server so I can move 3D Models, Structures,
and Textures from anyone's machine to our machine.  We are
basically trying to build a database of information which
anyone will be able to use and all of the items in the
database will be free.  The only drawback that I know of is
that instead of using something like DXF, we are going to
be storing the files as Enigma files (the 3D modelling
program we've built here).  The reason I say drawback is
simply because there aren't many people out there who know
about Enigma so the number of applications which can read
the files is rather limited.  :-/

The problem I've come across is this:

I can not seem to be able to send more than about 80 bytes
at a time across the line.  These 80 bytes (of course) when
printed as hex values become 160 bytes of character data.
I've tried 9600 bytes (19200 characters), 4800 bytes, 2400
bytes, 1200 bytes, 600 bytes, etc... down to 80 bytes and
that's where it's stayed.  Since some of the model files
are over 10mb of information - I just think this will take
a bit too long to transfer.  Ya' know?  ;-)

Here is the code.  All of my testing is being done on a Mac
IIfx with 32mb of memory.  The system crashed continuously
every time I attempted to send the information until I'd
reached the 80b level.  Since I'm running Quid Pro Quo I
checked the speedometer and found I had it set to the
slowest speed.  So I've put it at the highest speed and am
going to be testing it again today.  It might turn out that
the only problem is with QPQ opening and closing sockets
fast enough.  But I don't want to take a program over to
the SGIs which crashes the Macs since crashing a Mac is one
thing and crashing an SGI is another.  They are so delicate
in some ways and this is one of them.  I think the sysop
would strangle me if I started making her system die every
five minutes.  :-/.

PS: Ignore/cut out the junk at the beginning.  I'm working
on what the header should look like and this is just what's
there now.  :-)

#
#------------------------------------------------------------
#
#!/usr/local/bin/perl

	use Socket;

	$LENGTH_OF_LINES = 80;
	$NUMBER_OF_LINES = 1;
	$BUFFER_SIZE = $LENGTH_OF_LINES * $NUMBER_OF_LINES;
#
#	Note: I have removed our socket address because you can not
#	reach the Macintosh via the net.  It is on a subnet.  This
#	means you will have to have a web server of your own in
#	order to try this out.  Sorry!
#
	$theServer = 'x.x.x.x' || shift;
	$thePort   = 80 || shift;

	system( "clear" );
	print <<END_OF_LINE;



#     # #       ######  ######
##   ## #       #     # #     #
# # # # #       #     # #     #
#  #  # #       #     # ######
#     # #       #     # #     #
#     # #       #     # #     #
#     # ####### ######  ######
The Model Library Database (MLDB) program version 1.0a.


Welcome to the file transfer program for the Model Library
Database (MLDB) Program.  This program will move your
models, structures, or texture files from your machine to
our machine.  There are just a few things to remember when
using it:

1.	Please do NOT start to transfer a file and then
	stop transfering it.  This can cause all sorts of problems
	both on your end as well as our end of this.

2.	Be sure to set aside enough time for the transfer
	to complete.  This can take as long as three or four hours
	depending upon just how large of a file you are attempting
	to transfer.  The reason is the file has to be chopped up
	into 8, 16, or 32K segments (depending upon what you set
	the \$NUMBER_OF_LINES and \$LENGTH_OF_LINES variables to.
	Presently, they are set at $NUMBER_OF_LINES and
	$LENGTH_OF_LINES respectively.

Some of the information we will need in order to process
your file at the MLDB is:

1.	Your username as you typed it into the MLDB.
2.	Your password as you typed it into the MLDB.
3.	The type of file you are transferring.

	M = Model
	S = Structure
	T = Texture

4.	The name of the file as you input it into the MLDB.
5.	The full filename of the file to transfer.
	(ie: The directory path AND the filename so the
	program can open up the file and read it.)
6.	The name of a still picture file (GIF or JPEG).
7.	The name of a movie (GIF or MPEG).


After this, the program will begin the transfer.  In order
that you might see where the program is during the transfer
it will print out to the screen what the web site is
receiving.  Good luck!

END_OF_LINE

	print "What is your username on the MLDB web page? ";
	$userName = <STDIN>;
	chop( $userName );

	print "What is your password on the MLDB web page? ";
	$passWord = <STDIN>;
	chop( $passWord );

	print "What type of file are you transferring?\n\n";
	print "M = Model\n";
	print "S = Structure\n";
	print "T = Texture\n\n";
	print "Which? ";
	$fileType = <STDIN>;
	chop( $fileType );

	print "\n\nWhat was the name you gave the file in the MLDB? ";
	$fileName = <STDIN>;
	chop( $fileName );

	print "\n\nWhere does the file reside on your system? ";
	$thePath = <STDIN>;
	chop( $thePath );

	print "\n\nIf you have a still picture of the file, enter\n";
	print "where the file is so located it can be transferred: ";
	$pictureFile = <STDIN>;
	chop( $pictureFile );

	print "\n\nIf you have a movie to go with this file, enter\n";
	print "where the file is so located it can be transferred: ";
	$movieFile = <STDIN>;
	chop( $movieFile );

	print <<END_OF_LINE;


Ok, I have the following:


	Username = $userName
	Password = $passWord
	File Type = $fileType
	Filename in the MLDB = $fileName
	Path to the file on your system = $thePath
	Still Picture File location = $pictureFile
	Movie file location = $movieFile



END_OF_LINE

	print "Is this correct? ";
	$theAnswer = <STDIN>;
	chop( $theAnswer );

	if( $theAnswer =~ /n/i ){
		print "***** Sorry, but you will have to try this again.\n";
		print "***** MLDB.PL aborted.\n";
		exit( 0 );
		}
#
#	Open the file and transfer it.
#
#	As per the Perl documentation - zero (0) means read only.  Which is what we
#	want.  So instead of using O_READ, we are using the numerical value so there
#	can be no doubt as to what we want to do.
#
	print "\n\nOpening the file...please wait.\n";
	sysopen( THEFILE, $thePath, O_READ ) || die "Couldn't open $thePath - aborting.\n";
#
#	Begin looping through the file reading and sending things.
#
	print "\n\nTransferring the file...please wait.\n";

	$| = 1;
	$startLoc = 0;
	while( ($bufLength = sysread(THEFILE, $theBuffer, $BUFFER_SIZE)) > 0 ){
		$bufLength = sysread(THEFILE, $theBuffer, $BUFFER_SIZE);
		$theURL = "/mldb/test.cgi?&username=$userName&password=$passWord";
		$theURL .= "&filetype=$fileType&filename=$fileName";
		$theURL .= "&start=$startLoc&data=";

		for( $i=0; $i<$bufLength; $i++ ){
			$theByte = substr( $theBuffer, $i, 1 );
			$theURL .= sprintf( "%.2x", ord($theByte) );
			}
#
#	Code from Paul Schnider on the MacPerl list.
#
		socket  (SOCKET, PF_INET, SOCK_STREAM, (getprotobyname('tcp'))[2]);
		connect (SOCKET, pack('Sna4x8', AF_INET, $thePort, (gethostbyname($theServer))[4]))
		   || die "Can't connect to server $theServer on port $thePort.\n";

		select(SOCKET);

		print SOCKET "GET $theURL\012\012";
		while (<SOCKET>) {
			s/\012/\n/g;
			print STDOUT;
			}

		close SOCKET;

		$startLoc += $bufLength;
		}

	close( THEFILE );
	exit( 0 );
#
#------------------------------------------------------------
#