Chapter Reseau
Chapter Reseau
Chapter Reseau
Socket Programming
Lecturer: Sivadon Chaisiri
Mathematics, Statistics and Computer Department
Faculty of Science, Ubon Rajathanee University
Layered Protocols
Client
46770 Server
Virtual Circuit 25 80
(through network)
Client
48335
Socket ServerSocket
Streams
}
}
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());
Any Questions?