1: /* 2: * wserver.java 0.1 96/08/01 3: * 4: * Copyright (c) 1996 MARUYAMA Fujio All Rights Reserved. 5: * 6: * Permission to use, copy, modify, and distribute this software 7: * and its documentation for NON-COMMERCIAL purposes and without 8: * fee is hereby granted provided that this copyright notice 9: * appears in all copies. 10: * 11: * Mail : maruyama@wakhok.ac.jp 12: * 13: */ 14: 15: import java.net.*; 16: import java.io.*; 17: import java.util.*; 18: 19: class wserver { 20: 21: //String docroot = "/usr/backhome/maruyama/www"; 22: String docroot = "/usr/www/mosaic/httpd/docs"; 23: 24: String Reply = "HTTP/1.0 200 Document follows"; 25: String Mime = "MIME-Version: 1.0"; 26: String Server = "Server: Jarver/0.1"; 27: String Date = "Date: "; 28: String Type = "Content-Type: "; 29: String Length = "Content-Length: "; 30: String Modified = "Last-Modified: "; 31: 32: public String getContentType(String filename){ 33: String ext = filename.substring(filename.lastIndexOf('.')); 34: // System.out.println(filename); 35: if ( ext.equals(".html")) return "text/html" ; 36: if ( ext.equals(".htm") ) return "text/html" ; 37: if ( ext.equals(".xbm") ) return "image/x-xbitmap" ; 38: if ( ext.equals(".gif") ) return "image/gif" ; 39: if ( ext.equals(".jpeg")) return "image/jpeg" ; 40: if ( ext.equals(".jpg") ) return "image/jpeg" ; 41: if ( ext.equals(".au") ) return "audio/basic" ; 42: return "www/unknown" ; 43: } 44: 45: public void printString(String string,OutputStream os){ 46: try{ 47: for( int i=0; i< string.length() ; i++){ 48: os.write(string.charAt(i)); 49: } 50: os.write('n'); 51: // System.out.println(string); 52: } catch(Exception e){ 53: System.out.println("Exception : " + e ); 54: } 55: } 56: 57: public void Header(String path,OutputStream os){ 58: try{ 59: File file = new File(path); 60: String date = Date + new Date(System.currentTimeMillis()) ; 61: String type = Type + getContentType(path); 62: String length = Length + file.length(); 63: String modified = Modified + new Date(file.lastModified()) ; 64: 65: printString(Reply,os); 66: printString(Mime,os); 67: printString(Server,os); 68: printString(date,os); 69: printString(type,os); 70: printString(length,os); 71: printString(modified,os); 72: os.write('n'); 73: 74: } catch(Exception e){ 75: System.out.println("Exception : " + e ); 76: } 77: } 78: 79: wserver() { 80: ServerSocket serverSocket = null; 81: try { 82: serverSocket = new ServerSocket(8080); 83: } catch (Exception e) { 84: System.out.println("Exception : " + e); 85: System.exit(1); 86: } 87: 88: try { 89: while( true){ 90: Socket clientSocket = serverSocket.accept(); 91: DataInputStream is = new DataInputStream( 92: new BufferedInputStream(clientSocket.getInputStream())); 93: OutputStream os = clientSocket.getOutputStream(); 94: String inputLine, outputLine; 95: String path=null; 96: 97: while ((inputLine = is.readLine()) != null) { 98: if ( inputLine.startsWith("GET") ){ 99: int first = inputLine.indexOf(' '); 100: int second = inputLine.indexOf(' ',first+1); 101: path = docroot+inputLine.substring(first+1,second); 102: continue ; 103: } 104: if ( inputLine.equals("") ){ 105: Header(path,os); 106: BufferedInputStream fin = new BufferedInputStream( 107: new FileInputStream(path),1024); 108: int ch ; 109: while(( ch = fin.read()) != -1 ){ 110: os.write(ch); 111: } 112: break ; 113: } 114: } 115: os.close(); 116: is.close(); 117: clientSocket.close(); 118: } 119: } catch (IOException e) { 120: e.printStackTrace(); 121: } 122: } 123: 124: public static void main(String args[]) { 125: new wserver(); 126: } 127: 128: }
1: /* 2: * sender.java 0.1 96/08/01 3: * 4: * Copyright (c) 1996 MARUYAMA Fujio All Rights Reserved. 5: * 6: * Permission to use, copy, modify, and distribute this software 7: * and its documentation for NON-COMMERCIAL purposes and without 8: * fee is hereby granted provided that this copyright notice 9: * appears in all copies. 10: * 11: * Mail : maruyama@wakhok.ac.jp 12: * 13: */ 14: 15: import java.io.*; 16: import java.net.*; 17: import java.util.*; 18: 19: class Sender { 20: private int port ; 21: private InetAddress address = null ; 22: private byte buf[] = new byte[256]; 23: private DatagramSocket socket = null; 24: private DatagramPacket packet; 25: 26: Sender( String host, int port0 ){ 27: 28: DataInputStream stdin = new DataInputStream(System.in); 29: String localhost = null ; 30: boolean loop = true ; 31: 32: try { 33: address = InetAddress.getByName(host); 34: port = port0; 35: socket = new DatagramSocket(); 36: localhost = InetAddress.getLocalHost().getHostName(); 37: } catch (java.net.SocketException e) { 38: System.err.println("Could not create datagram socket."); 39: } catch (Exception e) { 40: System.err.println("Exception: " + e); 41: } 42: 43: sendString("Start19960801 "+System.getProperty("user.name")+"@"+localhost); 44: try { 45: while( loop ){ 46: System.out.print("> "); System.out.flush(); 47: String line = stdin.readLine(); 48: if ( line.equals("Quit.") ) loop=false ; 49: sendString(line); 50: } 51: socket.close(); 52: } catch (IOException e) { 53: System.err.println("IOException: " + e); 54: e.printStackTrace(); 55: } 56: } 57: 58: Sender(String host){ 59: this(host, 1996); 60: } 61: 62: public void sendString(String str){ 63: try{ 64: str.getBytes(0,str.length(),buf,0); 65: packet = new DatagramPacket(buf, str.length(), address, port); 66: socket.send(packet); 67: } catch (IOException e) { 68: System.err.println("IOException: " + e); 69: e.printStackTrace(); 70: } 71: } 72: 73: public static void main(String[] args) { 74: if (args.length != 1) { 75: System.out.println("Usage: java Sender <hostname> "); 76: return; 77: } 78: new Sender(args[0]); 79: } 80: 81: } 82:
1: /* 2: * receiver.java 0.1 96/08/01 3: * 4: * Copyright (c) 1996 MARUYAMA Fujio All Rights Reserved. 5: * 6: * Permission to use, copy, modify, and distribute this software 7: * and its documentation for NON-COMMERCIAL purposes and without 8: * fee is hereby granted provided that this copyright notice 9: * appears in all copies. 10: * 11: * Mail : maruyama@wakhok.ac.jp 12: * 13: */ 14: 15: import java.io.*; 16: import java.net.*; 17: import java.util.*; 18: 19: class Receiver extends Thread { 20: private DatagramSocket socket = null; 21: private DataInputStream qfs = null; 22: 23: Receiver() { 24: super("Receiver"); 25: try { 26: socket = new DatagramSocket(1997); 27: System.out.println("Receiver listening on port: " + socket.getLocalPort()); 28: } catch (java.net.SocketException e) { 29: System.err.println("Could not create datagram socket."); 30: } 31: } 32: 33: public void run() { 34: if (socket == null) 35: return; 36: 37: System.out.println("Receiver begin " ); 38: while (true) { 39: try { 40: byte[] buf = new byte[256]; 41: 42: DatagramPacket packet = new DatagramPacket(buf, 256); 43: socket.receive(packet); 44: String dString = new String(packet.getData(),0); 45: System.out.println(dString); 46: 47: } catch (IOException e) { 48: System.err.println("IOException: " + e); 49: e.printStackTrace(); 50: } 51: } 52: } 53: 54: protected void finalize() { 55: if (socket != null) { 56: socket.close(); 57: socket = null; 58: System.out.println("Closing datagram socket."); 59: } 60: } 61: 62: public static void main(String arg[]){ 63: new Receiver().start(); 64: } 65: } 66: 67:
1: /* 2: * chatServer.java 0.1 96/08/01 3: * 4: * Copyright (c) 1996 MARUYAMA Fujio All Rights Reserved. 5: * 6: * Permission to use, copy, modify, and distribute this software 7: * and its documentation for NON-COMMERCIAL purposes and without 8: * fee is hereby granted provided that this copyright notice 9: * appears in all copies. 10: * 11: * Mail : maruyama@wakhok.ac.jp 12: * 13: */ 14: 15: import java.io.*; 16: import java.net.*; 17: import java.util.*; 18: 19: class chatServer extends Thread { 20: private DatagramSocket socket = null; 21: private DatagramSocket send_socket = null; 22: Hashtable h = new Hashtable(); 23: 24: chatServer() { 25: super("chatServer"); 26: try { 27: socket = new DatagramSocket(1996); 28: send_socket = new DatagramSocket(); 29: } catch (java.net.SocketException e) { 30: System.err.println("Could not create datagram socket."); 31: } 32: } 33: 34: public void run() { 35: if (socket == null) 36: return; 37: 38: System.out.println("chatServer begin .... " ); 39: while (true) { 40: try { 41: byte buf[] = new byte[256]; 42: DatagramPacket packet; 43: InetAddress address; 44: int port; 45: int length; 46: String host = null; 47: String dString = null; 48: String header = ""; 49: int i,j ; 50: 51: packet = new DatagramPacket(buf, 256); 52: socket.receive(packet); 53: 54: address = packet.getAddress(); 55: host=address.getHostName() ; 56: length = packet.getLength(); 57: port = packet.getPort(); 58: 59: dString = new String(buf,0).substring(0,length); 60: if ( dString.startsWith("Start19960801 ") ){ 61: i = dString.indexOf(' '); 62: h.put(address , dString.substring(i+1)); 63: continue; 64: } 65: if ( dString.equals("Quit.") ){ 66: h.remove(address); 67: continue; 68: } 69: 70: if ( ( header=(String)h.get(address) ) == null ) 71: header="Unknown Host"; 72: header = "[" + header + "] "; 73: dString = header + dString ; 74: 75: // System.out.println(dString); 76: length=dString.length(); 77: dString.getBytes(0, length, buf, 0); 78: for( Enumeration e=h.keys(); e.hasMoreElements(); ){ 79: address= (InetAddress)e.nextElement() ; 80: packet = new DatagramPacket(buf, length, address, 1997); 81: send_socket.send(packet); 82: } 83: } catch (IOException e) { 84: System.err.println("IOException: " + e); 85: e.printStackTrace(); 86: } 87: } 88: } 89: 90: protected void finalize() { 91: if (socket != null) { 92: socket.close(); 93: socket = null; 94: System.out.println("Closing datagram socket."); 95: } 96: } 97: 98: public static void main(String arg[]){ 99: new chatServer().start(); 100: } 101: }
1: /* 2: * simpleLoader.java 0.1 96/08/01 3: * 4: * Copyright (c) 1996 MARUYAMA Fujio All Rights Reserved. 5: * 6: * Permission to use, copy, modify, and distribute this software 7: * and its documentation for NON-COMMERCIAL purposes and without 8: * fee is hereby granted provided that this copyright notice 9: * appears in all copies. 10: * 11: * Mail : maruyama@wakhok.ac.jp 12: * 13: */ 14: 15: import java.util.*; 16: import java.io.*; 17: import java.net.*; 18: 19: class simpleLoader extends ClassLoader { 20: String base = null ; 21: URL url = null ; 22: 23: simpleLoader(String str){ 24: base = str ; 25: } 26: 27: private Class loadNetClass( String name ) throws IOException { 28: InputStream in = null; 29: ByteArrayOutputStream out = new ByteArrayOutputStream(1024); 30: try { 31: url = new URL( base + "/" + name + ".class"); 32: URLConnection c = url.openConnection(); 33: in = c.getInputStream(); 34: 35: byte buff[] = new byte[1024]; 36: int n,m; 37: 38: int len=0; 39: while ((n = in.read(buff, 0 , 1024)) >= 0) { 40: out.write(buff,0,n); 41: len += n ; 42: } 43: System.out.println("Length: "+len); 44: 45: byte data[] = new byte[len]; 46: data=out.toByteArray(); 47: return defineClass(data, 0, len); 48: 49: } catch (IOException e) { 50: e.printStackTrace(); 51: throw e; 52: } catch (Throwable e) { 53: e.printStackTrace(); 54: throw new IOException("class not loaded: " + url); 55: } 56: } 57: 58: public Class loadClass(String name) throws ClassNotFoundException { 59: return loadClass(name, true); 60: } 61: 62: protected Class loadClass(String name, boolean resolve) 63: throws ClassNotFoundException { 64: Class cl = null ; 65: try { 66: return findSystemClass(name); 67: } catch (Throwable e) {} 68: try { 69: loadNetClass(name); 70: if (resolve) { 71: resolveClass(cl); 72: } 73: } catch( Exception e){ 74: System.out.println(e); 75: } 76: return cl; 77: } 78: 79: }
1: /* 2: * loadTest.java 0.1 96/08/01 3: * 4: * Copyright (c) 1996 MARUYAMA Fujio All Rights Reserved. 5: * 6: * Permission to use, copy, modify, and distribute this software 7: * and its documentation for NON-COMMERCIAL purposes and without 8: * fee is hereby granted provided that this copyright notice 9: * appears in all copies. 10: * 11: * Mail : maruyama@wakhok.ac.jp 12: * 13: */ 14: 15: import java.net.*; 16: 17: class loadTest { 18: public static void main(String args[]){ 19: Class sc = null ; 20: Object sample = null; 21: try { 22: 23: //String base="http://ews1:8080"; 24: String base="http://www.wakhok.ac.jp/~maruyama"; 25: 26: simpleLoader loader = new simpleLoader(base); 27: sc = loader.loadClass(args[0]); 28: 29: sample = sc.newInstance(); 30: 31: } catch ( Exception e){ 32: e.printStackTrace(); 33: System.out.println("Instance: " + e ) ; 34: } 35: } 36: }