Tugas (Pemrograman Jaringan) : Membangun Aplikasi Client-Server TCP Sederhana



simpleServer

import java.io.*;
                        import java.net.*;
                        public class simpleServer {
                                    public final static int TESTPORT = 5000;
                                    public static void main(String args[]) {
                                        ServerSocket checkServer = null;
                                        String line;
                                        BufferedReader is = null;
                                        DataOutputStream os = null;
                                        Socket clientSocket = null;
                                            try {
                                                checkServer = new ServerSocket(TESTPORT);
                                                System.out.println("Aplikasi Server hidup ...");
                                            } catch (IOException e) {
                                                System.out.println(e);
                                            }
                                            try {
                                                clientSocket = checkServer.accept();
                                                is = new BufferedReader(new
                                                InputStreamReader(clientSocket.getInputStream()));
                                                os = new DataOutputStream(clientSocket.getOutputStream());
                                            } catch (Exception ei) {
                                                ei.printStackTrace();
                                            }
                                            try {
                                                line = is.readLine();
                                                System.out.println("Terima : " + line);
                                            if (line.compareTo("salam") == 0) {
                                                os.writeBytes("salam juga");
                                            } else {
                                                os.writeBytes("Maaf, saya tidak mengerti");
                                            }
                                            } catch (IOException e) {
                                                System.out.println(e);
                                            }
                                 
                                            try {
                                            os.close();
                                            is.close();
                                            clientSocket.close();
                                            } catch (IOException ic) {
                                            ic.printStackTrace();
                                            }
                                    }
                                }
 
Pada listing program diatas digunakan untuk membuat aplikasi server menggunakan java. Pada listing ini dibuatlah class simpleServer. Jika program ini dijalankan pada server maka program akan mencetak output Aplikasi Server ini hidup. Lalu jika ada chat dari clien maka program akan mencetak tulisan Terima: (chat dari client). Jika chat dari client bertuliskan "salam" maka program akan mengirim chat balik dengan balasan "salam juga". Jika yang lain program akan mengirim chat ke client "Maaf, saya tidak mengerti". 
 

simpleClient

import java.io.*;
                        import java.net.*;
                        public class simpleClient {
                                    public final static int REMOTE_PORT = 5000;
                                    public static void main(String args[]) throws Exception {
                                        Socket cl = null;
                                        BufferedReader is = null;
                                        DataOutputStream os = null;
                                        BufferedReader stdin = new BufferedReader(new
                                        InputStreamReader(System.in));
                                        String userInput = null;
                                        String output = null;
                                // Membuka koneksi ke server pada port REMOTE_PORT
                                        try {
                                            cl = new Socket(args[0], REMOTE_PORT);
                                            is = new BufferedReader(new
                                            InputStreamReader(cl.getInputStream()));
                                            os = new DataOutputStream(cl.getOutputStream());
                                        } catch(UnknownHostException e1) {
                                            System.out.println("Unknown Host: " + e1);
                                        } catch (IOException e2) {
                                            System.out.println("Erorr io: " + e2);
                                        }
                                // Menulis ke server
                                        try {
                                            System.out.print("Masukkan kata kunci: ");
                                        userInput = stdin.readLine();
                                        os.writeBytes(userInput + "\n");
                                        } catch (IOException ex) {
                                        System.out.println("Error writing to server..." + ex);
                                        }
                                // Menerima tanggapan dari server
                                        try {
                                            output = is.readLine();
                                            System.out.println("Dari server: " + output);
                                        } catch (IOException e) {
                                        e.printStackTrace();
                                        }
                                    // close input stream, output stream dan koneksi
                                        try {
                                        is.close();
                                        os.close();
                                        cl.close();
                                        } catch (IOException x) {
                                        System.out.println("Error writing...." + x);
                                        }
                                    }
                        }
 
Pada listing program diatas digunakan untuk membuat aplikasi client menggunakan java. Pada listing ini dibuatlah class simpleClient. Jika program ini dijalankan pada komputer client maka program akan mencetak output Masukkan kata kunci:. masukkan sebuah kata dalam program tersebut. setelah itu akan ada balasan chat dari server dengan pencetakan tulisan Dari server: (tanggapan dari server). 




Responses

0 Respones to "Tugas (Pemrograman Jaringan) : Membangun Aplikasi Client-Server TCP Sederhana"

Post a Comment

 

Recent Comments

flicker photos

Popular Posts

Return to top of page Copyright © 2010 | Platinum Theme Converted into Blogger Template by HackTutors