Client talks to a server

The SQLite database is on the Solaris Unis server oit2.scps.nyu.edu. The Android app is a client. When the app wants to read or write a record, it talks to a Perl program on the server.

Source code in Client.zip

  1. MainActivity.java
  2. activity_main.xml consists of a RelativeLayout containing a ScrollView containing a TextView. The ScrollView lets the TextView scroll.
  3. strings.xml
  4. AndroidManifest.xml has uses-permission INTERNET.
  5. build.gradle (Module: app).

Create the database on the server

cd /home/m/meretzkm/public_html/INFO1-CE9705/src/client
pwd
sqlite3 presidents.db
sqlite> create table presidents (
	_id integer primary key autoincrement,
	lastname text
);
sqlite> .schema

sqlite> insert into presidents (lastname) values ('Washington');
sqlite> insert into presidents (lastname) values ('Adams');
sqlite> insert into presidents (lastname) values ('Jefferson');
sqlite> .dump
sqlite> .quit

chmod 600 presidents.db
ls -l presidents.db

Launch the server

The server is bound to TCP port 3000.

#!/bin/perl
#To launch this server on oit2.scps.nyu.edu and bind it to TCP port number 3000,
#/home/m/meretzkm/public_html/INFO1-CE9705/src/client/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/client/presidents.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 => 3000,   #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 lastname from presidents where _id = $data{_id};";
		my $lastname = `sqlite3 $database_file '$select_statement'`;
		chomp $lastname;
		print $client $lastname;
		$data{'_status'} = '200';
	}

	elsif ($request{METHOD} eq 'PUT') {
		my $update_statement =
			"update presidents set lastname = \"$data{lastname}\" where _id = $data{_id};";
		system("sqlite3 $database_file '$update_statement'");
	}

	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
-------------------- -------------------- ----- ------ ----- ------ -----------
i5.ssh               ool-182f45a8.dyn.optonline.net.37059 262140      0 128480      0 ESTABLISHED
i5.ssh               1172-08-OSX.FILMTV.TSOA.NYU.EDU.49414 131008      0 128872      0 ESTABLISHED
      *.ssh                *.*                0      0 128000      0 LISTEN
i5.ssh               cpe-66-108-91-251.nyc.res.rr.com.64972 131008      0 128872      0 ESTABLISHED
      *.smtp               *.*                0      0 128000      0 LISTEN
      *.80                 *.*                0      0 128000      0 LISTEN
      *.*                  *.*                0      0 128000      0 IDLE
      *.sunrpc             *.*                0      0 128000      0 LISTEN
      *.*                  *.*                0      0 128000      0 IDLE
      *.sunrpc             *.*                0      0 128000      0 LISTEN
      *.*                  *.*                0      0 128000      0 IDLE
i5.ssh               cpe-66-108-91-251.nyc.res.rr.com.64799 131008      0 128872      0 ESTABLISHED
      *.3000               *.*                0      0 128000      0 LISTEN
i5.80                RECON.ES.ITS.NYU.EDU.45847 28288      0 128592      0 TIME_WAIT
      *.3306               *.*                0      0 128000      0 LISTEN
      *.5666               *.*                0      0 128000      0 LISTEN

Kill the server

jobs
[1] -  Running                 server.pl &

kill %1
jobs

Point your browser at the server

http://oit2.scps.nyu.edu:3000/?_id=3
Jefferson

Talk to the server via telnet

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

JeffersonConnection closed by foreign host.
telnet oit2.scps.nyu.edu 3000
Trying 128.122.109.53...
Connected to oit2.scps.nyu.edu.
Escape character is '^]'.
PUT ?_id=3&lastname=Geoffrey 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 /?_id=1 HTTP/1.1
Server read the line User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.4; Samsung Galaxy S5 - 4.4.4 - API 19 - 1080x1920 Build/KTU84P)
Server read the line Host: oit2.scps.nyu.edu:3000
Server read the line Connection: Keep-Alive
Server read the line Accept-Encoding: gzip
Server read the line
   _id = 1

Server accepted a connection from client 66.108.91.251.
Server read the line GET /?_id=2 HTTP/1.1
Server read the line User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.4; Samsung Galaxy S5 - 4.4.4 - API 19 - 1080x1920 Build/KTU84P)
Server read the line Host: oit2.scps.nyu.edu:3000
Server read the line Connection: Keep-Alive
Server read the line Accept-Encoding: gzip
Server read the line
   _id = 2

Server accepted a connection from client 66.108.91.251.
Server read the line GET /?_id=3 HTTP/1.1
Server read the line User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.4; Samsung Galaxy S5 - 4.4.4 - API 19 - 1080x1920 Build/KTU84P)
Server read the line Host: oit2.scps.nyu.edu:3000
Server read the line Connection: Keep-Alive
Server read the line Accept-Encoding: gzip
Server read the line
   _id = 3

Server accepted a connection from client 66.108.91.251.
Server read the line PUT /?_id=3&lastname=Jeff HTTP/1.1
Server read the line User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.4; Samsung Galaxy S5 - 4.4.4 - API 19 - 1080x1920 Build/KTU84P)
Server read the line Host: oit2.scps.nyu.edu:3000
Server read the line Connection: Keep-Alive
Server read the line Accept-Encoding: gzip
Server read the line Content-Type: application/x-www-form-urlencoded
Server read the line Content-Length: 0
Server read the line
   _id = 3
   lastname = Jeff

Server accepted a connection from client 66.108.91.251.
Server read the line GET /?_id=1 HTTP/1.1
Server read the line User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.4; Samsung Galaxy S5 - 4.4.4 - API 19 - 1080x1920 Build/KTU84P)
Server read the line Host: oit2.scps.nyu.edu:3000
Server read the line Connection: Keep-Alive
Server read the line Accept-Encoding: gzip
Server read the line
   _id = 1

Server accepted a connection from client 66.108.91.251.
Server read the line GET /?_id=2 HTTP/1.1
Server read the line User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.4; Samsung Galaxy S5 - 4.4.4 - API 19 - 1080x1920 Build/KTU84P)
Server read the line Host: oit2.scps.nyu.edu:3000
Server read the line Connection: Keep-Alive
Server read the line Accept-Encoding: gzip
Server read the line
   _id = 2

Server accepted a connection from client 66.108.91.251.
Server read the line GET /?_id=3 HTTP/1.1
Server read the line User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.4; Samsung Galaxy S5 - 4.4.4 - API 19 - 1080x1920 Build/KTU84P)
Server read the line Host: oit2.scps.nyu.edu:3000
Server read the line Connection: Keep-Alive
Server read the line Accept-Encoding: gzip
Server read the line
   _id = 3