Networking
-
Upload
tuan-ngo -
Category
Technology
-
view
611 -
download
0
Transcript of Networking
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
Data transfer Ordered data transfer Retransmission of lost packets Error-free data transfer Flow control Congestion control
Java provides the java.net package to perform system-independent network communication
Networking basics A computer generally has a single physical connection available for the network.
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.
URL Uniform Resource Locator a reference or an address to a resource on Internet. Two parts:
URL Resource name contains: Host name File name Port number
What different? http://vn.yahoo.com:80/index.html http://vn.yahoo.com/index.html http://vn.yahoo.com/
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)
Methods of URL public String getHost() public String getFile() public String getPath() public final InputStream openStream()
Methods of InetAddress public String getHostAddress() public String getHostName() public static InetAddress getLocalHost() public static InetAddress getByName(String host)
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.
URLConnection Cannot instantiate a URLConnection obj directly. Create URLConnection obj by method openConnection() of URL class . openConnection() . getInputStream() . getOutStream()
Socket one end-point of a two-way communication link between two programs running on the network
Socket Socket(String host, int port) UnknowHostException OIException
Socket Retrieve the InputStream from the socket Retrieve the OutputStream from the socket Read from the InputStream Write the OutputStream
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
ServerSocket Steps: . Initiate ServerSocket obj . accept() . OutputStream . InputStream
UDP – User Datagram Protocol sent message, referred to as datagrams, to other host
UDP – User Datagram Protocol Unreliable Not ordered Lightweight No congestion control
UDP Time-sensitive applications often use UDP VoIp Online games
DatagramPacket create a datagram packet. Constructors: DatagramPacket(byte[], int) DatagramPacket(byte[], int, InetAddress, int) buf, length, address, port
DatagramSocket represents a socket for sending and receiving datagram packets. Constructors: DatagramSocket(int) DatagramSocket(port, InetAddress)
Methods of DatagramSocket public void send(DatagramPacket p) public void receive(DatagramPacket p)
Methods of DatagramPackets public InetAddress getAddress() public int getPort() public byte[] getData() public void setData(byte[] data) public void setPort(int port)