Download presentation
Presentation is loading. Please wait.
Published byVictor Patrick Modified over 6 years ago
1
Network Programming in Java CS 1111 Ryan Layer May 3, 2010
2
Vocabulary Client Server Packet switched network Address DNS Ports
Sockets Protocols
3
Echo client import java.net.Socket; public class Client {
public static void main(String[] args) throws Exception { String host = args[0]; int port = Integer.parseInt(args[1]); Socket socket = new Socket(host, port); In in = new In(socket); String reply = in.readLine(); System.out.println(reply); in.close(); socket.close(); }
4
Hello World server import java.net.Socket;
import java.net.ServerSocket; public class Server { public static void main(String[] args) throws Exception { int port = Integer.parseInt(args[0]); ServerSocket serverSocket = new ServerSocket(port); while (true) { Socket clientSocket = serverSocket.accept(); Out out = new Out(clientSocket); out.println("Hello World!"); out.close(); clientSocket.close(); }
5
Network Stack Internet Application Transport Network Link
WoW, HTTP, ftp, ... Transport TCP, UDP, ... Network IP, ... Application WoW, HTTP, ftp, ... Internet Link device driver Transport TCP, UDP, ... Network IP, ... Link device driver
6
Network Stack Internet
7
Ports app app app port port port IP Addr
Connection = (local addr, local port, remote addr, remote port) server p o r t port client
8
Ports Connection = (local addr, local port, remote addr, remote port)
host 5 port b (5, b, 1, a) port a host 1 (1, a, 5, b) (1, b, 4, a) (1, c, 4, b) port b port c (4, a, 1, b) (4, b, 1, c) (4, b, 2, a) (4, b, 3, a) host 4 port a port b port a host 2 (2, a, 4, b) port a host 3 (3, a, 4, b)
9
Putting it All Together
Sender Receiver msg app app app Application src port, dst port src port, dst port msg msg Transport src IP, dst IP src IP, dst IP src port, dst port src port, dst port msg msg Network src IP, dst IP src port, dst port msg src MAC, dst MAC src IP, dst IP src port, dst port msg src MAC, dst MAC Link Internet
10
What You Need to Remember:
Servers listen on ports Clients connect and address and a port Transport layer protocol (TCP, UDP) matters Write to / read from sockets to communicate
11
Application Protocols, HTTP
telnet 80 GET /index.html HEAD: Asks for the response identical to the one that would correspond to a GET request, but without the response body. GET: Requests a representation of the specified resource. POST: Submits data to be processed to the identified resource. PUT: Uploads a representation of the specified resource. DELETE: Deletes the specified resource. TRACE: Echoes back the received request. OPTIONS: Returns the HTTP methods that the server supports. CONNECT: Converts the request connection to a transparent TCP/IP tunnel. PATCH: Is used to apply partial modifications to a resource.
12
Vocabulary Client Server Packet switched network Address DNS Ports
Sockets Protocols
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.