Networking 1
URL Kullanımı Get URL URL info 2
import java.io.*; import java.net.*; public class GetURL { public static void main(string[] args) { InputStream in = null; OutputStream out = null; // Check the arguments if ((args.length!= 1) && (args.length!= 2)) throw new IllegalArgumentException("Wrong number of arguments"); URL url = new URL(args[0]); // Create the URL in = url.openstream(); // Open a stream to it if (args.length == 2) // Get an appropriate output stream out = new FileOutputStream(args[1]); else out = System.out; byte[] buffer = new byte[4096]; int bytes_read; while((bytes_read = in.read(buffer))!= 1) out.write(buffer, 0, bytes_read); catch (Exception e) { System.err.println(e); System.err.println("Usage: java GetURL <URL> [<filename>]"); finally { // Always close the streams, no matter what. in.close(); out.close(); catch (Exception e) { 3
import java.net.*; import java.io.*; import java.util.date; public class GetURLInfo { public static void printinfo(url url) throws IOException { URLConnection c = url.openconnection(); // Get URLConnection from the URL c.connect(); // Open a connection to the URL System.out.println(" Content Type: " + c.getcontenttype()); System.out.println(" Content Encoding: " + c.getcontentencoding()); System.out.println(" Content Length: " + c.getcontentlength()); g System.out.println(" Date: " + new Date(c.getDate())); System.out.println(" Last Modified: " + new Date(c.getLastModified())); System.out.println(" Expiration: " + new Date(c.getExpiration())); if (c instanceof HttpURLConnection) { HttpURLConnection h = (HttpURLConnection) c; System.out.println(" Request Method: " + h.getrequestmethod()); System.out.println(" Response Message: " + h.getresponsemessage()); g System.out.println(" Response Code: " + h.getresponsecode()); information about it. */ public static void main(string[] args) { printinfo(new URL(args[0])); catch (Exception e) { System.err.println(e); System.err.println("Usage: java GetURLInfo <url>"); 4
E posta Gönderme 5
import java.io.*; import java.net.*; public class SendMail { public static void main(string[] args) { if (args.length >= 1) System.getProperties().put("mail.host", args[0]); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("From: "); String from = in.readline(); System.out.print("To: "); String to = in.readline(); System.out.print("Subject: "); String subject = in.readline(); URL u = new URL("mailto:" + to); // Create a mailto: URL URLConnection c = u.openconnection(); // Create a URLConnection for it c.setdoinput(false); // Specify no input from this URL c.setdooutput(true); // Specify we'll do output System.out.println("Connecting..."); // Tell the user what's happening System.out.flush(); // Tell them right now c.connect(); // Connect to mail host PrintWriter out = // Get output stream to mail host new PrintWriter(new OutputStreamWriter(c.getOutputStream())); 6
out.println("from: \"" + from + "\" <" + System.getProperty("user.name") + "@" + InetAddress.getLocalHost().getHostName() + ">"); out.println("to: " + to); out.println("subject: " + subject); out.println(); // blank line to end the list of headers System.out.println("Enter the message. " + "End with a '.' on a line by itself."); String line; for(;;) { line = in.readline(); if ((line == null) line.equals(".")) break; out.println(line); out.close(); System.out.println("Message sent."); System.out.flush(); catch (Exception e) { // Handle any exceptions, print error message. System.err.println(e); System.err.println("Usage: java SendMail [<mailhost>]"); 7
Web Server a bağlanma 8
import java.io.*; import java.net.*; public class HttpClient { public static void main(string[] args) { if ((args.length!= 1) && (args.length!= 2)) throw new IllegalArgumentException("Wrong number of arguments"); OutputStream to_file; if (args.length == 2) to_file = new FileOutputStream(args[1]); else to_file = System.out; URL url = new URL(args[0]); String protocol = url.getprotocol(); if (!protocol.equals("http")) throw new IllegalArgumentException("URL must use 'http:' protocol"); String host = url.gethost(); t() int port = url.getport(); if (port == 1) port = 80; // if no port, use the default HTTP port String filename = url.getfile(); Socket socket = new Socket(host, port); InputStream from_server = socket.getinputstream(); Pi PrintWriter twit to_server = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); 9
to_server.println("get " + filename); to_server.flush(); // Send it right now! byte[] buffer = new byte[4096]; int bytes_read; while((bytes_read = from_server.read(buffer))!= 1)) to_file.write(buffer, 0, bytes_read); socket.close(); to_file.close(); catch (Exception e) { // Report any errors that arise System.err.println(e); System.err.println("Usage: java HttpClient <URL> [<filename>]"); 10
import java.io.*; import java.net.*; public class HttpMirror { public static void main(string args[]) { int port = Integer.parseInt(args[0]); ServerSocket ss = new ServerSocket(port); for(;;) { Socket client = ss.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter out = new PrintWriter(new OutputStreamWriter(client.getOutputStream())); out.println("http/1.0 200 "); // Version & status code out.println("content Type: text/plain"); // The type of data we send out.println(); // End of response headers out.flush(); String line; while((line = in.readline())!= null) { if (line.length() == 0) break; out.println(line); out.close(); in.close(); client.close(); close(); // Loop again, waiting for the next connection catch (Exception e) { System.err.println(e); err System.err.println("Usage: java HttpMirror <port>"); 11
SimpleProxyServer.java import java.io.*; import java.net.*; public class SimpleProxyServer { public static void main(string[] args) throws IOException { if (args.length!= 3) throw new IllegalArgumentException("Wrong number of arguments."); String host = args[0]; int remoteport = Integer.parseInt(args[1]); int localport = Integer.parseInt(args[2]); System.out.println( println("starting proxy for " + host + ":" + remoteport + " on port " + localport); runserver(host, remoteport, localport); // never returns catch (Exception e) { System.err.println(e); System.err.println("Usage: java SimpleProxyServer " + "<host> <remoteport> <localport>"); 12
public static void runserver(string host, int remoteport, int localport) throws IOException { ServerSocket ss = new ServerSocket(localport); final byte[] request = new byte[1024]; byte[] reply = new byte[4096]; while(true) { Socket client = null, server = null; client = ss.accept(); final InputStream from_client = client.getinputstream(); final OutputStream to_client= client.getoutputstream(); server = new Socket(host, remoteport); catch (IOException e) { PrintWriter out = new PrintWriter(new OutputStreamWriter(to_client)); out.println( println("proxy server cannot connect to " + host + ":" + remoteport + ":\n" + e); out.flush(); client.close(); continue; 13
final InputStream from_server = server.getinputstream(); final OutputStream to_server = server.getoutputstream(); Thread t = new Thread() { public void run() { int bytes_read; while((bytes_read = from_client.read(request))!= 1) { to_server.write(request, 0, bytes_read); to_server.flush(); catch (IOException e) { to_server.close(); catch (IOException e) { ; t.start(); intbytes _ read; while((bytes_read = from_server.read(reply))!= 1) { to_client.write(reply, 0, bytes_read); to_client.flush(); catch(ioexception e) { to_client.close(); catch (IOException e) { System.err.println(e); finally { if (server!= null) server.close(); if (client!= null) client.close(); catch(ioexception e) { 14
import java.applet.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class Who extends Applet implements ActionListener, Runnable { Button who; // The button in the applet public void init() { who = new Button("Who?"); who.setfont(new Font("SansSerif", Font.PLAIN, 14)); who.addactionlistener(this); who.setactioncommand("who"); this.add(who); public void actionperformed(actionevent e) { if (e.getactioncommand().equals("who")) new Thread(this).start(); public void run() { who.setenabled(false); Frame f = new CloseableFrame("Who's Logged On: Connecting..."); TextArea t = new TextArea(10, 80); t.setfont(new Font("MonoSpaced", Font.PLAIN, 10)); f.add(t, "Center"); f.pack(); f.show(); 15
Socket s = null; PrintWriter out = null; BufferedReader in = null; String hostname = this.getcodebase().gethost(); s = new Socket(hostname, 79); out = new PrintWriter(new OutputStreamWriter(s.getOutputStream())); in = new BufferedReader(new InputStreamReader(s.getInputStream())); out.println(); out.flush(); // Send it now! String line; while((line = in.readline())!= null) { t.append(line); t.append("\n"); f.settitle("who's Logged On: " + hostname); catch (IOException e) { t.append(e.tostring()); f.settitle("who's Logged On: Error"); finally { in.close(); out.close(); s.close(); catch(exception e){ who.setenabled(true); 16