Socket
Arsitektur Client Server
Karakteristik Client-Server
• Client dan Server merupakan item proses (logika) terpisah yang
bekerja sama pada suatu jaringan komputer untuk mengerjakan
suatu tugas
• Service
• Shared resource
• Asymmetrical Protocol
Karakteristik Client-Server
• Transparency Location
• Mix-and-match
• Message-based-exchange
• Encapsulation of service
• Scalability
• Integrity
Socket
• Socket adalah sebuah abstraksi perangkat lunak yang digunakan
sebagai suatu "terminal" dari suatu hubungan antara dua mesin
atau proses yang saling berinterkoneksi
Operasi Socket
Socket dapat melakukan operasi:
• Koneksi ke mesin remote
• Mengirim data
• Menerima data
• Mentutup koneksi
• Bind to a port
• Listen pada data yang masuk
• Menerima koneksi dari mesin remote pada port tertentu
Di tiap mesin yang saling berinterkoneksi, harus terpasang socket.
Socket API pada Java
• Pada J2SE telah disediakan paket [Link] yang berisi kelas- kelas
dan interface yang menyediakan API (Application Programming
Interface):
• level rendah (Socket, ServerSocket, DatagramSocket)
• level tinggi (URL, URLConnection)
• Disimpan pada package [Link].*
InetAddress class
• Kelas ini digunakan untuk mengambil informasi IP suatu komputer.
Kelas ini bersifat static dan tidak memiliki konstruktor.
• Method-methodnya adalah :
• getByName(namahost)
untuk menampilkannya gunakan method toString()
• getLocalHost
• getAllByName(namahost)
• Kemungkinan error: UnknownHostException
Informasi Antar muka Jaringan
Untuk mendapatkan informasi network interface pada Java telah
terdapat kelas NetworkInterface yang mampu mendapatkan
informasi tentang antar muka jaringan, nama device, dan IP yang ter-
bind. Nama device misalnya eth0, lo0
Contoh: [Link]
Connection-oriented &
Connectionless
Socket
• A socket programming construct can make use of either the UDP
(User Datagram Protocol) or TCP (Transmission Control Protocol).
• Sockets that use UDP for transport are known as datagram
sockets, while sockets that use TCP are termed stream sockets/TCP
sockets.
TCP vs UDP
• TCP
– Connection-oriented
– Reliable
– Stateful
• UDP
– Connectionless
– Unreliable
– Stateless
Java Socket & ServerSocket class
• Untuk Socket Connection Oriented Programming
– This means that the connection between server and client remains open
throughout the duration of the dialogue between the two and is only broken (under
normal circumstances) when one end of the dialogue formally terminates the
exchanges (via an agreed protocol)
• Kelas [Link] digunakan oleh Server untuk listen koneksi
• Kelas [Link] digunakan oleh Client untuk inisialisasi koneksi.
• Setelah client terkoneksi ke server dengan menggunakan Socket, maka
ServerSocket akan mengembalikan status server ke client melalui koneksi
• yang terbentuk sebelumnya.
The [Link] Class
• Connection is accomplished through the constructors. Each Socket
object is associated with exactly one remote host. To connect to a
different host, you must create a new Socket object.
• public Socket(String host, int port) throws
UnknownHostException, IOException
• public Socket(InetAddress address, int port) throws IOException
• public Socket(String host, int port, InetAddress localAddress, int
localPort) throws IOException
• public Socket(InetAddress address, int port, InetAddress
localAddress, int localPort) throws IOException
lanjutan
Sending and receiving data is accomplished with output and input
streams. There are methods to get an input stream for a socket and
an output stream for the socket.
public InputStream getInputStream() throws IOException
public OutputStream getOutputStream() throws IOException
• There's a method to close a socket:
public void close() throws IOException
The [Link] Class
The [Link] class represents a server socket. It is
constructed on a particular port. Then it calls accept() to listen for
incoming connections.
– accept() blocks until a connection is detected.
– Then accept() returns a [Link] object that is used to perform the
actual communication with the client.
• public ServerSocket(int port) throws IOException
• public ServerSocket(int port, int backlog) throws IOException
• public ServerSocket(int port, int backlog, InetAddress bindAddr) throws
IOException
• public Socket accept() throws IOException
• public void close() throws IOException
Prinsip ServerSocket
• Create a ServerSocket object.
– ServerSocket servSock = new ServerSocket(1234);
• Put the server into a waiting state.
– Socket link = [Link]();
• Set up input and output streams.
– Scanner input = new Scanner([Link]());
– PrintWriter output = new
PrintWriter([Link](),true);
Prinsip ServerSocket lanjutan
• Send and receive data.
[Link]("Awaiting data...");
String input = [Link]();
• Close the connection (after completion of the dialogue).
– [Link]();
Prinsip Socket (client)
• Establish a connection to the server.
– the server's IP address (of type InetAddress);
– the appropriate port number for the service.
Socket link = new Socket([Link](),1234);
• Set up input and output streams.
– Scanner input = new Scanner([Link]());
– PrintWriter output = new
PrintWriter([Link](),true);
Prinsip Socket (Client) lanjutan
• Send and receive data.
– The Scanner object at the client will receive messages sent by
the PrintWriter object at the server,
– while the PrintWriter object at the client will send messages
that are received by the Scanner object at the server (using
methods nextLine and println respectively).
• Close the connection
UTS
• Buatlah Aplikasi Chating dengan menggunakan Openfire dan Spark