Nearby Restaurants

Launch the server on oit2.scps.nyu.edu

The Client example was bound to port 3000 on oit2.scps.nyu.edu. This server is bound to TCP port 3001. The restaurants.db file was created in Restaurant.

#!/bin/perl
#To launch this server on oit2.scps.nyu.edu and bind it to TCP port number 3001,
#/home/m/meretzkm/public_html/INFO1-CE9705/src/near/server.pl &

use strict;
use Socket;
use IO::Socket;

#Simple HTTP server in Perl, connected to an SQLite database.
my $database_file =
	'/home/m/meretzkm/public_html/INFO1-CE9705/src/near/restaurants.db';

#Create a hash named %data containing the key/value pairs in the query string.

sub parse_form {
	my $data = $_[0];
	my %data;
	foreach (split /&/, $data) {
		my ($key, $val) = split /=/;
		$val =~ s/\+/ /g;
		$val =~ s/%(..)/chr(hex($1))/eg;
		$data{$key} = $val;
		print("   $key = $data{$key}\n");
	}
	return %data;
}

#Create a socket with which the server can listen for clients.

my $server = new IO::Socket::INET(
	Proto => 'tcp',
	LocalPort => 3001,   #TCP port number I'm allowed to use on oit2.scps.nyu.edu
	Listen => SOMAXCONN,
	Reuse => 1);

$server or die "Unable to create server socket: $!" ;

# Await requests and handle them as they arrive

while (my $client = $server->accept()) {
	my $peer_address = $client->peerhost();
	print("Server accepted a connection from client $peer_address.\n");
	$client->autoflush(1);
	my %request = ();
	my %data;
	{
		#Read the client's request.
		local $/ = Socket::CRLF;
		while (<$client>) {
			chomp;
			print("Server read the line $_\n");
			if (/\s*(GET)\s*([^\s]+)\s*HTTP\/(\d\.\d)/) {
				$request{METHOD} = uc $1;
				$request{URL} = $2;
				$request{HTTP_VERSION} = $3;
			}
			elsif (/\s*(PUT)\s*([^\s]+)\s*/) {
				$request{METHOD} = uc $1;
				$request{URL} = $2;
			}
			elsif (/:/) {
				(my $type, my $val) = split /:/, $_, 2;
				$type =~ s/^\s+//;
				foreach ($type, $val) {
					s/^\s+//;
					s/\s+$//;
				}
				$request{lc $type} = $val;
			} # POST data
			elsif (/^$/) {
				read($client, $request{CONTENT}, $request{'content-length'})
					if defined $request{'content-length'};
				last;
			}
		}
	}

	#If the URL contained a query string,
	if ($request{URL} =~ /(.*)\?(.*)/) {
		$request{URL} = $1;
		$request{CONTENT} = $2;
		%data = parse_form($request{CONTENT});
	} else {
		%data = ();
	}

	if ($request{METHOD} eq 'GET') {
		# Send Response
		print $client "HTTP/1.0 200 OK", Socket::CRLF;
		print $client "Content-type: text/plain", Socket::CRLF;
		print $client Socket::CRLF;

		my $select_statement =
			"select dba, building, street, zipcode, MIN(_id) "
			. "from restaurants where zipcode = $data{zipcode} "
			. "group by dba;";

		my $found = `sqlite3 $database_file '$select_statement'`;
		#chomp $found;
		print $client $found;
		$data{'_status'} = '200';
	}

	close $client;	#Close the server's connection to the client.
	print("\n");
}
cd /home/m/meretzkm/public_html/INFO1-CE9705/src/client
server.pl &

netstat -a -f inet -P tcp
TCP: IPv4
   Local Address        Remote Address    Swind Send-Q Rwind Recv-Q    State
-------------------- -------------------- ----- ------ ----- ------ -----------
      *.ssh                *.*                0      0 128000      0 LISTEN
      *.80                 *.*                0      0 128000      0 LISTEN
      *.*                  *.*                0      0 128000      0 IDLE
i5.ssh               cpe-66-108-91-251.nyc.res.rr.com.49727 131008      0 128872      0 ESTABLISHED
      *.3001               *.*                0      0 128000      0 LISTEN
      *.smtp               *.*                0      0 128000      0 LISTEN
      *.sunrpc             *.*                0      0 128000      0 LISTEN
      *.*                  *.*                0      0 128000      0 IDLE
      *.sunrpc             *.*                0      0 128000      0 LISTEN
      *.*                  *.*                0      0 128000      0 IDLE
      *.3306               *.*                0      0 128000      0 LISTEN
      *.5666               *.*                0      0 128000      0 LISTEN
i5.ssh               cpe-66-108-91-251.nyc.res.rr.com.49299 131008      0 128872      0 ESTABLISHED

Kill the server

jobs
[1] -  Running                 server.pl &

kill %1
jobs

Point your browser at the server

Zipcode 10007 contained the following 122 restaurants. Zipcode 10279 (just the Woolworth Building) contained one restaurant.

http://oit2.scps.nyu.edu:3001/?zipcode=10007
27 SHINJUKU SUSHI INC|27|PARK PLACE|10007|394488
AAA BURRITO MARIACHI|189|CHURCH STREET|10007|488934
AGODA ASIAN CUISINE|8         |MURRAY STREET                                     |10007|443779
AKIMOTO SUSHI|187|CHURCH STREET|10007|422594
ANDY'S DELI|291|BROADWAY|10007|408101
AROMA ESPRESSO BAR|100|CHURCH STREET|10007|375620
AROME|325 |BROADWAY|10007|454570
AROME CAFE II|7|DEY STREET|10007|281656
ATOMIC WINGS|311|BROADWAY|10007|405770
B & CO Baguettes & Company|195       |BROADWAY                                                                                            |10007|100979
BABAGHANOUSH|165       |CHURCH STREET                                     |10007|244931
BALUCHI'S|275       |GREENWICH STREET                                  |10007|106945
BANGAL CURRY|27|PARK PLACE|10007|33245
BANH MI VEITNAMESE SANDWICHES|73|WEST BROADWAY|10007|266394
BARCLAY STREET CAFE|245|GREENWICH STREET|10007|456498
BARLEYCORN|23|PARK PL|10007|495525
BARNES AND NOBLE BOOKSELLERS|97|WARREN STREET|10007|196153
BENARES|45        |MURRAY STREET                                     |10007|419289
BEST OF THE BEST DELICATESSEN|11        |PARK PLACE                                        |10007|125715
BITS BITE & BAGUETTES|22|PARK PLACE|10007|51751
BLUESPOON COFFEE|76|CHAMBERS STREET|10007|142091
BURGER KING|325 |BROADWAY|10007|480159
BY SUZETTE FRENCH CREPES|86|CHAMBERS STREET|10007|478168
CAFE|180|GREENWICH ST|10007|499122
CAFE 101|101       |BARCLAY STREET                                    |10007|436409
CAFE 101 16TH FLOOR CAFETERIA|101       |BARCLAY STREET                                    |10007|34686
CANTEEN CAFE|125|BARCLAY STREET|10007|460208
CAVIARTERIA|75|MURRAY STREET|10007|475451
CENTRE MELTS|1         |CENTRE STREET                                     |10007|442382
CHECKERS|77|CHAMBERS ST|10007|470260
CHINA RED GOURMENT|118|CHAMBERS STREET|10007|412089
CHIPOTLE MEXICAN GRILL|281|BROADWAY|10007|361179
CHURCH PUBLICK|78|READE STREET|10007|458073
CONDE NAST CONFERENCE CENTER|1|WORLD TRADE CENTER|10007|497658
CORNER GOURMET II|66        |WEST BROADWAY                                     |10007|40308
CORTE CAFE|2         |LAFAYETTE STREET                                                                                    |10007|116105
CREATIVE JUICE|54|MURRAY STREET|10007|461390
DARKHORSE|17|MURRAY STREET|10007|317235
DELIMARIE|106|DUANE STREET|10007|254175
DIRTY BIRD TO GO|155|CHAMBERS STREET|10007|404961
DOMINO'S PIZZA|181       |CHURCH STREET                                     |10007|29767
DONA BELLA  PIZZA|154       |CHURCH STREET                                     |10007|238043
DRILL FITNESS|136|CHURCH ST|10007|489422
DUNKIN DONUTS|130|CHURCH STREET|10007|229589
DUNKIN' DONUTS|5|PARK PLACE|10007|148894
DUNKIN' DONUTS, BASKIN ROBBINS|321|BROADWAY|10007|131236
ECCO RESTAURANT|124|CHAMBERS STREET|10007|453563
ELIEZER KITCHEN|100|CHURCH STREET|10007|303100
EQUINOX TRIBECA|54|MURRAY ST|10007|499481
FAMOUS FAMIGLIA PIZZA|26|MURRAY STREET|10007|489386
FIKA|52|DUANE ST|10007|498451
GEE WHIZ|295|GREENWICH STREET|10007|218579
GRAN MORSI|22|WARREN STREET|10007|475898
GRAZIN''|56|READE ST|10007|498496
HAMACHI SUSHI|63A       |READE STREET                                      |10007|448908
HARPER COLLINS|195       |BROADWAY                                                                                            |10007|486109
IL GIGLIO|81|WARREN STREET|10007|335031
IMPERIAL COFFEE HOUSE|108       |CHAMBERS STREET                                   |10007|12080
J R SUSHI|86A|W BROADWAY|10007|470564
JAMBA JUICE|88        |WEST BROADWAY                                                                                       |10007|139042
JUPIOCA INC|113|CHURCH STREET|10007|488442
Juice Press|83|MURRAY STREET|10007|486423
KAEDE JAPANESE RESTAURANT|90|CHAMBERS ST|10007|491241
KAFFE 1668|275       |GREENWICH STREET                                  |10007|225509
KITCHENETTE|156|CHAMBERS STREET|10007|159803
KOKI & VEGI|1         |CENTRE STREET                                     |10007|339755
LA BELLEZZA PIZZERIA|315|BROADWAY|10007|457597
LE PAIN QUOTIDIEN|81|WEST BROADWAY|10007|214403
LENNY'S ULTIMATE SANDWICH|25        |PARK PLACE                                                                                          |10007|439080
LILLY O'BRIENS|18|MURRAY ST|10007|471664
LITTLE ITALY PIZZA|11        |PARK PLACE                                        |10007|235979
LITTLE PARK|85|W BROADWAY|10007|490973
MAJESTIC PIZZA|8|CORTLANDT STREET|10007|52109
MANGEZ AVEC MOI|71|WEST BROADWAY|10007|60363
MANHATTAN PROPER|6         |MURRAY STREET                                     |10007|398763
MARIACHI'S RESTAURANT|61|WARREN STREET|10007|443634
MAXWELL'S|59        |READE STREET                                      |10007|340347
MCDONALD'S|317|BROADWAY|10007|363725
MILLENIUM HILTON|55|CHURCH STREET|10007|357775
MOODY'S CAFETERIA|250|GREENWICH STREET|10007|188501
MUDVILLE SALOON|126       |CHAMBERS STREET                                   |10007|6665
MULBERRY & VINE|73|WARREN STREET|10007|436696
MUSCLE MAKER GRILL|10        |MURRAY STREET                                     |10007|279867
N.Y. ACADEMY OF SCIENCE|250|GREENWICH STREET|10007|164768
NEW FRESCO TORTILLAS|63|READE STREET|10007|249619
NEW SUN CAFE|67        |READE STREET                                                                                        |10007|490185
NEW YORK DOLLS|59|MURRAY STREET|10007|85792
NEW YORK VINTNERS|21|WARREN STREET|10007|257171
NYC YOGURT|299       |BROADWAY                                                                                            |10007|445071
OLIVA GOURMET|225|BROADWAY|10007|110033
ONE WORLD OBSERVATORY|1|WORLD TRADE CTR|10007|499124
PALERMO PIZZA|61|MURRAY STREET|10007|221498
PALM RESTAURANT|200|CHAMBERS STREET|10007|223425
PLAZA FOOD COURT|1         |CENTRE STREET                                     |10007|328157
POTBELLY|280|BROADWAY|10007|377243
PRET A MANGER|319       |BROADWAY                                                                                            |10007|294995
RACCOON LODGE|59        |WARREN STREET                                     |10007|9861
RACINES|94|CHAMBERS ST|10007|475237
REMIX|24|MURRAY STREET|10007|127202
SABOR DE MEXICO|11        |PARK PLACE                                        |10007|429599
SAFFRON INDIAN CUISINE|98        |CHAMBERS STREET                                   |10007|424142
SALEYA|65        |WEST BROADWAY                                     |10007|343304
SHEEZAN RESTAURANT|183       |CHURCH STREET                                                                                       |10007|448885
SOLE DI CAPRI|165       |CHURCH STREET                                     |10007|410952
SOPHIE'S CUBAN CUISINE|96        |CHAMBERS STREET                                   |10007|317892
STAGE DOOR DELI|26        |VESEY STREET                                      |10007|19248
STARBUCKS COFFEE|291|BROADWAY|10007|52168
SUBWAY|51|MURRAY STREET|10007|263163
SUN IN BLOOM|165       |CHURCH STREET                                     |10007|490467
TAKAHACHI BAKERY|25|MURRAY STREET|10007|273174
TET TEST|22        |CORTLANDT STREET                                                                                    |10007|487845
THE COFFEE BEAN & TEA LEAF|280|BROADWAY|10007|441153
THE CRICKETERS ARMS|57        |MURRAY STREET                                     |10007|420044
THE HUMMUS & PITA CO.|79        |CHAMBERS STREET                                   |10007|435913
THE PATRIOT SALOON|110       |CHAMBERS STREET                                   |10007|106164
TRE SORELLE|61|READE STREET|10007|356491
WARREN 77|77        |WARREN STREET                                     |10007|242123
WASHINGTON MARKET TAVERN|41        |MURRAY STREET                                                                                       |10007|484867
WILMER HALE|7|WORLD TRADE CENTER|10007|400755
WOODROWS|43|MURRAY STREET|10007|374892
Yorganic|275       |GREENWICH STREET                                  |10007|313520
ZUCKER'S BAGELS AND SMOKED FISH|146|CHAMBERS STREET|10007|191115

Talk to the server via telnet

telnet oit2.scps.nyu.edu 3001
Trying 128.122.109.53...
Connected to oit2.scps.nyu.edu.
Escape character is '^]'.
GET ?zipcode=10007 HTTP/1.0   (Press Return twice.)

Output from the server

Server accepted a connection from client 66.108.91.251.
Server read the line GET /?zipcode=10007 HTTP/1.1
Server read the line Host: oit2.scps.nyu.edu:3001
Server read the line Connection: keep-alive
Server read the line Cache-Control: max-age=0
Server read the line Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Server read the line User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36
Server read the line Accept-Encoding: gzip, deflate, sdch
Server read the line Accept-Language: en-US,en;q=0.8
Server read the line
   zipcode = 10007

Server accepted a connection from client 66.108.91.251.
Server read the line GET /favicon.ico HTTP/1.1
Server read the line Host: oit2.scps.nyu.edu:3001
Server read the line Connection: keep-alive
Server read the line User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36
Server read the line Accept: */*
Server read the line Referer: http://oit2.scps.nyu.edu:3001/?zipcode=10007
Server read the line Accept-Encoding: gzip, deflate, sdch
Server read the line Accept-Language: en-US,en;q=0.8
Server read the line
Error: near "group": syntax error