+ All Categories
Home > Documents > 3 Java Networking Lanjut

3 Java Networking Lanjut

Date post: 10-Oct-2015
Category:
Upload: syam-gunawan
View: 13 times
Download: 0 times
Share this document with a friend

of 12

Transcript
  • BP4BAHASA PEMROGRAMAN JAVA KOMUNIKASI DATA DAN J2ME

  • BP4-NETWORK PROGRAM//Program diambil dari buku Java Networking Programming-Oreilly//Hal : 210//Simpan File : SourceViewer.java//Compile : javac SourceViewer.java//Cara Menjalankan : java SourceViewer http://www.bl.ac.idimport java.net.*;import java.io.*;public class SourceViewer { public static void main (String[] args) throws Throwable { if (args.length > 0) { try { URL u = new URL(args[0]); //Buka koneksi URL InputStream in = u.openStream(); //jika URL ditemukan //program membaca halaman web in = new BufferedInputStream(in); Reader r = new InputStreamReader(in); int c; while ((c = r.read()) != -1) { System.out.print((char) c); } } catch (MalformedURLException e) { //Jika URL tidak ditemukan System.out.print(args[0] + " is not parseable URL"); } } }} Web serverWeb Page

  • BP4-NETWORK PROGRAMWeb Page

  • BP4-NETWORK PROGRAMEmailEmail menggunakan protocol TCP/IP. Sedangkan port yang digunakan adalah port 25 untuk pengiriman dan port 110 untuk penerimaan mail.jar, imap.jar, mailapi.jar, pop3.jar, dan smtp.jar

  • BP4-NETWORK PROGRAMKirim Email - SMTP//Program diambil dan dimodifikasi dari LKBN ANTARA//Simpan File : SendEmail.java//Compile : javac SendEmail.java//Cara Menjalankan : java SendEmailimport javax.mail.*;import javax.mail.internet.*;import java.util.*;public class SendEmail { public static void main(String[] args) { try { String NamaHost = www.bl.ac.id"; String Kepada = [email protected]"; String Dari = [email protected]"; Properties props = System.getProperties(); props.put("mail.smtp.host", NamaHost); Session session = Session.getInstance(props, null); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(Dari)); InternetAddress[] address = {new InternetAddress(Kepada)}; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject("Testing"); msg.setContent("In program ngetest", "text/plain"); Transport.send(msg); } catch (Exception e) { e.printStackTrace(); } }}

  • BP4-NETWORK PROGRAMTerima Email POP3//program diambil dan dimodifikasi dari Buku//Java Networking//Simpan File : POP3Client.java//Compile : javac POP3Client.java//Cara Menjalankan : java POP3Clientimport javax.mail.*;import javax.mail.internet.*;import java.util.*;public class POP3Client { public static void main(String[] args) { Properties props = System.getProperties(); String host = "202.155.73.26"; String username = "windu"; //mau tau aja passnya String password = "xxxxxx"; String provider = "pop3"; try { //Koneksi POP3 mail server Session session = Session.getDefaultInstance(props, null); Store store = session.getStore(provider); store.connect(host,username,password); //buka folder Folder inbox = store.getFolder("INBOX"); if (inbox == null) { System.out.println("No Inbox"); System.exit(1); } inbox.open(Folder.READ_ONLY); //Tampilkan Pesan Surat Message[] messages = inbox.getMessages(); for (int i = 0; i < messages.length; i++) { System.out.println("---- Message " + (i + 1) + " ----"); messages[i].writeTo(System.out); } //jangan lupa tutup koneksi inbox.close(false); store.close(); } catch (Exception e) { e.printStackTrace(); } }}

  • BP4-NETWORK PROGRAMTCP/IPTCP/IP

  • CHATTING WITH JAVA

  • BP4-NETWORK PROGRAMTCP/IP - Program Server 1

    //program diambil dan dimodifikasi dari java.sun.com//Simple Network TCP/IP programm//Simpan File : KnockKnockProtocol.java//Compile : javac KnockKnockProtocol.java//SubClass Dari : KnockKnockServer.javaimport java.net.*;import java.io.*;public class KnockKnockProtocol { private static final int WAITING = 0; private static final int SENTKNOCKKNOCK = 1; private static final int SENTCLUE = 2; private static final int ANOTHER = 3; private static final int NUMJOKES = 5; private int state = WAITING; private int currentJoke = 0; private String[] clues = { "Indra", "Sinta", "James", "Dian", "Pswasnita" }; private String[] answers = { "Nyalakan Penghangat, Dingin Nich..!", "Taunya Kamu Bisa Nyanyi!", "Doa Untuk Mu!", "Ada Burung Di sini?", "Bisa Terlihat?" }; public String processInput(String theInput) { String theOutput = null; if (state == WAITING) { theOutput = "Tok... Tok... Tok..."; state = SENTKNOCKKNOCK; } else if (state == SENTKNOCKKNOCK) { if (theInput.equalsIgnoreCase("Siapa Yach?")) { theOutput = clues[currentJoke]; state = SENTCLUE; } else { theOutput = "Seharusnya \"Siapa Yach?\"! " +"Coba Lagi. Tok... Tok... Tok...!"; } } else if (state == SENTCLUE) { if (theInput.equalsIgnoreCase(clues[currentJoke] + " siapa?")) { theOutput = answers[currentJoke] + " Ingin Orang Lain? (y/n)"; state = ANOTHER; } else { theOutput = "Seharusnya \"" + clues[currentJoke] + " siapa?\"" + "! Coba Lagi. Tok.. Tok.. Tok..."; state = SENTKNOCKKNOCK; } } else if (state == ANOTHER) { if (theInput.equalsIgnoreCase("y")) { theOutput = "Tok... Tok... Tok..."; if (currentJoke == (NUMJOKES - 1)) currentJoke = 0; else currentJoke++; state = SENTKNOCKKNOCK; } else { theOutput = "Dadah"; state = WAITING; } } return theOutput; }}

  • BP4-NETWORK PROGRAMTCP/IP- Program Server 2//program diambil dan dimodifikasi dari java.sun.com//Simple Network TCP/IP programm//Simpan File : KnockKnockServer.java//Compile : javac KnockKnockServer.java//Cara Menjalankan : java KnockKnockServerimport java.net.*;import java.io.*;public class KnockKnockServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(4444); System.out.println("Okeh .. Server Ready"); } catch (IOException e) { System.err.println("port 4444 Tidak Dapat Digunakan"); System.exit(1); } Socket clientSocket = null; try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.err.println("Gagal Menggunakan Socket."); System.exit(1); } PrintWriter out = new PrintWriter (clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String inputLine, outputLine; KnockKnockProtocol kkp = new KnockKnockProtocol(); outputLine = kkp.processInput(null); out.println(outputLine); while ((inputLine = in.readLine()) != null) { outputLine = kkp.processInput(inputLine); out.println(outputLine); if (outputLine.equals("Dadah")) break; } out.close(); in.close(); clientSocket.close(); serverSocket.close(); }}

  • BP4-NETWORK PROGRAMTCP/IP- Program Client//program diambil dan dimodifikasi dari java.sun.com//Simple Network TCP/IP programm//Simpan File : KnockKnockClient.java//Compile : javac KnockKnockClient.java//Cara Menjalankan : java KnockKnockClientimport java.io.*;import java.net.*;public class KnockKnockClient { public static void main(String[] args) throws IOException { Socket kkSocket = null; PrintWriter out = null; BufferedReader in = null; try { kkSocket = new Socket("localhost", 4444); out = new PrintWriter(kkSocket.getOutputStream(), true); in = new BufferedReader( new InputStreamReader ( kkSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("nga Ada Tuch host: localhost."); System.exit(1); } catch (IOException e) { System.err.println ("koneksi I/O Host : Localhost Gagal."); System.exit(1); } BufferedReader stdIn = new BufferedReader( new InputStreamReader( System.in)); String fromServer; String fromUser; while ((fromServer = in.readLine()) != null) { System.out.println("Server: " + fromServer); if (fromServer.equals("Dadah.")) break; fromUser = stdIn.readLine(); if (fromUser != null) { System.out.println("Client: " + fromUser); out.println(fromUser); } } out.close(); in.close(); stdIn.close(); kkSocket.close(); }}

  • BP4-NETWORK PROGRAMTCP/IP- Running


Recommended