3 - Module 2 - SocketProgramming
3 - Module 2 - SocketProgramming
3 - Module 2 - SocketProgramming
SOCKET PROGRAMMING
What is Networking
◼ When two processes lying on same or different
machines are communicating over the netword is
called networking
◼ For example: Client – server Communication
Request
Client Server
Response
Client
Server
Network
Client machine
Server machine
◼ There are four elements in networking:
◼ Client: sends request for servives
◼ Server: Sends (reply) response
◼ Network: media of communication
◼ Protocols: rules (language) for communication
Khoa CNTT – ĐH Nông Lâm TP. HCM 3/78
Single machine – Multiple clients
Google Microsoft Facebook
Server Server Server
Clients’ Machine
One machine can execute multiple clients processes concurrently
Khoa CNTT – ĐH Nông Lâm TP. HCM 4/78
Client is a Process
◼ One machine can run multiple clients (processes at a
time
◼ Client is not machine – it is a process:
◼ Web browser open google.com is a client
◼ Facebook messenger connected to chat server is a
client
◼ CuteFTP downloading a file from FTP server is a client
◼ All above mentioned clients (Browser, Messenger,
CuteFTP) run together on a single machine
concurrently
Server Machine
Oracle DB Server
(Port: 1521)
Well-Known Ports:
◼ 20,21: FTP
◼ 23: Telnet
◼ 25: SMTP
◼ 80: HTTP
◼ 110: POP3
◼ 1099: RMI
a socket
socket socket
API runtime Process A Process B API runtime
support support
socket socket
API runtime Process A Process B API runtime
support support
Client Server
- Client Task 1 initiates - Server Task 1
connection
- Client Task 2 - Server Task 2
- ..... - .....
- Client Task n Data Exchange - Server Task n
Khoa CNTT – ĐH Nông Lâm TP. HCM 23/78
Sockets and Ports (Diagram)
Socket Socket
Server
1.
2.
3.
bind(), listen()
accept()
write() read()
Data (requests)
read() write()
Data (reply - response)
Close() Close()
Server Client
InputStream inClient =
clientSoc.getInputStream();
OutputStream outServer =
serverSoc.getOutputStream();
Khoa CNTT – ĐH Nông Lâm TP. HCM 29/78
TCP Socket Programmning (3. step)
3a. serverSoc.close();
3b. clientSoc.close();
protocol
◼ 3. Step: Connection closing
5) Bind to a port
6) Listen for incoming data Server:
7) Accept connections from ServerSocket()
remote machines on the
bound port
1. Open a socket.
2. Open an input stream and output stream to
the socket.
3. Read from and write to the stream according
to the server's protocol.(
4. Close the streams.
5. Close the socket.
try {
Socket theSocket = new Socket("java.sun.com", 80);
InetAddress host = theSocket.getInetAddress( );
System.out.println("Connected to remote host " + host);
}
catch (UnknownHostException e) {System.err.println(e); }
catch (IOException e) {System.err.println(e); }
add = InetAddress.getByName(hostNameIP);
System.out.println("DNS host name: "+add.getCanonicalHostName());
System.out.println("IP Address: "+add.getHostAddress());
System.out.println("InetAddress toString: "+add);
◼ DatagramPacket packet =
◼ DatagramSocket clientSocket =
new DatagramSocket();
new DatagramSocket(port);
socket.close();
if (line.equals("end")) break;
packet.setData(buff);
packet.setLength(buff.length);
socket.receive(packet);
String received = new String(packet.getData(),
0, packet.getLength());
System.out.println(received);
}//while
socket.close();