| author | plurSKI <black.gavin@gmail.com> | 2010-10-16 14:12:30 (GMT) |
|---|---|---|
| committer | plurSKI <black.gavin@gmail.com> | 2010-10-16 14:12:30 (GMT) |
| commit | 7a0ae62426c5660121bc544d008764870bd4b558 (patch) | |
| tree | 684d584fbe87b11f8ae29665408a7363205406cb | |
| download | javaNetwork-master.zip javaNetwork-master.tar.gz | |
Initial Checkinmaster
| -rw-r--r-- | WebServer.java | 142 | ||||
| -rw-r--r-- | fileTransClient.java | 140 | ||||
| -rw-r--r-- | fileTransServer.java | 134 | ||||
| -rw-r--r-- | makefile | 13 |
4 files changed, 429 insertions, 0 deletions
diff --git a/WebServer.java b/WebServer.java new file mode 100644 index 0000000..1978c40 --- a/dev/null +++ b/WebServer.java @@ -0,0 +1,142 @@ +// Small Java Based Webserver, ideal for local GUIS -- Gavin S. Black
+// GNU GPL
+
+
+import java.io.*;
+import java.net.*;
+
+class WebServe extends Thread {
+
+ private Socket socket;
+ private BufferedReader in;
+ private PrintWriter out;
+ private int timesAccessed;
+ private static String text = "";
+
+ public WebServe (Socket s, int tA)
+ throws IOException {
+ socket = s;
+ timesAccessed = tA;
+ in =
+ new BufferedReader(
+ new InputStreamReader(
+ socket.getInputStream()));
+ out =
+ new PrintWriter(
+ new BufferedWriter(
+ new OutputStreamWriter(
+ socket.getOutputStream())), true);
+ start();
+ }
+
+ // Thread to actually run content delivery
+ // <in> is being used to read in GET variables
+ // <out> is being passed raw HTML to display to the user
+ public void run() {
+ int count = 0;
+ boolean newText = false;
+ String newestText = "";
+ String newestTextL = "";
+ String imgText = "";
+
+ try {
+ while (count < 10)
+ {
+ String str = in.readLine();
+ if(str.indexOf("GET") >= 0)
+ if(str.indexOf("?inText=") >=0) {
+ newestText = str.substring(str.indexOf("=") + 1, str.indexOf(" ", str.indexOf("=")));
+ newestTextL = newestText.toLowerCase();
+ if(newestTextL.indexOf("murder") >=0){
+ out.println("<TITLE>Server shutdown</TITLE><H1>Goodbye</H1>");
+ System.exit(0);
+ }
+ imgText = " <img alt=\".\" src=\"http://www.pdictionary.com/images/" + newestTextL.charAt(0) + "/" + newestTextL + ".gif\">";
+ text = newestText + imgText + "<br>" + text;
+
+ newText = true;
+ }
+ if (str== null ) break;
+ count ++;
+ }
+
+ // Print out test web page
+ out.println("<TITLE>You have accessed this page " + timesAccessed + " times!</Title>");
+ out.println("<form>Your text: <input type=\"text\" name=\"inText\"></form>");
+ out.println(text);
+
+ } catch (IOException e) {
+ } finally {
+ try {
+ socket.close();
+ } catch(IOException e) {}
+ }
+ }
+}
+
+public class WebServer {
+ static int PORT=80; // assign to default http port
+ public static void main(String[] args) throws IOException {
+ if (args.length == 1) PORT=Integer.parseInt(args[0]);
+ int timesAccessed = 0;
+
+ ServerSocket s = new ServerSocket(PORT);
+ InetAddress addrs= InetAddress.getLocalHost();
+
+ System.out.println("TCP/Server running on : "+ addrs +" ,Port "+s.getLocalPort());
+
+ try {
+ while(true) {
+ Socket socket = s.accept();
+ try {
+ new WebServe(socket, ++ timesAccessed); // Handle the incoming Client.
+ } catch(IOException e) {
+ socket.close();
+ }
+ }
+ } finally {
+ s.close();
+ }
+ }
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/fileTransClient.java b/fileTransClient.java new file mode 100644 index 0000000..e7d1a6c --- a/dev/null +++ b/fileTransClient.java @@ -0,0 +1,140 @@ +import java.net.*;
+import java.io.*;
+
+public class fileTransClient{
+ // Convert an integer to an array of bytes
+ public static final byte[] intToByteArray(int value)
+ {
+ return new byte[]{ (byte)(value >>> 24), (byte)(value >> 16 & 0xff), (byte)(value >> 8 & 0xff), (byte)(value & 0xff) };
+ }
+
+ public static void main(String[] args) throws IOException
+ {
+ if (args.length != 3 && args.length != 4)
+ {
+ System.out.println("Usage: java ftpServer hostid port# fileName [transferName]");
+ System.exit(0);
+ }
+
+ String fileName = args[2];
+ String transferName;
+ if(args.length == 3)
+ transferName = fileName;
+ else
+ transferName = args[3];
+
+ try
+ {
+ InetAddress addr =
+ InetAddress.getByName(args[0]);
+ Socket socket = new Socket(addr, Integer.parseInt(args[1]));
+
+ try
+ {
+ System.out.println("socket = " + socket);
+ BufferedReader in =
+ new BufferedReader(
+ new InputStreamReader(
+ socket.getInputStream()));
+ OutputStream out = socket.getOutputStream();
+
+ // read from keyboard input
+ DataInputStream myinput =
+ new DataInputStream(
+ new BufferedInputStream(System.in));
+ try
+ {
+ FileInputStream fIn = new FileInputStream(fileName);
+ byte[] nameBytes = new byte[255];
+ int sizeOfLastPart;
+ byte[] b = new byte[1024];
+
+ // First send out filename length and data length
+ out.write(intToByteArray(transferName.length()));
+ out.flush();
+
+ // Get the file's size and calculate the number of blocks
+ File f = new File(fileName);
+ int fileSizeInKB = (int)(f.length() / 1024);
+ sizeOfLastPart = (int)(f.length() - (fileSizeInKB)*1024);
+
+ // Send out the number of 1kB blocks
+ // and then the number of bytes for the last black
+ out.write(intToByteArray(fileSizeInKB));
+ out.flush();
+ out.write(intToByteArray(sizeOfLastPart));
+ out.flush();
+
+ System.out.println("File Length: " +
+ (float)f.length() / 1024 + " KB");
+ nameBytes = transferName.getBytes();
+ out.write(nameBytes);
+ out.flush();
+
+ // Write it out in 1kb blocks
+ int i;
+ System.out.print( "\nSending: ");
+ for(i = 0; i < fileSizeInKB; i ++ )
+ {
+ fIn.read(b); // Read 1kB
+ out.write(b);
+ out.flush();
+
+ // Print out the percentage completed
+ System.out.print(i*100/fileSizeInKB + "%");
+ System.out.flush();
+ if(i*100/fileSizeInKB < 10)
+ System.out.print("\b\b");
+ else
+ System.out.print("\b\b\b");
+ in.readLine(); // Receive ack
+ }
+
+ // Output the end of the data if needed
+ byte bEnd[] = new byte[sizeOfLastPart];
+ if(sizeOfLastPart > 0)
+ {
+ fIn.read(bEnd);
+ out.write(bEnd, 0, sizeOfLastPart);
+ out.flush();
+ }
+ String strout = in.readLine(); // Receive last ack
+ System.out.println( "100%");
+ }catch (IOException e){
+ e.printStackTrace();
+ }catch (Exception e){
+ e.printStackTrace();
+ }
+ } finally {
+ socket.close();
+ }
+ } catch(UnknownHostException e){
+ System.err.println("Can't find host");
+ System.exit(1);
+ } catch (SocketException e){
+ System.err.println("Can't open socket");
+ e.printStackTrace();
+ System.exit(1);
+ }
+ }
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/fileTransServer.java b/fileTransServer.java new file mode 100644 index 0000000..88aa410 --- a/dev/null +++ b/fileTransServer.java @@ -0,0 +1,134 @@ +import java.io.*;
+import java.net.*;
+
+class InetServe extends Thread {
+ private Socket socket;
+ private InputStream in;
+ private PrintWriter out;
+ private int timesAccessed;
+
+ public InetServe (Socket s, int tA) throws IOException
+ {
+ socket = s;
+ timesAccessed = tA;
+ System.out.println("Serving: "+socket);
+ in = socket.getInputStream();
+ out = new PrintWriter(
+ new BufferedWriter(
+ new OutputStreamWriter(
+ socket.getOutputStream())), true);
+ start(); // Start the thread, calls run()
+ }
+
+ // Convert an array to an integer
+ public static final int byteArrayToInt(byte[] b)
+ {
+ return (int) ( ((b[0] & 0xFF) << 24) +
+ ((b[1] & 0xFF) << 16) +
+ ((b[2] & 0xFF) << 8) +
+ ( b[3] & 0xFF) );
+ }
+
+ public void run()
+ {
+ byte[] b = new byte[1024]; // 1 KB block
+ byte[] bInt = new byte[4]; // Integer in bytes
+ String fileName;
+ int fileNameSize=0;
+ int fileSize=0;
+ int fileSizeInKB=0;
+ int sizeOfLastPart=0;
+ try
+ {
+ // Get length of filename
+ in.read(bInt);
+ fileNameSize = byteArrayToInt(bInt);
+
+ // Get length of file
+ in.read(bInt);
+ fileSizeInKB = byteArrayToInt(bInt);
+ in.read(bInt);
+ sizeOfLastPart = byteArrayToInt(bInt);
+
+ // Read in filename
+ in.read(b,0,fileNameSize);
+ fileName = new String(b);
+ FileOutputStream fOut = new FileOutputStream(fileName);
+ System.out.println("File Name: " + fileName);
+ System.out.println("File Size: " +
+ (float)(fileSizeInKB*1024 + sizeOfLastPart)/1024 + " kB");
+
+ // Read in the data in 1 kB blocks
+ System.out.print( "\nReceiving: ");
+ for(int i = 0; i < fileSizeInKB; i ++)
+ {
+ in.read(b);
+ fOut.write(b); // Write data to output file
+
+ // Display the current percentage
+ System.out.print(i*100/fileSizeInKB + "%");
+ System.out.flush();
+ if(i*100/fileSizeInKB < 10)
+ System.out.print("\b\b");
+ else
+ System.out.print("\b\b\b");
+ out.println("Okay");
+ }
+
+ // Output the end of the file if needed
+ if(sizeOfLastPart > 0)
+ {
+ byte[] bEnd = new byte[sizeOfLastPart];
+ in.read(bEnd);
+ fOut.write(bEnd);
+ }
+ out.println("Okay");
+ System.out.println( "100%" );
+ fOut.close();
+ }catch (Exception e){
+ System.out.println("Caught unexpected exception\n");
+ } finally {
+ try
+ {
+ socket.close();
+ } catch(IOException e) {
+ System.out.println("Caught unexpected exception\n");
+ }
+ }
+ }
+}
+
+public class fileTransServer
+{
+ static int PORT=0; // assign to next avalible Port.
+ public static void main(String[] args) throws IOException
+ {
+ if (args.length == 1)
+ {
+ PORT=Integer.parseInt(args[0]); // assign to a given Port.
+ }
+ int timesAccessed = 0;
+
+ // Create a Server Socket.
+ ServerSocket s = new ServerSocket(PORT);
+ InetAddress addrs= InetAddress.getLocalHost();
+ System.out.println("Server running on : "+ addrs +
+ " ,Port "+s.getLocalPort());
+
+ try
+ {
+ while(true)
+ {
+ Socket socket = s.accept(); // Note: This is a blocking call
+ try
+ {
+ new InetServe(socket, ++ timesAccessed); // Handle incoming client
+ } catch(IOException e) {
+ socket.close();
+ }
+ }
+ } finally {
+ s.close();
+ }
+ }
+}
diff --git a/makefile b/makefile new file mode 100644 index 0000000..c395f7b --- a/dev/null +++ b/makefile @@ -0,0 +1,13 @@ +all: fileTransServer fileTransClient WebServer + +WebServer: WebServer.java + javac WebServer.java + +fileTransServer: fileTransServer.java + javac fileTransServer.java + +fileTransClient: fileTransClient.java + javac fileTransClient.java + +clean: + rm *class |
