Escolar Documentos
Profissional Documentos
Cultura Documentos
InetAddress
Socket
ServerSocket
Networking Classes and Interfaces
InetSocketAddress
SocketAddress
CookieHandler
SocketPermission
CookieManager
DatagramPacket
DatagramSocket
URL
URI
MulticastSocket
Networking Classes and Interfaces
The java.net packages interfaces are :
ContentHandlerFactory
DatagramSocketImplFactory
SocketOptions
CookiePolicy
FileNameMap
CookieStore
SocketImplFactory
URLStreamHandlerFactory
InetAddress
getLocalHost( )
getByName(String hostName)
getAllByName(String hostName)
InetAddress-Factory Methods
static InetAddress getLocalHost( )
throws UnknownHostException
Output (Of course, the output you see may be slightly different.)
vikash-desktop/127.0.1.1
gmail.com/216.58.220.37
www.gmail.com/216.58.220.37
InetAddress- Instance Methods
When we create a ServerSocket, it will register itself with the system as having an interest
in client connections.
The constructors for ServerSocket reflect the port number that we want to accept
connections on and, optionally, how long we want the queue for said port to be.
The queue length tells the system how many client connections it can leave pending before
it should simply refuse connections.
Constructors:
accept() - ServerSocket has a method called accept( ), which is a blocking call that will
wait for a client to initiate communications and then return with a normal Socket that is
then used for communication with the client.
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
bw.write(returnMessage);
System.out.println("Message sent to the client is "+returnMessage);
bw.flush();
}
}
catch (Exception e){
e.printStackTrace();
}
finally{
try{
socket.close();
}
catch(Exception e){}
Datagrams are bundles of information passed between machines.
Once the datagram has been released to its intended target, there is no assurance that it will
arrive or even that someone will be there to catch it.
when the datagram is received, there is no assurance that it hasnt been damaged in transit
or that whoever sent it is still there to receive a response.
Java implements datagrams on top of the UDP protocol by using two classes:
DatagramSocket
DatagramPacket(byte data[ ], int offset, int size, InetAddress ipAddress, int port)
transmits packets beginning at the specified offset into the data.
import java.io.*;
import java.net.*;
class UDPClient
{
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
IPAddress, 9875);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
import java.io.*;
import java.net.*;
class UDPServer
{
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(9875);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String( receivePacket.getData());
System.out.println("RECEIVED: " + sentence);
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
}
}
DatagramPacket defines several
methods
Uniform Resource Locator (URL)
The URL provides a way to uniquely identify or address information on the Internet.
URL class provides a simple, concise API to access information across the Internet
using URLs.
http://www.gmail.com:80/index.html
Protocol: http
Port: -1
Host: www.osborne
File: /downloads
Ext:http://www.osborne/downloads
Thus, given a URL object, we can retrieve the data associated with it.
To access the content information of a URL, create a URLConnection object from it, using its
openConnection( ) method,
openConnection( ) has the following general form:
URLConnection openConnection( ) throws IOException
URLConnection
Once we make a connection to a remote server, we can use URLConnection to inspect the
properties of the remote object before transporting it locally.