May 5, 2015

Client Server Menggunakan Java Programming
Afriduarchan3:12 PM 1 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

1 comment:

  1. Borgata Hotel Casino & Spa - MapYRO
    Address: 4 양산 출장샵 Borgata Way, 1 Borgata Way, Atlantic 거제 출장샵 City, NJ 08401. Phone: (609) 317-3360. 논산 출장샵 Find reviews, hours, directions,  Rating: 2.2 · ‎2,821 reviews · ‎Price range: $ (Based on Average Nightly Rates for 광명 출장샵 a Standard Room from our Partners)What are some of the property amenities at Borgata Hotel Casino & Spa?Which 군산 출장마사지 room amenities are available at Borgata Hotel Casino & Spa?

    ReplyDelete