On October 23rd, 2014, we updated our
By continuing to use LinkedIn’s SlideShare service, you agree to the revised terms, so please take a few minutes to review them.
Introduction Computers running on the Internet communicate with each other using TCP or UDP protocols 1. TCP/IP IP stands for Internet protocol, it’s a datagram protocol, which means transmitted packets of information are not guaranteed to be delivered. IP is a connectionless protocol. Since IP packets are never guaranteed to arrive at their destination, a higher level protocol TCP is used to provide a service that guarantees delivery
Application that require a reliable point to point channel to communicate use TCP to communicate. Examples Hypertext Transfer Protocol (HTTP), File Transfer Protocol(FTP) , and Telnet. User Datagram Protocol(UDP) UDP is protocol that send independent packets of data called datagram from one computer to another with no guarantee about the arrival. UDP is not connection based like TCP, rather it sends independent packets of data called datagrams from one application to another.
Sockets Sockets are software interfaces that connect an application to a network. On the Internet each machine has 65536 addressable ports it can use. Server programs listen to these ports for any incoming service request. A client program needs to open a port of its own before it can connect to a server port at the other end. The port numbers range from 0 to 1023 are restricted. They are reserved for use by well known services such as HTTP and FTP
Internet Addressing * Every computer on the Internet has an address. * An Internet address is a number that uniquely identifies each computer on the Net. * Originally, all Internet addresses consisted of 32-bit values, also known as IPv4 (Internet Protocol, version 4). * IPv6 uses a 128-bit value to represent an address, so it supports a much larger address space writing, * IPv6 is not supported by all environments.
There are 32 bits in an IPv4 IP address, and we often refer to them as a sequence of four numbers between 0 and 255 separated by dots (.). Domain Naming Service (DNS) * Refer to the addresses as numbers, is difficult, like http://126.96.36.199/ . * So it is mapped to name www.osborne.com that makes it easy to remember.
Java and the Net
How Java relates to all of these network concepts.
Java support both TCP/IP and UDP to communicate over the
URL, URLConnection, Socket and Server Socket classes all
use the TCP.
DatagramPacket and DatagramServer classes use UDP to
communicate over the network.
* The InetAddress class is used to encapsulate both the
numerical IP address and the domain name for that address.
* The InetAddress class has no visible constructors.
* To create an InetAddress object, use one of the available factory
* Factory methods are merely a convention whereby static methods
This method returns an InetAddress for a host name
passed to it.
static InetAddress[ ] getAllByName(String hostName) throws UnknownHostException This method returns an array of InetAddresses that represent all of the addresses that a particular name resolves to. Example InetAddressTest.java
TCP Sockets There are two kinds of TCP sockets in Java. One is for servers, and the other is for clients. The ServerSocket class is designed to be a “listener,” which waits for clients to connect before doing anything. The Socket class is designed to connect to server sockets and initiate protocol exchanges. The creation of a Socket object implicitly establishes a connection between the client and server .
Example Whois.java * Opens a connection to a port on the InterNIC server * Sends the command-line argument down the socket, and then prints the data that is returned * InterNIC will try to look up the argument as a registered Internet domain name, then send back the IP address and contact information for that site.
URL The URL provides a reasonably intelligible form to uniquely identify or address information on the Internet. Every browser uses them to identify information on the Web. Examples of URLs are http://www.osborne.com/ and http://www.osborne.com:80/index.htm A URL specification is based on four components. The first is the protocol to use, separated from the rest of the locator by a colon (:). Common protocols are http, ftp,
The second component is the host name or IP address of the host to use. The third component, the port number, is an optional parameter, The fourth part is the actual file path. Java’s URL class has got methods that gives information about the URL. Example URLDemo.java
Communication with Remote Systems using TCP TCP is used to implement reliable, bidirectional, persistent connections between hosts on the Internet. The java.net package provide two classes that allow you to create two kind of sockets. The classes are Socket and ServerSocket. Socket class The Socket class is used to create the client sockets, is designed to connect to server socket and initiate the protocol exchange. When we create the Socket object it implicitly establishes the connection between the client and the server.
It is not possible to get the details of establishing that connection because the Socket class does not provide any method for this purpose. Socket (String hostName, int port) This constructor is used to create a client socket and connect the local host to the specified hostName and port. Socket (InetAddress ipAddress, int port) This constructor is used to create a client socket using a Pre-existing InetAddress object and a port. There should be two ports for all TCP connections: a port on the remote machine a port on local machine through which the client communicates.
ServerSocket class basically used to create the server sockets, is designed to be a listener, which wait for client to connect before doing anything. The ServerSocket class is used to create servers that listen for either Local or remote client to connect to them on standard ports. Constructors in ServerSocket class ServerSocket (int port) throws IOException This constructor is used to create the server socket on the specified port with default queue length of 50 Queue length Each server socket object is associated with queue length attribute which tell the system how many client Connection it can leave pending before it can simply refuse connection
ServerSocket (int port, int queueLength) throws IOException This constructor is used to create a server socket on the Specified port with a specified queue length.
Writing Client/Server using TCP To write the server program use the following steps 1.Create a server socket and begin listening. 2.Call the accept method to get new connection. 3.Create the input and output stream for the returned socket. 4.Conduct the conversation based on agreed protocol. 5.Close the client streams and socket. 6.Go back to step 2 or continue the step 7. 7.Close the server socket
To write the client use the following steps 1.Create the client socket connection. 2.Acquire the read and write streams for the socket. 3.Use the streams according to the server’s protocol. 4.Close the streams. 5.Close the socket.
Communicating with remote systems using UDP UDP cannot guarantee whether the sent or received datagrams reach at their destinations. UDP is mainly useful to broadcast low value information on a frequent basis, so that losing a communication does not effect the service. The java.net package provides two classes namely DatagramPacket And DatagramSocket to communicate with remote systems.
DatagramPacket class is used to implement a connectionless packet delivery service. This class define two constructors DatagramPacket(bytebuf, int length) This constructor is used for receiving data over a DatagramSocket. DatagramPacket(bytebuf, int length, InetAddress address, int port) This constructor is used for transmitting datagrams,this requires destination machine address and the port number apart from the buffer and the size parameters.
DatagramSocket class The DatagramSocket class provides the functionality for sending and receiving the datagrams. This class defines three constructors DatagramSocket() Construct a datagram socket and bind it to any available port on the local host machine. DatagramSocket(int port) Construct a datagram socket and bind it to the specified port on the local host machine. DatagramSocket(int port, InetAddress Iaddr) Construct a datagram socket and bind it to the specified local address.
Writing Client/Server System using UDP To write a server using UDP follow the following steps 1.Create the datagram socket on a specific port. 2.Invoke the receive() method to wait for incoming packets. 3.According to the agreed protocol respond to received packets. 4.Repeat step 2 or continue to step 5. 5.Close the datagram socket. Example UDPServer.java
To write a Client using UDP follow the following basic steps 1.Create the datagram socket on any available port. 2.Create the address to send data. 3.Send the data according to the Server’s protocol. 4.Wait for incoming data 5.Go back to step 3 or step 4 or go to step 6 6.Close the datagram socket. Example UDPClient.java