Teknik Komputasi: Ujian Akhir Semester (UAS)
Mobile Computing
Tugas Individu 1
Dosen : Ir. Teddy Mantoro, M.Sc. Ph.D Nama : Yoga Prihastomo NIM : 1011601026 Kelas : XB
MAGISTER ILMU KOMPUTER UNIVERSITAS BUDI LUHUR 2012
Halaman 0
Mobile Computing: Tugas Individu 1
Lingkungan Pengujian: Processor: Intel Core2Duo T6600 Memory: 3GB Sistem Operasi: Windows XP SP3 Versi JDK: 1.6.0 Update 27 Editor: Notepad++
Halaman 1
Mobile Computing: Tugas Individu 1
A. TCP Protocol 1) TCPServer Listening di Port 6789; Merespon inputan dari Client dengan menjadikannya huruf kapital. Adapun programnya adalah sebagai berikut: /* TCPServer Listening on Port 6789 */ import java.io.*; import java.net.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); outToClient.writeBytes(clientSentence.toUpperCase()+'\n'); } } }
Kompilasi: javac TCPServer.java
Eksekusi: java TCPServer
Cek Port: netstat –a –n –o –p tcp
Halaman 2
Mobile Computing: Tugas Individu 1
2) TCP Client Menggunakan Port 6789 untuk berkomunikasi dengan TCP Server; Menerima respon inputan dari Server berupa huruf kapital; Melakukan looping (while) hingga mengetik kata EXIT. Adapun programnya adalah sebagai berikut: /* TCPClient Loop Until Input Equal Exit */ import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String modifiedSentence="EXIT"; String sentence; while ( modifiedSentence.equals("EXIT") ) { BufferedReader inFromUser = new BufferedReader (new InputStreamReader(System.in)); Socket clientSocket = new Socket("localhost", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader (new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); if ( !modifiedSentence.equals("EXIT") ) { modifiedSentence="EXIT"; } else { modifiedSentence="GOEXIT"; } clientSocket.close(); } } }
Kompilasi: javac TCPClient.java
Eksekusi: java TCPClient
Halaman 3
Mobile Computing: Tugas Individu 1
Cek Port: netstat –a –n –o –p tcp
B. UDP Protocol 1) UDP Server Listening di Port 9876; Merespon inputan dari Client dengan menjadikannya huruf kapital. Adapun programnya adalah sebagai berikut: /* UDPServer Listening on Port 9876*/ import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[512]; byte[] sendData = new byte[512]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); String sentence = new String(receivePacket.getData()); sentence = sentence.toUpperCase(); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); sendData = sentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket (sendData, sendData.length, IPAddress, port);
Halaman 4
Mobile Computing: Tugas Individu 1
serverSocket.send(sendPacket); } } }
Kompilasi: javac UDPServer.java
Eksekusi: java UDPServer
Cek Port: netstat –a –n –o –p udp
2) UDP Client Menggunakan Port 9876 untuk berkomunikasi dengan UDP Server; Menerima respon inputan dari Server berupa huruf kapital; Melakukan looping (do‐while) hingga mengetik kata EXIT. Adapun programnya adalah sebagai berikut: /* UDPClient Loop Until Input Equal Exit */ import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception { String sentence; do { BufferedReader inFromUser = new BufferedReader (new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("localhost"); byte[] sendData = new byte[512];
Halaman 5
Mobile Computing: Tugas Individu 1
byte[] receiveData = new byte[512]; sentence = inFromUser.readLine(); sendData = sentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket (sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket (receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String (receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence.trim()); clientSocket.close(); sentence = sentence.toUpperCase(); } while( !sentence.equals("EXIT")); } }
Kompilasi: javac UDPClient.java
Eksekusi: java UDPClient
Cek Port: netstat –a –n –o –p udp
Halaman 6