Senin, 05 Mei 2014

Contoh Socket TCP di Java

Class JavaSocketClient.
=========================================================================
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javasocketserver;

/**
 *
 * @author Hakim
 */
import java.io.*;
import java.net.*;
public class JavaSocketClient {
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 menggunakan remote port
    try {
        cl = new Socket("LocalHost", 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("Error io: " + e2);
    }
   
    //Menulis ke server
    do {
        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();
         while(true);
    } catch (IOException x ) {
        System.out.println("Error writing..." + x);
       
}





=========================================================================

Class JavaSocketServer



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javasocketserver;

/**
 *
 * @author Hakim
 */
import java.io.*;
import java.net.*;
public class JavaSocketServer {
public final static int TESTPORT = 5000;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        ServerSocket checkServer = null;
        String line;
        BufferedReader is = null ;
        DataOutputStream os = null;
        Socket clientSocket = null;
        try {
            checkServer = new ServerSocket(TESTPORT);
            System.out.println("Applikasi 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("Horas") == 0) {
                os.writeBytes("Horas 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();
           
        }
    }
}

Tidak ada komentar:

Posting Komentar