Professional Documents
Culture Documents
Chapter03 1216733257635944 8
Chapter03 1216733257635944 8
Lecturer: Sivadon Chaisiri Mathematics, Statistics and Computer Department Faculty of Science, Ubon Rajathanee University
Chapter 3
Layered Protocols
Metadata in a Messages
Protocols
Application = HTTP, FTP, SMTP, NSF, Telnet, SSH, ECHO, Presentation = SMB, NCP, Session = SSH, NetBIOS, RPC, Transport = TCP, UDP, Network = IP, ICMP, IPX Data link = Ethernet, Token Ring, ISDN, Physical = 100BASE-T, 1000BASE-T, 802.11
Distributed System (1104451)
4
Ports
A port is a special number present in the data packet. Ports are typically used to map data to a particular process running on a computer (i.e., which process associates with the data determining by port number) IANA is responsible for assigning TCP and UDP port numbers to specific used.
Well-known ports (0-1023) Registered ports (1024-49151) Dynamic and/or Private ports (49152-65535)
Distributed System (1104451)
7
Socket Application
A socket is a connection between two hosts (endpoints). A socket can perform 7 basic operations.
Connect to a remote machine Send data Receive data Close a connection Bind to a port Listen for incoming data Accept connections from remote machines on the bound port
Distributed System (1104451)
10
Endpoint-to-Endpoint Communications
25
80
Client 48335
11
Methods:
Methods:
Socket accept() void close() void setSoTimeout(int timeout)
13
Socket 46770
ServerSocket
80
14
Streams
Filters of Streams
16
public class SocketInfo { public static void main(String[] args) throws Exception { Socket socket = new Socket("www.sanook.com", 80); System.out.println("Connected to " + socket.getInetAddress() + " on port " + socket.getPort() + " from port " + socket.getLocalPort() + " of " + socket.getLocalAddress()); } }
Distributed System (1104451)
17
Server-Side
Bind ServerSocket object with a specified port Listening for the incoming requesting Accept connection while listened to an incoming contact and get its Socket reference Send/Receive data via streams of the Socket Object
Distributed System (1104451)
18
20
21
import java.io.*; import java.net.*; public class BusyHelloServer { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(12345); while(true) { Socket socket = server.accept(); DataInputStream dis = new DataInputStream(socket.getInputStream()); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); String name = dis.readUTF(); System.out.println("I see " + name); for (int i = 0 ; i < 10 ; i++) { Thread.sleep(1000); System.out.println("Delay for " + name + " #" + i); } dos.writeUTF("Hello " + name); socket.close(); } } Distributed System (1104451) }
23
import java.io.*; import java.net.*; public class ThreadingHelloServer extends Thread { Socket soc; public ThreadingHelloServer(Socket soc) {this.soc = soc; } public void run() { try { DataInputStream dis = new DataInputStream(soc.getInputStream()); DataOutputStream dos = new DataOutputStream(soc.getOutputStream()); String name = dis.readUTF(); System.out.println("I see " + name); for (int i = 0 ; i < 10 ; i++) { Thread.sleep(1000); System.out.println("Delay for " + name + " #" + i); } dos.writeUTF("Hello " + name); soc.close(); } catch(Exception ex) {ex.printStackTrace();} } public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(12345); while(true) { Socket socket = server.accept(); new ThreadingHelloServer(socket) .start(); } } Distributed System (1104451) }
24
The End
Any Questions?