Networking

26
Networking

Transcript of Networking

Page 1: Networking

Networking

Page 2: Networking

TCP connection-based protocol that provides a reliable flow of data between two computers. TCP is based on a four-layered model.

HTTP, FTP – High level protocol

TCP, UDP – Open, maintain connection

IP – Transmits packet from one IP add to another

IP – Physical equipment such as twisted pair cables

Page 3: Networking

Data transfer Ordered data transfer Retransmission of lost packets Error-free data transfer Flow control Congestion control

Page 4: Networking

Java provides the java.net package to perform system-independent network communication

Page 5: Networking

Networking basics A computer generally has a single physical connection available for the network.

Page 6: Networking

Port The physical connection is numbered within a range of 0 to 65535, called Ports. Ranges from 0 to 1023 are reserved for HTTP, FTP … Data transmitted is accompanied with the destination address and the port number.

Page 7: Networking

URL Uniform Resource Locator a reference or an address to a resource on Internet. Two parts:

Page 8: Networking

URL Resource name contains: Host name File name Port number

Page 9: Networking

What different? http://vn.yahoo.com:80/index.html http://vn.yahoo.com/index.html http://vn.yahoo.com/

Page 10: Networking

URL Constructors: URL(String url) URL(String protocol, String host, String file) URL(String protocol, String host, int port, String file) URL(URL baseURL, String relativeURL)

Page 11: Networking

Methods of URL public String getHost() public String getFile() public String getPath() public final InputStream openStream()

Page 12: Networking

Methods of InetAddress public String getHostAddress() public String getHostName() public static InetAddress getLocalHost() public static InetAddress getByName(String host)

Page 13: Networking

URLConnection Abstract class represents the communication link between an application and a URL. Used to read from and to write to the resource referenced by the URL.

Page 14: Networking

URLConnection Cannot instantiate a URLConnection obj directly. Create URLConnection obj by method openConnection() of URL class . openConnection() . getInputStream() . getOutStream()

Page 15: Networking

Socket one end-point of a two-way communication link between two programs running on the network

Page 16: Networking

Socket Socket(String host, int port) UnknowHostException OIException

Page 17: Networking

Socket Retrieve the InputStream from the socket Retrieve the OutputStream from the socket Read from the InputStream Write the OutputStream

Page 18: Networking

ServerSocket represents the server side. If the ServerSocket successful in binding to a port, I can then wait and listen for client request public ServerSocket(int port) throws IOException

Page 19: Networking

ServerSocket Steps: . Initiate ServerSocket obj . accept() . OutputStream . InputStream

Page 20: Networking

UDP – User Datagram Protocol sent message, referred to as datagrams, to other host

Page 21: Networking

UDP – User Datagram Protocol Unreliable Not ordered Lightweight No congestion control

Page 22: Networking

UDP Time-sensitive applications often use UDP VoIp Online games

Page 23: Networking

DatagramPacket create a datagram packet. Constructors: DatagramPacket(byte[], int) DatagramPacket(byte[], int, InetAddress, int) buf, length, address, port

Page 24: Networking

DatagramSocket represents a socket for sending and receiving datagram packets. Constructors: DatagramSocket(int) DatagramSocket(port, InetAddress)

Page 25: Networking

Methods of DatagramSocket public void send(DatagramPacket p) public void receive(DatagramPacket p)

Page 26: Networking

Methods of DatagramPackets public InetAddress getAddress() public int getPort() public byte[] getData() public void setData(byte[] data) public void setPort(int port)