Introduction to Java Programming and Data Structures 2020 Ch33a
Introduction to Java Programming and Data Structures 2020 Ch33a
Objectives
To explain the terms: TCP, IP, domain name, domain
name server, stream-based communications, and packet-
based communications (§33.2).
To create servers using server sockets (§33.2.1) and
clients using client sockets (§33.2.2).
To implement Java networking programs using stream
sockets (§33.2.3).
To develop an example of a client/server application
(§33.2.4).
To obtain Internet addresses using the InetAddress class
(§33.3).
To develop servers for multiple clients (§33.4).
To send and receive objects on a network (§33.5).
To develop an interactive tic-tac-toe game played on the
Internet (§33.6).
tambahan materi Untuk membuat aplikasi yang dapat mengirim data melalui jaringan komputer kita membutuhkan suatu
interface yang disebut socket.
Dengan soket, aplikasi dapat dengan mudah mengirim atau menerima data dari dan/atau ke aplikasi
lainnya di jaringan komputer.
Dengan menggunakan soket, kita tidak perlu memahami detail dari sistem jaringan komputer.
Dapat dianalogikan bahwa soket itu adalah sebuah pesawat telepon yang membantu kita untuk
menghubungi orang lain di tempat lain yang terhubung dengan jaringan telepon.
Pesawat telepon sebagai end point = soket adalah end point pada jalur komunikasi sebuah jaringan
komputer.
Pesawat telepon membutuhkan nomor telepon + nomor ekstensi = soket membutuhkan alamat IP dari
komputer tujuan + nomor dari aplikasi (port3) atau service yang akan dituju.
3 Port dalam bahasa inggris berarti pintu, celah, atau lubang pada sistem yang ada di komputer atau mikro komputer yang berguna untuk jalur
transfer data. Jenis-jenis port pada sistem komputer terbagi menjadi dua yaitu :
Port Fisik, yaitu suatu slot, soket, atau tempat colokan, yang ada di Kompter, Laptop atau perangkat keras lainnya yang berfungsi
sebagai penghubung antara komputer dan pralatan input-output seperti: printer, mouse, keyboard, dan lain-lain.
Port Logikal (nonfisik), yaitu port yang digunakan untuk jalur transfer data dengan koneksi tanpa kabel dan biasa digunakan oleh
software-software.
Contoh port fisik:
port RJ-45
port power
port USB
port serial
port infrared
port VGA
port paralel
port HDMI
Berikan deskripsi proses pengiriman data yang ditunjukkan pada gambar diatas …
Dengan soket, aplikasi yang kita buat hanya berhubungan dengan layer transport saja. Protocol yang
dipilih dapat menggunakan protokol UDP atau TCP yang merupakan bagian dari layer transport.
Untuk menghubungi server, client harus mengetahui alamat IP dari server dan number port dari aplikasi
yang ada diserver. IP address untuk menuju ke alamat server, sedangkan number portnya digunakan
untuk menuju alamat aplikasi yang dijalankan pada server.
Dengan demikian, soket programming antara client server akan membutuhkan IP address, number port,
dan protokol TCP atau UDP.
Contoh dari number port: misalkan port 23 untuk layanan atau aplikasi telnet server, port 25 untuk mail
server, port 80 untuk web server. Jadi sebuah client web server akan menggunakan aplikasi dengan
number port 80.
Gambaran sebuah port: misalnya ada aplikasi pegadaian dengan nomor port 62 dipasang pada server.
Untuk menghubungi aplikasi pegadaian (server), maka client harus akses dengan nomor port yang
sama, yaitu aplikasi pegadaian (server) dengan port 62.
Contoh lainnya: aplikasi catur online dengan port 5000 dan diakses menggunakan web ada http dengan
port 80 dan chatting dg port 6000. (satu aplikasi dapat menggunakan beberapa port untuk menangani
berbagai jenis komunikasi jaringan)
Pembagian port di bagi menjadi 3 grup berdasarkan fungsinya, yaitu port well-known ports (0-1023),
registered ports (1024-49151), dan dynamic/private ports (49152-65535).
Di kelompok well-known ports, port sudah dipetakan penggunaannya, misalnya:
Port 21 (FTP): Digunakan untuk transfer file.
Port 22 (SSH): Digunakan untuk koneksi aman ke server.
Port 25 (SMTP): Digunakan untuk mengirim email.
Port 53 (DNS): Digunakan untuk menerjemahkan nama domain ke alamat IP.
Port 80 (HTTP): Digunakan untuk mengakses halaman web.
Port 110 (POP3): Digunakan untuk mengambil email.
Port 143 (IMAP): Digunakan untuk mengakses email.
Port 443 (HTTPS): Digunakan untuk mengakses halaman web secara aman.
Registered Ports (1024-49151) berisi daftar port ini IANA (Internet Assigned Numbers Authority) dan
dapat digunakan oleh aplikasi atau layanan tertentu.
Contohnya:
Port 3306 (MySQL): Digunakan oleh database MySQL.
Port 8080: Digunakan sebagai port alternatif untuk server web.
Dynamic/Private Ports (49152-65535): port-port ini digunakan untuk aplikasi yang tidak memiliki port
yang didedikasikan secara khusus. Port-port ini juga digunakan oleh klien untuk berkomunikasi dengan
server.
The two primary protocols at the transport layer are4:
Transmission Control Protocol (TCP), and
User Datagram Protocol (UDP).
TCP providing reliable, ordered, and error-checked delivery, whereas UDP support faster,
connectionless communication.
Other Transport Layer Protocols are (a) SCTP (Stream Control Transmission Protocol): Combines
features of both TCP and UDP, offering reliable and ordered data delivery with multiple streams, and
(b) DCCP (Datagram Congestion Control Protocol): A protocol designed to control congestion in
datagram-based networks.
4 https://www.geeksforgeeks.org/transport-layer-protocols/
Tahapan komunikasi menggunakan socket
Processes running on different machines communicate with each other by sending messages into
sockets. We said that each process was analogous to a house and the process's socket is analogous to a
door. As shown in Figure 21, the socket is the door between the application process and TCP. The
application developer has control of everything on the application-layer side of the socket; however, it
has little control of the transport-layer side.5
Now let's to a little closer look at the interaction of the client and server programs. The client has the
job of initiating contact with the server. In order for the server to be able to react to the client's initial
contact, the server has to be ready. This implies two things.
First, the server program can not be dormant; it must be running as a process before the client attempts
to initiate contact.
Second, the server program must have some sort of door (i.e., socket) that welcomes some initial
contact from a client (running on an arbitrary machine). Using our house/door analogy for a
process/socket, we will sometimes refer to the client's initial contact as "knocking on the door".
With the server process running, the client process can initiate a TCP connection to the server. This is
done in the client program by creating a socket object. When the client creates its socket object, it
specifies the address of the server process, namely, the IP address of the server and the port number of
the process. Upon creation of the socket object, TCP in the client initiates a three-way handshake and
establishes a TCP connection with the server. The three-way handshake is completely transparent to
the client and server programs.
During the three-way handshake, the client process knocks on the welcoming door of the server
process. When the server "hears" the knocking, it creates a new door (i.e., a new socket) that is
dedicated to that particular client. In our example below, the welcoming door is a ServerSocket object
that we call the welcomeSocket. When a client knocks on this door, the program invokes
welcomeSocket's accept() method, which creates a new door for the client. At the end of the
handshaking phase, a TCP connection exists between the client's socket and the server's new socket.
Henceforth, we refer to the new socket as the server's "connection socket".
From the application's perspective, the TCP connection is a direct virtual pipe between the client's
socket and the server's connection socket. The client process can send arbitrary bytes into its socket;
TCP guarantees that the server process will receive (through the connection socket) each byte in the
5 http://www2.ic.uff.br/~michael/kr1999/2-application/2_06-sockettcp.htm
order sent. Furthermore, just as people can go in and out the same door, the client process can also
receive bytes from its socket and the server process can also send bytes into its connection socket. This
is illustrated in Figure 22.
Buatlah iilustrasi yang menunjukkan tahapan terjadinya three-way handshake pada koneksi TCP antara
client – server !
Buatlah iilustrasi yang menunjukkan tidak terjadinya handshake pada koneksi UDP antara client –
server !
TUGAS
Jawablah pertanyaan (ada 7) yg diberi tanda warna kuning pada materi diatas!