Kode dari server, pada kelas EchoServer, adalah sebagai berikut:
01 | package netprog2010; |
02 |
03 | //Server yang meng-echoe kembali pesan dari client (TCP) |
04 | import java.io.*; |
05 | import java.net.*; |
06 | import java.util.*; |
07 |
08 | public class EchoServer { |
09 |
10 | private static ServerSocket servSock; |
11 | private static final int PORT = 1234; |
12 |
13 | public static void main(String[] args) { |
14 | System.out.println("Membuka port...\n"); |
15 |
16 | try { |
17 | servSock = new ServerSocket(PORT); //Langkah 1. |
18 | } catch (IOException ioEx) { |
19 | System.out.println("Tidak terhubung ke port!"); |
20 | System.exit(1); |
21 | } |
22 |
23 | do { |
24 | handleClient(); |
25 | } while (true); |
26 | } // akhir dari metode main() |
27 |
28 | private static void handleClient() { |
29 | Socket link = null; //Langkah 2. |
31 | link = servSock.accept(); // Langkah 2. |
32 | Scanner input = new Scanner(link.getInputStream()); //Langkah 3 |
33 | PrintWriter output = new PrintWriter(link.getOutputStream(), true); |
34 |
35 | int numMessages = 0; |
36 | String message = input.nextLine(); // Langkah 4. |
37 |
38 | while (!message.equals("QUIT")) { |
39 | numMessages++; |
40 | System.out.println("Pesan masuk [" + numMessages + "]: " + message); |
41 | output.println("Pesan " + numMessages + ": " + message); |
42 | message = input.nextLine(); |
43 | } |
44 |
45 | output.println(numMessages + " pesan yang diterima."); |
46 | } // akhir dari try |
47 | catch (IOException ioEx) { |
48 | //ioEx.printStackTrace(); |
49 | } finally { |
50 | try { |
51 | System.out.println("\nMenutup koneksi..."); |
52 | link.close(); // Langkah 5. |
53 | } catch (IOException ioEx) { |
54 | System.out.println("Gagal menutup koneksi!"); |
55 | System.exit(1); |
56 | } |
57 | } //akhir dari finally |
58 | } // akhir dari handleClient() |
59 | } // akhir dari kelas TCPEchoServer |
01 | package netprog2010; |
02 |
03 | import java.io.*; |
04 | import java.net.*; |
05 | import java.util.*; |
06 |
07 | public class EchoClient { |
08 |
09 | private static InetAddress host; |
10 | private static final int PORT = 1234; |
11 |
12 | public static void main(String[] args) { |
13 | try { |
14 | host = InetAddress.getLocalHost(); |
15 | } catch (UnknownHostException uhEx) { |
16 | System.out.println("Host ID not found!"); |
17 | System.exit(1); |
18 | } |
19 | accessServer(); |
20 | } //akhir dari metode main() |
21 |
22 | private static void accessServer() { |
23 | Socket link = null; //Langkah 1. |
24 | try { |
25 | link = new Socket(host, PORT); //langkah 1. |
26 | Scanner input = new Scanner(link.getInputStream()); //Langkah 2. |
27 | PrintWriter output = new PrintWriter(link.getOutputStream(), true); //Langkah 2. |
28 |
29 | //Set up stream untuk input dari keyboard |
30 | Scanner userEntry = new Scanner(System.in); |
31 | String message, response; |
32 |
33 | do { |
34 | System.out.print("Masukkan pesan (QUIT untuk keluar): "); |
35 | message = userEntry.nextLine(); |
36 | output.println(message); //Langkah 3. |
37 | response = input.nextLine(); //Langkah 3. |
38 | System.out.println("\nSERVER> " + response); |
39 | } while (!message.equals("QUIT")); |
40 | } catch (IOException ioEx) { |
41 | //ioEx.printStackTrace(); |
42 | } finally { |
43 | try { |
44 | System.out.println("\nMenutup koneksi..."); |
45 | link.close(); //Langkah 4. |
46 | } catch (IOException ioEx) { |
47 | System.out.println("Gagal menutup koneksi!"); |
48 | System.exit(1); |
49 | } |
50 | } |
51 | } //akhir metode accessServer |
52 | } //akhir kelas TCPEchoClient |

0 komentar:
Posting Komentar