Professional Documents
Culture Documents
Java Networking
202 . 12 . 28 . 129
1 1 0 0 1 0 1 0 0 0 0 0 1 1 0 0 0 0 1 1 1 0 0 0 1 0 0 0 0 0 0 1
IP version 6 Addresses
(Inet6address)
• 128- bit unsigned integers
• Hexadecimal Notation:
3FFE:F200:0234:AB00:0123:4567:8901:ABCD
import java.net.*;
class InetAddressTest
{
public static void main(String args[]) throws UnknownHostException
{
InetAddress Address = InetAddress.getLocalHost();
System.out.println(Address);
Address = InetAddress.getByName("starwave.com");
System.out.println(Address);
InetAddress SW[] = InetAddress.getAllByName("www.nba.com");
for (int i=0; i<SW.length; i++)
System.out.println(SW[i]);
}
}
Basics of Networking (cont..)
• ContentHandler * DatagramPacket
• DatagramSocket * DatagramSocketImpl
• HttpURLConnection * InetAddress
• MulticastSocket *ServerSocket
• Socket *SocketImpl
• URL *URLConnection
• URLEncoder *URLStreamHandler
TCP/IP SOCKET PROGRAMMING
• The two key classes from the java.net package
used in creation of server and client programs
are:
– ServerSocket
– Socket
• A server program creates a specific type of
socket that is used to listen for client requests
(server socket).
• In the case of a connection request, the program
creates a new socket through which it will
exchange data with the client using input and
output streams.
TCP/IP SOCKET PROGRAMMING
ServerSocket class
• The java.net.ServerSocket class is used by server
applications to obtain a port and listen for client requests
• The ServerSocket class has four constructors:
SN Methods with Description
1 public ServerSocket(int port) throws IOExceptionAttempts to create a server
socket bound to the specified port. An exception occurs if the port is already bound
by another application.
2 public ServerSocket(int port, int backlog) throws IOExceptionSimilar to the
previous constructor, the backlog parameter specifies how many incoming clients
to store in a wait queue.
3 public ServerSocket(int port, int backlog, InetAddress address) throws
IOExceptionSimilar to the previous constructor, the InetAddress parameter
specifies the local IP address to bind to. The InetAddress is used for servers that
may have multiple IP addresses, allowing the server to specify which of its IP
addresses to accept client requests on
3 public void setSoTimeout(int timeout) Sets the time-out value for how long
the server socket waits for a client during the accept().
4 public void bind(SocketAddress host, int backlog) Binds the socket to the
specified server and port in the SocketAddress object. Use this method if you
instantiated the ServerSocket using the no-argument constructor.
Socket class
• The java.net.Socket class represents the socket that both the client
and server use to communicate with each other. The client obtains a
Socket object by instantiating one, whereas the server obtains a
Socket object from the return value of the accept() method.
w.println(msg);
}
r.close(); w.close(); sock.close(); serverSock.close();
}
}
Echo Client (Example)
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) {
try { Socket s = new Socket("127.0.0.1", 4444);
String line;
do { line = r.readLine();
if ( line != null )
System.out.println(line);
line = con.readLine();
w.println(line);
} while ( !line.trim().equals("bye") );
} catch (Exception err) { System.err.println(err); } } }
UDP SOCKET PROGRAMMING
• Datagram packets are used to implement a connectionless packet
delivery service supported by the UDP protocol.
• Each message is transferred from source machine to destination
based on information contained within that packet.
• That means, each packet needs to have destination address and
each packet might be routed differently, and might arrive in any
order.
• Packet delivery is not guaranteed.
• The format of datagram packet is:
| Msg | length | Host | serverPort |
• Java supports datagram communication through the following
classes:
– DatagramPacket
– DatagramSocket
UDP SOCKET PROGRAMMING
try {
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(
new InputStreamReader( clientSocket.getInputStream()));
String inputLine;
if (inputLine.equals("Bye."))
break;
}
out.close();
in.close();
clientSocket.close();
}
catch (IOException e) {
System.err.println("Problem with Communication Server");
System.exit(1); }
}
}
URL Class
• URLs identify a resource & its address information anywhere
on the Internet.
• URL is based on four components
– The protocol (e.g. http, ftp)
– Host name/ IP address of the host ( e.g. java.sun.com)
– Port number (optional. Default port for http is 80)
– The actual file path
– For example,
– http//ww.javatpoint.com:80/sonoojaiswal/
• A URL can be broken down into parts, as follows −
• protocol://host:port/path?query#ref
Commonly used methods of Java URL class
Method Description
System.out.println("Protocol: "+url.getProtocol());
System.out.println("Host Name: "+url.getHost());
System.out.println("Port Number: "+url.getPort());
System.out.println("File Name: "+url.getFile());
Output:
Protocol: http
}catch(Exception e){System.out.println(e);}
Host Name: www.svit.com
} Port Number: -1
} File Name: /java-tutorial
Example of Java URL class
import java.net.*;
import java.io.*;