Minggu, 15 Juni 2014

Socket Programming

Pada postingan kali ini saya akan memposting materi Socet Programming. Socet Programming terdiri dari Client dan Server.


Listing Server
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author Jeffrey
 */
public class Server {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        ServerSocket server = null;
        Socket client;
        try {
            server = new ServerSocket(1234);
                                    System.out.println("Server Turned On");
        } catch (IOException ie) {
            System.out.println("Cannot open socket.");
            System.exit(1);
        }
        String temp;
        while(true) {
            try {
                client = server.accept();
                OutputStream clientOut = client.getOutputStream();
                PrintWriter pw = new PrintWriter(clientOut, true);
                InputStream clientIn = client.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(clientIn));
                temp = br.readLine();
                System.out.println(temp);
                pw.println(temp);
                br.close();
                pw.close();
                clientIn.close();
                clientOut.close();
            } catch (IOException ie) {
                ie.printStackTrace();
            }
        }}}

Listing Client
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**

 *

 * @author Jeffrey

 */

public class Client {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) throws UnknownHostException, IOException {

        String temp = "";

        do {

            try {

                Socket client = new Socket(InetAddress.getLocalHost(),1234);

                InputStream clientIn = client.getInputStream();

                OutputStream clientOut = client.getOutputStream();

                PrintWriter pw = new PrintWriter(clientOut, true);

                //BufferedReader br = new BufferedReader(new InputStreamReader(clientIn));

                BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

                System.out.println("Type a message for the server: ");

                temp = stdIn.readLine();

                pw.println(temp);

                System.out.println("Server message: ");

                //System.out.println(br.readLine());

                pw.close();

                //br.close();

                client.close();

            } catch (ConnectException ce) {

                System.out.println("Cannot connect to the server.");

            } catch (IOException ie) {

                System.out.println("I/O Error.");

          }}

        while (!temp.matches("EXIT"));

    }}

OUTPUT


CLIENT


SERVER
Pada sintak di atas menjelaskan bahwa server dan client saling terhubung satu sama lain. Dimana kedua nya dapat saling mengirim pesan. Yang pertama di jalankan adalah server, kemudian baru client. Setelah itu client dapat mengirim sebuah pesan kepada server kemudian server pun dapat menerima pesan yang dikirim client. Selama kedua jendela saling terbuka, maka selama itu pula keduanya dapat saling bertukar pesan.

0 komentar:

Posting Komentar

By :
Free Blog Templates