May 5, 2015

Client Server Menggunakan Java Programming
Afriduarchan3:12 PM 0 komentar


Pada praktikum Labti ini saya menggunakan java programming dalam penggunaan client dan server. dan dijalankan menggunakan aplikasi Neatbens.berikut adalah code yang digunakan.


Main.java (Clientapp)

package clientapp;
import java.io.*;
import java.net.*;

public class Main {
    public static void main(String[] args) throws IOException {
        Socket client = null;       
        InputStream in = null;
        OutputStream out = null;
        byte[] receiveMsg =  new byte[64];
        try {
            client = new Socket("Afriduarchan-PC", 8080);
            in = client.getInputStream();
            out = client.getOutputStream();
        } catch (UnknownHostException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        } catch (IOException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        String fromServer;
        String fromUser;
        in.read(receiveMsg);
        fromServer = new String(receiveMsg);
        System.out.println("Server: " + fromServer);

        fromUser = "Salam dari client";
        System.out.println("Kirim ke server: " + fromUser);
        out.write(fromUser.getBytes());
        fromUser = "Ini data dari client";
        System.out.println("Kirim ke server: " + fromUser);
        out.write(fromUser.getBytes());
        fromUser = "Keluar";
        out.write(fromUser.getBytes());
        System.out.println("Kirim ke server: " + fromUser);           
        out.close();
        in.close();

        client.close();}}

Main.java(Serverapp)
package serverapp;
import java.io.*;
import java.net.*;
public class Main {
    public static void main(String[] args) throws IOException {
        ServerSocket server = null;
        Socket client = null;
        byte[] receiveBuf = new byte[64];
        int recvMsgSize;       
        try {
            server = new ServerSocket(8080);
            System.out.println("Server started");
            client = server.accept();
            System.out.println("Client connected");           
        } catch (IOException e){
          System.out.println(e.getMessage());
          System.exit(-1); }
        InputStream in = client.getInputStream();
        OutputStream out = client.getOutputStream();
        String data;
        data = "Salam dari server";
        out.write(data.getBytes());
        java.util.Arrays.fill(receiveBuf, (byte)0);
        while (true) {
            recvMsgSize = in.read(receiveBuf);
            data = new String(receiveBuf);
            if (data.trim().equals("Keluar")){
                out.write(data.getBytes());
                break;
            }
            java.util.Arrays.fill(receiveBuf, (byte)0);
            System.out.println("Client: " + data);
        }
        out.close();
        in.close();
        client.close();
        server.close();}}

dan hasil dari output program diatas adalah sebagai berikut:
- ini adalah output dari server 


-ini adalah output dari client


demikian sedikit berbagi ilmu mengenai pemrograman jaringan.semoga bermanfaat. GBU


In Category : ,
About The Author Afriduarchan Network Engineer, College Student of Informatics engineering
Facebook and Twitter

0 komentar

Post a Comment