Selasa, 11 Januari 2011 By: artikel blogger

contoh sarver

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
01import java.io.*;
02import java.net.*;
03public class Provider{
04    ServerSocket providerSocket;
05    Socket connection = null;
06    ObjectOutputStream out;
07    ObjectInputStream in;
08    String message;
09    Provider(){}
10    void run()
11    {
12        try{
13            //1. creating a server socket
14            providerSocket = new ServerSocket(2004, 10);
15            //2. Wait for connection
16            System.out.println("Waiting for connection");
17            connection = providerSocket.accept();
18            System.out.println("Connection received from " + connection.getInetAddress().getHostName());
19            //3. get Input and Output streams
20            out = new ObjectOutputStream(connection.getOutputStream());
21            out.flush();
22            in = new ObjectInputStream(connection.getInputStream());
23            sendMessage("Connection successful");
24            //4. The two parts communicate via the input and output streams
25            do{
26                try{
27                    message = (String)in.readObject();
28                    System.out.println("client>" + message);
29                    if (message.equals("bye"))
30                        sendMessage("bye");
31                }
32                catch(ClassNotFoundException classnot){
33                    System.err.println("Data received in unknown format");
34                }
35            }while(!message.equals("bye"));
36        }
37        catch(IOException ioException){
38            ioException.printStackTrace();
39        }
40        finally{
41            //4: Closing connection
42            try{
43                in.close();
44                out.close();
45                providerSocket.close();
46            }
47            catch(IOException ioException){
48                ioException.printStackTrace();
49            }
50        }
51    }
52    void sendMessage(String msg)
53    {
54        try{
55            out.writeObject(msg);
56            out.flush();
57            System.out.println("server>" + msg);
58        }
59        catch(IOException ioException){
60            ioException.printStackTrace();
61        }
62    }
63    public static void main(String args[])
64    {
65        Provider server = new Provider();
66        while(true){
67            server.run();
68        }
69    }
70}
Client
01import java.io.*;
02import java.net.*;
03public class Requester{
04    Socket requestSocket;
05    ObjectOutputStream out;
06    ObjectInputStream in;
07    String message;
08    Requester(){}
09    void run()
10    {
11        try{
12            //1. creating a socket to connect to the server
13            requestSocket = new Socket("localhost", 2004);
14            System.out.println("Connected to localhost in port 2004");
15            //2. get Input and Output streams
16            out = new ObjectOutputStream(requestSocket.getOutputStream());
17            out.flush();
18            in = new ObjectInputStream(requestSocket.getInputStream());
19            //3: Communicating with the server
20            do{
21                try{
22                    message = (String)in.readObject();
23                    System.out.println("server>" + message);
24                    sendMessage("Hi my server");
25                    message = "bye";
26                    sendMessage(message);
27                }
28                catch(ClassNotFoundException classNot){
29                    System.err.println("data received in unknown format");
30                }
31            }while(!message.equals("bye"));
32        }
33        catch(UnknownHostException unknownHost){
34            System.err.println("You are trying to connect to an unknown host!");
35        }
36        catch(IOException ioException){
37            ioException.printStackTrace();
38        }
39        finally{
40            //4: Closing connection
41            try{
42                in.close();
43                out.close();
44                requestSocket.close();
45            }
46            catch(IOException ioException){
47                ioException.printStackTrace();
48            }
49        }
50    }
51    void sendMessage(String msg)
52    {
53        try{
54            out.writeObject(msg);
55            out.flush();
56            System.out.println("client>" + msg);
57        }
58        catch(IOException ioException){
59            ioException.printStackTrace();
60        }
61    }
62    public static void main(String args[])
63    {
64        Requester client = new Requester();
65        client.run();
66    }
67}

0 komentar:

Posting Komentar

Diberdayakan oleh Blogger.

Followers