MULTICAST NETWORK
Mobile Programming
LAST WEEK
TODAY
It’s all about UDP
USER DATAGRAM PROTOCOL
USER DATAGRAM PROTOCOL
UDPVSTCP
UDP is simple and faster
compared toTCP
NOW LET’S BROADCAST
SOMETHING!
WHAT WE WILL BUILD
WHAT WE WILL BUILD
Hello
Client
Server
HELLO
1 import java.io.*;
2 import java.net.*;
3
4 class UDPServer {
5 public static void main(String args[]) throws Exception {
6 DatagramSocket serverSocket = new DatagramSocket(9876);
7 byte[] receiveData = new byte[1024];
8 byte[] sendData = new byte[1024];
9
10 while(true) {
11 DatagramPacket receivePacket = new DatagramPacket(receiveData,
12 receiveData.length);
13 serverSocket.receive(receivePacket);
14 String sentence = new String(receivePacket.getData());
15 System.out.println("RECEIVED: " + sentence);
16 InetAddress IPAddress = receivePacket.getAddress();
17 int port = receivePacket.getPort();
18 String capitalizedSentence = sentence.toUpperCase();
19 sendData = capitalizedSentence.getBytes();
20 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.
21 length, IPAddress, port);
22 serverSocket.send(sendPacket);
23 }
24 }
25 }
1 import java.io.*;
2 import java.net.*;
3
4 class UDPClient {
5 public static void main(String args[]) throws Exception {
6 BufferedReader inFromUser = new BufferedReader(new
InputStreamReader(
7 System.in));
8 DatagramSocket clientSocket = new DatagramSocket();
9 InetAddress IPAddress =InetAddress.getByName("localhost");
10 byte[] sendData = new byte[1024];
11 byte[] receiveData = new byte[1024];
12 String sentence = inFromUser.readLine();
13 sendData = sentence.getBytes();
14 DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length,
15 IPAddress, 9876);
16 clientSocket.send(sendPacket);
17
18
19 DatagramPacket receivePacket = new DatagramPacket(receiveData,
20 receiveData.length);
21 clientSocket.receive(receivePacket);
22 String modifiedSentence = new String(receivePacket.getData());
23 System.out.println("FROM SERVER: " + modifiedSentence);
24 clientSocket.close();
25 }
26 }
YOURTURN!
YOURTURN!
Besok
Client
Server
16 Maret 2016
GROUP ASSIGNMENT
Mobile Programming - 3 UDP
Mobile Programming - 3 UDP
Mobile Programming - 3 UDP

Mobile Programming - 3 UDP

  • 1.
  • 2.
  • 4.
  • 5.
  • 6.
  • 8.
    UDPVSTCP UDP is simpleand faster compared toTCP
  • 9.
  • 10.
  • 11.
    WHAT WE WILLBUILD Hello Client Server HELLO
  • 12.
    1 import java.io.*; 2import java.net.*; 3 4 class UDPServer { 5 public static void main(String args[]) throws Exception { 6 DatagramSocket serverSocket = new DatagramSocket(9876); 7 byte[] receiveData = new byte[1024]; 8 byte[] sendData = new byte[1024]; 9 10 while(true) { 11 DatagramPacket receivePacket = new DatagramPacket(receiveData, 12 receiveData.length); 13 serverSocket.receive(receivePacket); 14 String sentence = new String(receivePacket.getData()); 15 System.out.println("RECEIVED: " + sentence); 16 InetAddress IPAddress = receivePacket.getAddress(); 17 int port = receivePacket.getPort(); 18 String capitalizedSentence = sentence.toUpperCase(); 19 sendData = capitalizedSentence.getBytes(); 20 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData. 21 length, IPAddress, port); 22 serverSocket.send(sendPacket); 23 } 24 } 25 }
  • 13.
    1 import java.io.*; 2import java.net.*; 3 4 class UDPClient { 5 public static void main(String args[]) throws Exception { 6 BufferedReader inFromUser = new BufferedReader(new InputStreamReader( 7 System.in)); 8 DatagramSocket clientSocket = new DatagramSocket(); 9 InetAddress IPAddress =InetAddress.getByName("localhost"); 10 byte[] sendData = new byte[1024]; 11 byte[] receiveData = new byte[1024]; 12 String sentence = inFromUser.readLine(); 13 sendData = sentence.getBytes(); 14 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, 15 IPAddress, 9876); 16 clientSocket.send(sendPacket); 17 18 19 DatagramPacket receivePacket = new DatagramPacket(receiveData, 20 receiveData.length); 21 clientSocket.receive(receivePacket); 22 String modifiedSentence = new String(receivePacket.getData()); 23 System.out.println("FROM SERVER: " + modifiedSentence); 24 clientSocket.close(); 25 } 26 }
  • 14.
  • 15.
  • 16.