PBO Pert 9-10: Network Programming

Digram blok Jaringan komputer

Persiapan

  • Mahasiswa membuat kelompok yang terdiri dari 4-5 orang
  • Mahasiswa menyiapkan perangkat komputer dan jaringannya
  • Mahasiswa menginstall sebuah web server

Aktivitas

  1. Dosen memberikan pengantar tentang pemrograman jaringan
  2. Dosen memberi contoh program client-server sederhana
  3. Mahasiswa menjalankan server Apache
  4. Mahasiswa mencoba mengakses server melaui browser
  5. Mahasiswa membuat program dengan Socket untuk mengetahui port berapa saja yang saat ini terbuka di server
  6. Mahasiswa mencoba membuat dan menjalankan program client-server
  7. Mahasiswa membuat laporan hasil pembelajaran

Materi

  1. Pengantar pemrograman jaringan
  2. Protocol
  3. Port
  4. Socket
  5. ServerSocket
  6. Datagram

Sumber

  1. All About Socket
  2. Xampp

Contoh Kode

BacaServer.java

package exp2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class BacaServer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Socket s = null;
		try {
	s = new Socket(InetAddress.getByName("localhost"), 80);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		PrintWriter pw = null;
		try {
			pw = new PrintWriter(s.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		pw.print("GET / HTTP/1.1");
		pw.print("Host: stackoverflow.com");
		pw.flush();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new 
                          InputStreamReader(s.getInputStream()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String t;
		try {
			while((t = br.readLine()) != null) 
                        System.out.println(t);
			br.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

 

DateServer.java

package exp2;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class DateServer {

    /**
     * Runs the server.
     */
    public static void main(String[] args) throws IOException {
        ServerSocket listener = new ServerSocket(9999);
        System.out.println("Server Tanggal runing....");
        try {
            while (true) {
                Socket socket = listener.accept();
                try {
                    PrintWriter out =
                      new PrintWriter(socket.getOutputStream(),true);
                    out.println("Tanggal Server: "+new Date().toString());
                } finally {
                    socket.close();
                }
            }
        }
        finally {
            listener.close();
        }
    }
}

 

EchoServer

EchoServer.java

EchoClient

EchoClient.java

This entry was posted in PBO, Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *