JAVA NETWORKING
Mobile Programming
NETWORKTODAY
NETWORKTODAY
NETWORKTODAY
NETWORKTODAY
NETWORK IS…
WEB IS BIGGER NETWORK
HOWTHEY COMMUNICATE?
PROTOCOL
ILLUSTRATION
IP: 192.168.1.1
IP: 192.168.1.2 IP: 192.168.1.3 IP: 192.168.1.4 IP: 192.168.1.5 IP: 192.168.1.6
DETAILED ILLUSTRATION
Logical Path
Physical Layer
Internet Layer (IP) Internet Layer (IP)
Transport Layer (TCP/IP) Transport Layer (TCP/IP)
Application Layer Application Layer
TERMINOLOGY
• Transmission Control Protocol
• User Datagram Protocol
• Port
• URL
• HTTP
MORETERMINOLOGY
• HTML
• XML
• FTP
• SMTP
• POP3/IMAP
PROTOCOL SCANNER
1 import java.net.*;
2
3 public class ProtocolTester {
4 public static void main(String[] args) {
5 String url = "www.bl.ac.id";
6
7 testProtocol("http://" + url);
8 testProtocol("https://" + url);
9 testProtocol("ftp://" + url);
10 testProtocol("nfs://" + url);
11 testProtocol("telnet://" + url);
12 testProtocol("mailto:rizafahmi@gmail.com");
13
14
15 }
16
17 private static void testProtocol (String url) {
18 try {
19 URL u = new URL(url);
20 System.out.println(u.getProtocol() + " is supported.");
21 } catch (MalformedURLException e) {
22 String protocol = url.substring(0, url.indexOf(":"));
23 System.out.println(protocol + " is not supported");
24 }
25 }
26 }
YOURTURN!
PORT SCANNER
1 import java.net.*;
2 import java.io.*;
3
4 public class LowPortScanner {
5 public static void main(String[] args) {
6 String host = "www.bl.ac.id";
7 System.out.println("Tunggu ya, sedang
mendeteksi...");
8
9 for (int i = 1; i < 100; i++) {
10 try {
11 Socket s = new Socket(host, i);
12 System.out.println("Port " + i + " digunakan
" + host);
13 } catch (UnknownHostException e) {
14 System.err.println(e);
15 } catch (IOException e) {
16 System.out.println("Port " + i + " Tidak
Digunakan " + host);
17 }
18 }
19 }
20 }
YOURTURN!
DOWNLOAD AND PARSE
HTML PAGE
1 import java.net.*;
2 import java.io.*;
3 public class SourceViewer {
4
5 public static void main (String[] args) {
6 if (args.length > 0) {
7 InputStream in = null;
8 try {
9 URL u = new URL(args[0]);
10 in = u.openStream();
11
12 in = new BufferedInputStream(in);
13 Reader r = new InputStreamReader(in);
14 int c;
15 while((c = r.read()) != -1) {
16 System.out.print((char) c);
17 }
18 } catch (MalformedURLException e) {
19 System.out.print(args[0] + " is not
parseable URL");
20 } catch (IOException e) {
21 System.err.println(e);
22 } finally {
23 if (in != null) {
24 try {
25 in.close();
26 } catch (IOException e) {
27 // ignore
28 }
29 }
30 }
31
32 }
33 }
34 }
> java SourceViewer http://www.bl.ac.id/
<!DOCTYPE html>
<html lang="id-ID">
<head>
<meta charset="UTF-8" />
<title>Universitas Budi Luhur</title>
<link rel="profile" href="http://gmpg.org/xfn/11" />
<link rel="pingback" href="http://www.budiluhur.ac.id/xmlrpc.php" />
<script type="text/javascript" src="http://www.budiluhur.ac.id/wp-
content/themes/new-bl/images/jquery.js"></script>
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://
www.
budiluhur.ac.id/xmlrpc.php?rsd">
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://
www.
budiluhur.ac.id/wp-includes/wlwmanifest.xml">
<link rel="index" title="Universitas Budi Luhur" href="http://
www.budiluhur.ac.
id/">
<meta name="generator" content="WordPress 3.0.1">
…
YOURTURN!
SENDING EMAIL
1 import java.util.*;
2 import javax.mail.*;
3 import javax.mail.internet.*;
4 import javax.activation.*;
5
6 public class SendEmail {
7 public static void main (String[] args) {
8 String to = "rizafahmi@gmail.com";
9 String from = “riza@appsco.id";
10 String host = "192.168.1.134";
11 Properties properties = System.getProperties();
12 properties.setProperty("mail.smtp.host", host);
13 properties.put("mail.smtp.port", 1025);
14 Session session = Session.getDefaultInstance(properties);
15
16 try {
17 MimeMessage message = new MimeMessage(session);
18 message.setFrom(new InternetAddress(from));
19 message.addRecipient(Message.RecipientType.TO, new
InternetAddress(to));
20 message.setSubject("This is the subject line");
21 message.setText("This is actual message");
22
23 Transport.send(message);
24 System.out.println("Sent message successfully...");
25 } catch (MessagingException e) {
26 e.printStackTrace();
27 }
28 }
29 }
30
RECEIVING EMAIL
1 import java.util.Properties;
2 import javax.mail.*;
3 import javax.mail.internet.*;
4
5 public class POP3Client {
6 public static void main(String[] args) throws Exception {
7
8 Properties properties = getProperties();
9
10 Session session = getSession(properties);
11
12 Store store = connectingStore(session);
13
14 Folder inbox = getInboxFolder(store, "Inbox");
15
16 Message[] messages = inbox.getMessages();
17
18 if (messages.length == 0)
19 System.out.println("No message found");
20
21 System.out.println(messages.length);
22
23 inbox.close(true);
24 store.close();
25 }
26
27 private static Properties getProperties() {
28
29 Properties properties = System.getProperties();
30 properties.put("mail.pop3.port", 995);
31 properties.put("mail.pop3.ssl.enable", true);
32 properties.setProperty("mail.pop3.ssl.trust", "*");
33
34 return properties;
35 }
36
37 private static Session getSession(Properties properties) {
38 Session session = Session.getDefaultInstance(properties);
39
40 return session;
41 }
42
43 private static Store connectingStore(Session session) throws
Exception {
44 String host = "pop.zoho.com";
45 String username = "riza@appsco.id";
46 String password = "123456";
47
48 Store store = session.getStore("pop3");
49 store.connect(host, 995, username, password);
50
51 return store;
52 }
53
54 private static Folder getInboxFolder(Store store, String
folder) throws
55 Exception {
56 Folder inbox = store.getFolder("Inbox");
57 inbox.open(Folder.READ_ONLY);
58
59 return inbox;
60 }
61 }
YOURTURN!
LET’S DO CHAT!
PROTOCOL
1 import java.net.*;
2 import java.io.*;
3
4 public class KnockKnockProtocol {
5 private static final int WAITING = 0;
6 private static final int SENTKNOCKKNOCK = 1;
7 private static final int SENTCLUE = 2;
8 private static final int ANOTHER = 3;
9 private static final int NUMJOKES = 5;
10
11 private int state = WAITING;
12 private int currentJoke = 0;
13 private String[] clues = {"Bessarion", "Colby", "Gary", "Teodor", "Kamala"};
14 private String[] answers = {"Matiin AC dong, dingin nih!", "Lagi pusing
15 ya?", "Kelihatan ngga?", "Semoga sukses",
16 "Bazinga!"};
17
18 public String processInput(String inputString) {
19 String outputString = null;
20
21 if (state == WAITING) {
22 outputString = "Tok, tok, tok";
23 state = SENTKNOCKKNOCK;
24 } else if (state == SENTKNOCKKNOCK) {
25 if (inputString.equalsIgnoreCase("Siapa?")) {
26 outputString = clues[currentJoke];
27 state = SENTCLUE;
28 } else {
29 outputString = "Seharusnya kamu menjawab "Siapa?"! " + "Yuk kita
30 coba lagi. Tok, tok, tok";
31 state = ANOTHER;
32 }
33 } else if (state == SENTCLUE) {
34 if (inputString.equalsIgnoreCase(clues[currentJoke] + " siapa?")) {
35 outputString = answers[currentJoke] + " Ingin orang lain? (y/n)";
36 state = ANOTHER;
37 } else {
38 outputString = "Seharusnya "" + clues[currentJoke] + " siapa?"" +
39 "! Coba lagi yaa. Tok, tok, tok";
40 state = SENTKNOCKKNOCK;
41 }
42 } else if (state == ANOTHER) {
43 if (inputString.equalsIgnoreCase("y")) {
44 outputString = "Tok, tok, tok";
45 if (currentJoke == (NUMJOKES - 1))
46 currentJoke = 0;
47 else
48 currentJoke++;
49 state = SENTKNOCKKNOCK;
50 } else {
51 outputString = "Bye bye!";
52 state = WAITING;
53 }
54
55 }
56
57 return outputString;
58 }
59 }
SERVER
1 import java.net.*;
2 import java.io.*;
3
4 public class KnockKnockServer {
5 public static void main(String[] args) throws IOException {
6 ServerSocket serverSocket = null;
7
8 try {
9 serverSocket = new ServerSocket(4444);
10 System.out.println("Ok, server ready!");
11 } catch (IOException e) {
12 System.err.println("port 4444 tidak dapat digunakan.");
13 System.exit(1);
14 }
15
16 Socket clientSocket = null;
17 try {
18 clientSocket = serverSocket.accept();
19 } catch (IOException e) {
20 System.err.println("Client gagal menggunakan port.");
21 System.exit(1);
22 }
23
24 PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
25
26 BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.
27 getInputStream()));
28
29 String inputLine, outputLine;
30
31 KnockKnockProtocol kkp = new KnockKnockProtocol();
32 outputLine = kkp.processInput(null);
33 out.println(outputLine);
34
35 while((inputLine = in.readLine()) != null) {
36 outputLine = kkp.processInput(inputLine);
37 out.println(outputLine);
38
39 if (outputLine.equals("Bye bye!"))
40 break;
41 }
42
43 out.close();
44 in.close();
45 clientSocket.close();
46 serverSocket.close();
47 }
48 }
CLIENT
1 import java.io.*;
2 import java.net.*;
3
4 public class KnockKnockClient {
5 public static void main(String[] args) throws IOException {
6 Socket kkSocket = null;
7 PrintWriter out = null;
8 BufferedReader in = null;
9
10 try {
11 kkSocket = new Socket("localhost", 4444);
12 out = new PrintWriter(kkSocket.getOutputStream(), true);
13 in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream())
14 );
15
16 } catch (UnknownHostException e) {
17 System.err.println("Host localhost tidak ditemukan.");
18 System.exit(1);
19 } catch (IOException e) {
20 System.err.println("Koneksi Input output ke localhost gagal.");
21 System.exit(1);
22 }
23
24 BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)
25 );
26 String fromServer;
27 String fromUser;
28
29 while((fromServer = in.readLine()) != null) {
30 System.out.println("Server: " + fromServer);
31 if (fromServer.equals("Bye bye!"))
32 break;
33
34 fromUser = stdIn.readLine();
35
36 if (fromUser != null) {
37 System.out.println("Client: " + fromUser);
38 out.println(fromUser);
39 }
40 }
41
42 out.close();
43 in.close();
44 stdIn.close();
45 kkSocket.close();
46 }
47 }
YOURTURN!

Mobile Programming - Network Universitas Budi Luhur

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
    ILLUSTRATION IP: 192.168.1.1 IP: 192.168.1.2IP: 192.168.1.3 IP: 192.168.1.4 IP: 192.168.1.5 IP: 192.168.1.6
  • 11.
    DETAILED ILLUSTRATION Logical Path PhysicalLayer Internet Layer (IP) Internet Layer (IP) Transport Layer (TCP/IP) Transport Layer (TCP/IP) Application Layer Application Layer
  • 12.
    TERMINOLOGY • Transmission ControlProtocol • User Datagram Protocol • Port • URL • HTTP
  • 13.
    MORETERMINOLOGY • HTML • XML •FTP • SMTP • POP3/IMAP
  • 14.
  • 15.
    1 import java.net.*; 2 3public class ProtocolTester { 4 public static void main(String[] args) { 5 String url = "www.bl.ac.id"; 6 7 testProtocol("http://" + url); 8 testProtocol("https://" + url); 9 testProtocol("ftp://" + url); 10 testProtocol("nfs://" + url); 11 testProtocol("telnet://" + url); 12 testProtocol("mailto:[email protected]"); 13 14 15 } 16 17 private static void testProtocol (String url) { 18 try { 19 URL u = new URL(url); 20 System.out.println(u.getProtocol() + " is supported."); 21 } catch (MalformedURLException e) { 22 String protocol = url.substring(0, url.indexOf(":")); 23 System.out.println(protocol + " is not supported"); 24 } 25 } 26 }
  • 16.
  • 17.
  • 18.
    1 import java.net.*; 2import java.io.*; 3 4 public class LowPortScanner { 5 public static void main(String[] args) { 6 String host = "www.bl.ac.id"; 7 System.out.println("Tunggu ya, sedang mendeteksi..."); 8 9 for (int i = 1; i < 100; i++) { 10 try { 11 Socket s = new Socket(host, i); 12 System.out.println("Port " + i + " digunakan " + host); 13 } catch (UnknownHostException e) { 14 System.err.println(e); 15 } catch (IOException e) { 16 System.out.println("Port " + i + " Tidak Digunakan " + host); 17 } 18 } 19 } 20 }
  • 19.
  • 20.
  • 21.
    1 import java.net.*; 2import java.io.*; 3 public class SourceViewer { 4 5 public static void main (String[] args) { 6 if (args.length > 0) { 7 InputStream in = null; 8 try { 9 URL u = new URL(args[0]); 10 in = u.openStream(); 11 12 in = new BufferedInputStream(in); 13 Reader r = new InputStreamReader(in); 14 int c; 15 while((c = r.read()) != -1) { 16 System.out.print((char) c); 17 }
  • 22.
    18 } catch(MalformedURLException e) { 19 System.out.print(args[0] + " is not parseable URL"); 20 } catch (IOException e) { 21 System.err.println(e); 22 } finally { 23 if (in != null) { 24 try { 25 in.close(); 26 } catch (IOException e) { 27 // ignore 28 } 29 } 30 } 31 32 } 33 } 34 }
  • 23.
    > java SourceViewerhttp://www.bl.ac.id/ <!DOCTYPE html> <html lang="id-ID"> <head> <meta charset="UTF-8" /> <title>Universitas Budi Luhur</title> <link rel="profile" href="http://gmpg.org/xfn/11" /> <link rel="pingback" href="http://www.budiluhur.ac.id/xmlrpc.php" /> <script type="text/javascript" src="http://www.budiluhur.ac.id/wp- content/themes/new-bl/images/jquery.js"></script> <link rel="EditURI" type="application/rsd+xml" title="RSD" href="http:// www. budiluhur.ac.id/xmlrpc.php?rsd"> <link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http:// www. budiluhur.ac.id/wp-includes/wlwmanifest.xml"> <link rel="index" title="Universitas Budi Luhur" href="http:// www.budiluhur.ac. id/"> <meta name="generator" content="WordPress 3.0.1"> …
  • 24.
  • 25.
  • 26.
    1 import java.util.*; 2import javax.mail.*; 3 import javax.mail.internet.*; 4 import javax.activation.*; 5 6 public class SendEmail { 7 public static void main (String[] args) { 8 String to = "[email protected]"; 9 String from = “[email protected]"; 10 String host = "192.168.1.134"; 11 Properties properties = System.getProperties(); 12 properties.setProperty("mail.smtp.host", host); 13 properties.put("mail.smtp.port", 1025); 14 Session session = Session.getDefaultInstance(properties); 15
  • 27.
    16 try { 17MimeMessage message = new MimeMessage(session); 18 message.setFrom(new InternetAddress(from)); 19 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 20 message.setSubject("This is the subject line"); 21 message.setText("This is actual message"); 22 23 Transport.send(message); 24 System.out.println("Sent message successfully..."); 25 } catch (MessagingException e) { 26 e.printStackTrace(); 27 } 28 } 29 } 30
  • 28.
  • 29.
    1 import java.util.Properties; 2import javax.mail.*; 3 import javax.mail.internet.*; 4 5 public class POP3Client { 6 public static void main(String[] args) throws Exception { 7 8 Properties properties = getProperties(); 9 10 Session session = getSession(properties); 11 12 Store store = connectingStore(session); 13 14 Folder inbox = getInboxFolder(store, "Inbox"); 15 16 Message[] messages = inbox.getMessages(); 17 18 if (messages.length == 0) 19 System.out.println("No message found"); 20 21 System.out.println(messages.length); 22 23 inbox.close(true); 24 store.close(); 25 } 26
  • 30.
    27 private staticProperties getProperties() { 28 29 Properties properties = System.getProperties(); 30 properties.put("mail.pop3.port", 995); 31 properties.put("mail.pop3.ssl.enable", true); 32 properties.setProperty("mail.pop3.ssl.trust", "*"); 33 34 return properties; 35 } 36 37 private static Session getSession(Properties properties) { 38 Session session = Session.getDefaultInstance(properties); 39 40 return session; 41 } 42
  • 31.
    43 private staticStore connectingStore(Session session) throws Exception { 44 String host = "pop.zoho.com"; 45 String username = "[email protected]"; 46 String password = "123456"; 47 48 Store store = session.getStore("pop3"); 49 store.connect(host, 995, username, password); 50 51 return store; 52 } 53 54 private static Folder getInboxFolder(Store store, String folder) throws 55 Exception { 56 Folder inbox = store.getFolder("Inbox"); 57 inbox.open(Folder.READ_ONLY); 58 59 return inbox; 60 } 61 }
  • 32.
  • 33.
  • 35.
  • 36.
    1 import java.net.*; 2import java.io.*; 3 4 public class KnockKnockProtocol { 5 private static final int WAITING = 0; 6 private static final int SENTKNOCKKNOCK = 1; 7 private static final int SENTCLUE = 2; 8 private static final int ANOTHER = 3; 9 private static final int NUMJOKES = 5; 10 11 private int state = WAITING; 12 private int currentJoke = 0; 13 private String[] clues = {"Bessarion", "Colby", "Gary", "Teodor", "Kamala"}; 14 private String[] answers = {"Matiin AC dong, dingin nih!", "Lagi pusing 15 ya?", "Kelihatan ngga?", "Semoga sukses", 16 "Bazinga!"}; 17
  • 37.
    18 public StringprocessInput(String inputString) { 19 String outputString = null; 20 21 if (state == WAITING) { 22 outputString = "Tok, tok, tok"; 23 state = SENTKNOCKKNOCK; 24 } else if (state == SENTKNOCKKNOCK) { 25 if (inputString.equalsIgnoreCase("Siapa?")) { 26 outputString = clues[currentJoke]; 27 state = SENTCLUE; 28 } else { 29 outputString = "Seharusnya kamu menjawab "Siapa?"! " + "Yuk kita 30 coba lagi. Tok, tok, tok"; 31 state = ANOTHER; 32 } 33 } else if (state == SENTCLUE) { 34 if (inputString.equalsIgnoreCase(clues[currentJoke] + " siapa?")) { 35 outputString = answers[currentJoke] + " Ingin orang lain? (y/n)"; 36 state = ANOTHER; 37 } else { 38 outputString = "Seharusnya "" + clues[currentJoke] + " siapa?"" + 39 "! Coba lagi yaa. Tok, tok, tok"; 40 state = SENTKNOCKKNOCK; 41 }
  • 38.
    42 } elseif (state == ANOTHER) { 43 if (inputString.equalsIgnoreCase("y")) { 44 outputString = "Tok, tok, tok"; 45 if (currentJoke == (NUMJOKES - 1)) 46 currentJoke = 0; 47 else 48 currentJoke++; 49 state = SENTKNOCKKNOCK; 50 } else { 51 outputString = "Bye bye!"; 52 state = WAITING; 53 } 54 55 } 56 57 return outputString; 58 } 59 }
  • 39.
  • 40.
    1 import java.net.*; 2import java.io.*; 3 4 public class KnockKnockServer { 5 public static void main(String[] args) throws IOException { 6 ServerSocket serverSocket = null; 7 8 try { 9 serverSocket = new ServerSocket(4444); 10 System.out.println("Ok, server ready!"); 11 } catch (IOException e) { 12 System.err.println("port 4444 tidak dapat digunakan."); 13 System.exit(1); 14 } 15 16 Socket clientSocket = null; 17 try { 18 clientSocket = serverSocket.accept(); 19 } catch (IOException e) { 20 System.err.println("Client gagal menggunakan port."); 21 System.exit(1); 22 } 23
  • 41.
    24 PrintWriter out= new PrintWriter(clientSocket.getOutputStream(), true); 25 26 BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket. 27 getInputStream())); 28 29 String inputLine, outputLine; 30 31 KnockKnockProtocol kkp = new KnockKnockProtocol(); 32 outputLine = kkp.processInput(null); 33 out.println(outputLine); 34 35 while((inputLine = in.readLine()) != null) { 36 outputLine = kkp.processInput(inputLine); 37 out.println(outputLine); 38 39 if (outputLine.equals("Bye bye!")) 40 break; 41 } 42 43 out.close(); 44 in.close(); 45 clientSocket.close(); 46 serverSocket.close(); 47 } 48 }
  • 42.
  • 43.
    1 import java.io.*; 2import java.net.*; 3 4 public class KnockKnockClient { 5 public static void main(String[] args) throws IOException { 6 Socket kkSocket = null; 7 PrintWriter out = null; 8 BufferedReader in = null; 9 10 try { 11 kkSocket = new Socket("localhost", 4444); 12 out = new PrintWriter(kkSocket.getOutputStream(), true); 13 in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()) 14 ); 15 16 } catch (UnknownHostException e) { 17 System.err.println("Host localhost tidak ditemukan."); 18 System.exit(1); 19 } catch (IOException e) { 20 System.err.println("Koneksi Input output ke localhost gagal."); 21 System.exit(1); 22 } 23
  • 44.
    24 BufferedReader stdIn= new BufferedReader(new InputStreamReader(System.in) 25 ); 26 String fromServer; 27 String fromUser; 28 29 while((fromServer = in.readLine()) != null) { 30 System.out.println("Server: " + fromServer); 31 if (fromServer.equals("Bye bye!")) 32 break; 33 34 fromUser = stdIn.readLine(); 35 36 if (fromUser != null) { 37 System.out.println("Client: " + fromUser); 38 out.println(fromUser); 39 } 40 } 41 42 out.close(); 43 in.close(); 44 stdIn.close(); 45 kkSocket.close(); 46 } 47 }
  • 45.