Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

Chapter 3

Socket Programming
Lecturer: Sivadon Chaisiri
Mathematics, Statistics and Computer Department
Faculty of Science, Ubon Rajathanee University
Layered Protocols

Distributed System (1104451) 2


Metadata in a Messages

Distributed System (1104451) 3


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


TCP/IP four-layer model

Distributed System (1104451) 5


IP, TCP, and UDP
□ IP (Internet Protocol)
□ TCP (Transmission Control Protocol)
□ UDP (User Datagram Protocol) = Unreliable
communication, no ordering guarantee (e.g.,
DNS, TFTP, VoIP, …)

Distributed System (1104451) 6


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


Ports and Applications

Distributed System (1104451) 8


The Client-Server Model

Distributed System (1104451) 9


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

Client
46770 Server

Virtual Circuit 25 80

(through network)

Client
48335

Distributed System (1104451) 11


Java and Socket
□ java.net.Socket
Constructors:
Socket(InetAddress address, int port)
Socket(String host, int port)
Socket(InetAddress address, int port,
InetAddress localAddr, int localPort)
Methods:
InputStream getInputStream()
OutputStream getOutputStream()
void close()
InetAddress getLocalAddress()
int getLocalPort()
void setSoTimeout(int timeout)
Distributed System (1104451) 12
Java and Socket
□ java.net.ServerSocket
Constructors:
ServerSocket(int port)
Methods:
Socket accept()
void close()
void setSoTimeout(int timeout)

Distributed System (1104451) 13


Socket and ServerSocket

Socket ServerSocket

46770 Virtual Circuit 80


(through network)

Streams

Distributed System (1104451) 14


Streams

More stream classes


InputStream: DataInputStream, ObjectInputStream
OutputStream: DataOutputStream, ObjectOutputStream
Distributed System (1104451) 15
Filters of Streams

Distributed System (1104451) 16


Code: Socket Information
import java.net.Socket;

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


Steps to develop a socket app
□ Client-Side
□ Bind Socket object with a specified host&port then
connect to the host&port
□ [option] Send data via stream (from
getOutputStream)
□ Wait for a response of the host via getInputStream
□ 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
Code: Time Server
import java.io.DataOutputStream;
import java.net.*;
import java.util.Date;
public class TimeServer {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(7000);
System.out.println("Server is started");
while(true) {
Socket socket = server.accept();
DataOutputStream dos = new
DataOutputStream(socket.getOutputStream());
String time = new Date().toString();
dos.writeUTF(time);
socket.close();
}
}
}

Distributed System (1104451) 19


Code: Time Client
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

public class TimeClient {


public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost", 7000);
DataInputStream din = new
DataInputStream(socket.getInputStream());
String time = din.readUTF();
System.out.println(time);

}
}
Distributed System (1104451) 20
Code: Hello Server
import java.io.*;
import java.net.*;
public class HelloServer {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(12345);
System.out.println("Server is started");
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);
dos.writeUTF("Hello " + name);
socket.close();
}
}
} Distributed System (1104451) 21
Code: Hello Client
import java.io*;
import java.net.*;
public class HelloClient {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost", 12345);
DataInputStream din = new
DataInputStream(socket.getInputStream());
DataOutputStream dos = new
DataOutputStream(socket.getOutputStream());

String name = "World";


if (args.length > 0) name = args[0];
dos.writeUTF(name);
String message = din.readUTF();
System.out.println(message);
}
} Distributed System (1104451) 22
Code: Busy Hello Server
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.*;
Code: Multithread Hello Server
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
Code: Multithread Hello Server (Short Form)
import java.io.*; import java.net.*;
public class ThreadingHelloServer2 {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(12345);
System.out.println("Server is started");
while(true) {
final Socket socket = server.accept();
Thread t = new Thread() {
public void run() {
try {
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();
} catch(Exception ex) { ex.printStackTrace(); }
}};
t.start();
}}}
Distributed System (1104451) 25
The End

Any Questions?

You might also like