Contoh ini mengenalkan pemrograman soscket dengan Java. Server mendengar (listen) koneksi yang masuk. Ketika suatu koneksi terbangun, client dapat mengirimkan data. Pada kode di bawah ini, client mengirimkan pesan “Hi my server”. Client mengirimkan pesan khusus “bye” untuk menghentikan koneksi dari server. Kemudian server mengirimkan pesan “bye” juga. Terakhir, koneksi diakhiri dan server menunggu koneksi berikutnya. Dua program ini dapat dijalankan pada mesin yang sama. Jika dijalankan pada mesin berbeda, cukup ubah alamat “localhost” dengan suatu IP adress dari mesin di mana server dijalankan.
Server
04 | ServerSocket providerSocket; |
05 | Socket connection = null ; |
06 | ObjectOutputStream out; |
14 | providerSocket = new ServerSocket( 2004 , 10 ); |
16 | System.out.println( "Waiting for connection" ); |
17 | connection = providerSocket.accept(); |
18 | System.out.println( "Connection received from " + connection.getInetAddress().getHostName()); |
20 | out = new ObjectOutputStream(connection.getOutputStream()); |
22 | in = new ObjectInputStream(connection.getInputStream()); |
23 | sendMessage( "Connection successful" ); |
27 | message = (String)in.readObject(); |
28 | System.out.println( "client>" + message); |
29 | if (message.equals( "bye" )) |
32 | catch (ClassNotFoundException classnot){ |
33 | System.err.println( "Data received in unknown format" ); |
35 | } while (!message.equals( "bye" )); |
37 | catch (IOException ioException){ |
38 | ioException.printStackTrace(); |
45 | providerSocket.close(); |
47 | catch (IOException ioException){ |
48 | ioException.printStackTrace(); |
52 | void sendMessage(String msg) |
57 | System.out.println( "server>" + msg); |
59 | catch (IOException ioException){ |
60 | ioException.printStackTrace(); |
63 | public static void main(String args[]) |
65 | Provider server = new Provider(); |
Client
03 | public class Requester{ |
05 | ObjectOutputStream out; |
13 | requestSocket = new Socket( "localhost" , 2004 ); |
14 | System.out.println( "Connected to localhost in port 2004" ); |
16 | out = new ObjectOutputStream(requestSocket.getOutputStream()); |
18 | in = new ObjectInputStream(requestSocket.getInputStream()); |
22 | message = (String)in.readObject(); |
23 | System.out.println( "server>" + message); |
24 | sendMessage( "Hi my server" ); |
28 | catch (ClassNotFoundException classNot){ |
29 | System.err.println( "data received in unknown format" ); |
31 | } while (!message.equals( "bye" )); |
33 | catch (UnknownHostException unknownHost){ |
34 | System.err.println( "You are trying to connect to an unknown host!" ); |
36 | catch (IOException ioException){ |
37 | ioException.printStackTrace(); |
44 | requestSocket.close(); |
46 | catch (IOException ioException){ |
47 | ioException.printStackTrace(); |
51 | void sendMessage(String msg) |
56 | System.out.println( "client>" + msg); |
58 | catch (IOException ioException){ |
59 | ioException.printStackTrace(); |
62 | public static void main(String args[]) |
64 | Requester client = new Requester(); |
0 komentar:
Posting Komentar