�������� ������������ � Java � ���� 3

�����: ������� �����,

�������� ����������� ���. ������� ��������

Web-site: http://www.nakov.com

�������� �������: 10.06.2002

������ � UDP ������

� ����������� ���� ��������� ��� �� ����������� Java ����������, ����� �� ����������� ���� TCP ������. ����� ����� �� ������� ���� �� ��������� ����, TCP � ��������, ����� ��������� �������� ���������� �������-����������� �������������� �����. � ���� ���� �� �� �������� ��� ����������, ����� ����������� Java �� ���� �� ����������� ���� �������� UDP ������.

����� �����, ���������� UDP ��������� ��������� � ���������� �� �������� ������ � �����, ������������ �� ����� �� � �����������. ������ �����, �� �� ���������� ������ ����� ����� ���������, ����� ����������� �� ����� ��, UDP �������� ��-����� ������ ������ ��������� TCP � ������ ��������� ��-������ �������. ��� ���� �� �� ��������, ������ ������ �� �� �������� ����� �� ������ � ���������� ���� �� ����� ���������, �� �� � �������� �� ��������� �� ��������� � ����� ������ ��� ��� ����� �� ������� ���������� � �����. ����� ����, ����� ���� �����, ��������� ��������� �� ���� UDP ����� �� ��������� �� ��� �� ��������� ��� ��� �� ��� �������� ��� UDP ������, �� �� ���������� � ����� ���, � ����� �� ���������. ��� ���� ����� �� �� ����� ������� �� �� �������� ����������� �� UDP, ������ �� �� ������� ���� ���� �������� � ��������.

� Java �� ����������� �� UDP ������ ����������� � ��������� java.net.DatagramSocketjava.net.DatagramPacket. ������ DatagramSocket �� ���� ���������� �� �� �������� ��� ��������� ������ ��������� � ����, �� ��������� ������ � �� ���������� ������. ������ DatagramPacket ������������ ��������� �� �����, ����� ������ ���� UDP �����. �� ����������� ������������ �� ���� ������� ���� ���� ������. ���� �������� ������������ UDPDataServer, ����� �������� �� ��������� UDP ������ �� ������� �� �������� ����:

import java.net.*;

 

public class UDPDateServer {

��� public static void main(String[] args)

��� throws Exception {

������� DatagramSocket socketIn =

����������� new DatagramSocket(12345);

������� DatagramSocket socketOut =

����������� new DatagramSocket();

������� System.out.println(

����������� "UDP Date Server started.");

������� while (true) {

����������� // Receive � request from a client

����������� byte[] requestBuf = new byte[256];

����������� DatagramPacket packetIn =

��������������� new DatagramPacket(requestBuf,

������������������� requestBuf.length);

����������� socketIn.receive(packetIn);

����������� int len = packetIn.getLength();

����������� String request =

��������������� new String(requestBuf, 0, len);

 

����������� // Send � response to the client

����������� if (request.startsWith("PORT=")) {

��������������� int port = new Integer(request.

������������������� substring(5)).intValue();

��������������� String resp = new java.util.

������������������� Date().toString();

��������������� byte[] respBuf =

������������������� resp.getBytes();

��������������� DatagramPacket packetOut =

������������������� new DatagramPacket(respBuf,

����������������������� respBuf.length,

����������������������� packetIn.getAddress(),

����������������������� port);

��������������� socketOut.send(packetOut);

����������� }

������� }

��� }

}

����� �� ����� �� ����, �������� ������ ��� UDP ������ � ���� �� ���� 12345 �� ���������� �� UDP ������ � ��� ���� �� ������� ����, ���� ����� �� ������� ���������� �� ���������. ���� ���� � �������� ����� �������� ��������� ������, ���� ����� �� �� �� �������� 256 �����, ������� �� ��� IP ������ � �����, ������ �� ������� �������, ������� ������� (�������� ���, �������� �������� ���� � ���) � �� ������� �� �������. IP ������� �� ������� �� ����� �� ������, �� ����� � ����� ������ ��� ��������, � ������ �� ����� �� ������ �� ����������� ������, ����� ������ �� � ��� ������ �PORT=������. ����� ��������� �� �������������� � UDP ������ � Java �, �� ���� ����� �� ����� DatagramSocket ���� �� �� �������� ��� ���� �� ����������, ��� ���� �� ��������� �� UDP ������. ��� ���� ������ �������� �����, � ���� ���� ������� �������, ��� ������ �� ������� ��� ������, � ����� �� �������� ����, ����� � � ����� ������.

������ ���� ���� ������� ������ �� ����� ������. ��� ������ �� ������� �� ������� ������, � ����� �� ������ �� ��� ���� �� �� ������ �������� � ���� ���� ������ ���� �������, �� �� ��������. ������� ��, ������������ �� ������� �� � ����������� � ������ �������� �� ������ �� � ���������� ����� �����. ����� ������� ������ � �� �� �������� �� ������� �������� �� ����� ����, �� ����� ��� ��������� ��������. �� ���� ������ ���� �������� �������, ������ ���� �� ��� ����� �� ��������� �����, �� ����� �� ���������� ��������. �������� �� ����� �� �� ����� �� ������, � ����� � ������� ��������. �� �������� ���� ������ � ������, ������ ����� ���� ����������, �� ����� �� ���������� ���� � ��� ����� �� ����� DatagramSocket ��� �� ���������, ��� �� ���������� �� ������. ��� ���������� ��� DatagramSocket ������ � ���� �� ��������� � ���� �� ����������, �� ����� �� �� �������� �� ��������� ���� � ��� ����, ������ ����������� �� DatagramSocket ����� ������� �������� ����. ��� �� ������� ���� ����������� �� �������� �� ��������� �����, �� ����� �� � ��������� � ������� �� �������� �� ������� �� ���� �� �������, ��� ���������� ���� �������, � ����� ��� �� ��� ��������� �� �������, ��������� ���� �� � ���������� � �� �� ��������. ����� �� ����� � ���� ������ �� ������. ��� ���� ����� �� ���������� �������� � ��������� ��� UDP ����������� �� �� �� �������� �������. ��� � �������� ���������� �� ������ �� ����� ������:

import java.net.*;

 

public class UDPDateClient {

��� public static void main(String[] args)

��� throws Exception {

������� // Open an UDP socket for the response

������� DatagramSocket socketIn =

����������� new DatagramSocket();

 

������� // Send request to the UDP Date Server

������� DatagramSocket socketOut =

����������� new DatagramSocket();

������� String request = "PORT=" +

����������� socketIn.getLocalPort();

������� byte[] requestBuf = request.getBytes();

������� DatagramPacket packetOut =

����������� new DatagramPacket(requestBuf,

����������� requestBuf.length,

����������� InetAddress.getByName("localhost"),

����������� 12345);

������� socketOut.send(packetOut);

������� socketOut.close();

 

������� // Receive the server response

������� byte[] responseBuf = new byte[256];

������� DatagramPacket packetIn =

����������� new DatagramPacket(responseBuf,

��������������� responseBuf.length);

������� socketIn.setSoTimeout(5000);

������� socketIn.receive(packetIn);

������� int len = packetIn.getLength();

������� String response =

����������� new String(responseBuf, 0, len);

������� System.out.println(response);

������� socketIn.close();

��� }

}

����� �� ����� �� ����, �������� ������ UDP ����� �� ���������� �� ������� �� �������, �������� ���� �����, ���� ������� � ���� ���� ����, ���� ����� ������ ����� ����� � ������� ���� ���� ����������� ����� �� ����� localhost:12345, ������ �� ������ �� � ��������� �������. ���� ���� ���� �� ������� 5 ������� � ��� �� ���� ����� ������ ����� � �������, �� ��������� �� ���������, � � �������� ������ �������� ����������.

 

Multicast ������

�������� �� ������ ���� ����� �� ���� �������� �� ����� ����������. ���������� ���� ���������� �� ����������, ����� �� �������, �� ����� �� ��������� ���� ������, �.�. �� �� �� ��������� ������������� �� ���. �� ������ �� ������ ���� �����������. ����������� � ��� �� ��������� �� ������� ����� � ����� �������� ���� �� ���������� ��������� � �������� �� ������ ����� ���� �� ������� ���������. �� ����������� ��� ����� �� ����� �� ��������� ����� ��������� �� ������ ���������. ���� ������ �������������� ������� ���� �� �� ������� �� ������� �����: ��������� �� ����� �������� ������ TCP ����� �� ��������� ������, ���� ����� ������� ������ ���������. �������� ���� �� ����� �������� ��� ��� ����� ���������� � ������ ������ ��������� ������������� �� ����� �����, �� �������� �� ������ ����� �������. �� ������� �����, ������ � ��������� ������, ������ ������� ���� �� �� ������� � ���� UDP ������. ��� ������ ������������, �����, ����������� ������ �� ����� �� �� ������� ��� �� ������, �� ����������� �����. ����������� �� ��� ��������� ����� �� ����������� �� ����� ����, � ����� �������� ��������� ���������� �� ���� ������ ������������� �������, ������ ��� ������� �������, ���� ����� �� ��� ������� � ������ ����� � ������� ������� ��������� � �������. �������� ������������� � ������, � ������ �� ���� �� ������� ���� �� � �����. �� ���������� �� ������ �������� �� ����������� multicast ��������. �� ����� �������� �� UDP ������, �� ��� ��� ��� ��� ������� �������� � ��������� �� �����, ��������� �� ��������� �� �����, ��������� �� �����. ����� ����� �� ������������ � �������� IP ����� � ��������� �����. ���� �������� ���� �� � ������������ � ����� �����. ����������� �� ����� �� ������ ������� �� ����� ����� ���� �� ������� ���� ����� �� IP ������ �� �������. �������� � multicast ����� ����� ������� �� �������� � UDP ������. �� ���������� ���� ������ � ��������� ����������, ����� �� ������� �� ������� 224.0.0.1 � ��������� �� ������ ����� ���������, �������� � ���� ����� �� ���� 2002.

import java.net.*;

 

public class MulticastListener {

��� public static void main(String[] args)

throws Exception {

������� InetAddress multicastAddr =

����������� InetAddress.getByName("224.0.0.1");

������� byte[] buf = new byte[1024];

������� DatagramPacket packet = new

����������� DatagramPacket(buf,buf.length);

������� MulticastSocket multicastSocket =

����������� new MulticastSocket(2002);

������� multicastSocket.joinGroup(

����������� multicastAddr);

������� while (true) {

����������� multicastSocket.receive(packet);

����������� String msg = new String(

��������������� packet.getData(),

��������������� 0, packet.getLength());

����������� System.out.println(msg);

������� }

��� }

}

����� ������� �� ����, �������� � multicast ������ ������ �� � ������. ��������� �� multicast �����, ��������� ������ joinGroup(), � ����� �� ��������� �� ����� multicast ����� � ���� ���� ���������� � ����� UDP ��������, ������������� �� ���� ����� � �������� ����. K�����, ����� ����������� java.net.MulticastSocket, ��� ������� ������� ������ � joinGroup() �� �������������� ��� multicast �����, leaveGroup() �� ��������� �� multicast �����, getTTL()setTTL() �� ��������� � ������� �� ���������� TTL (time to live). ����� �� ������� ���� ���������, ������ �� �� ������� ���������, �� ����� ������ multicasting-�. ��������� ���������� �� ��������� (multicasting) �� ������ �� ��������� IGMP (Internet Group Management Protocol). ���� � ������ ��������, ���� �� ��������� ��������� TCP/IP, �� ����� ��������� � ������� ���� �� ��������� ������ ��������. Multicast ����������� �� ��������������� �� ������� ����� ��� ��� �� IGMP ������. ��� ��������� ��������� �� ������� ���� ������ ����� �� ���������� ���� ���������������� � ��������� �����. ������������� ���� ���� ������������� �������� ���������� TTL � �������. ����������� �� �������� 0 ���������� ����������������� �� ������. ���������� TTL �������� ����������� ���� ��������������, ���� ����� ����������� ���� �� �������. �� �������� � ������������� �� IP ��������� ������������ � ���������� ��������� ���� �� ������ �� 224.0.0.1 �� 239.255.255.255, ����� �� ������������� �� multicasting. ����� �� ���� ������ �� ����� �� ���� ��������� �� ����� �� multicast �����. ����������� �� multicast ������ ����� �� IP ����� �� �������, �� � ������� �� ���� ����� ���� �� ��� 65535 �������� ������, ������������� �� ���������� �������� ������ �� �������. ������ �����, �� �� ����������������� IGMP �������� �� ����� �������� ������� � ������� �� IP ����, ���� ����� �� ���������� �� ��������� �� ����� ����������� � ������������ ��������� � ����������� ��-���� �� ���������, ����� ����������� ��-���� (� ��������� ������ � TCP ��� UDP �������� �����������). ����������� �� ���� ����� �� ������ �������� ��������� �� ����� multicast ����� ������ ����� ������� �����, ������� ����������� �� ���� ����� �� ���� ��������. ���� ��������, �� ����������� �� ���� UDP ��������� �� ����� ���������� �� multicast ����� ���� �� � ������ ���� ��-����� ��������� ����������� �� ������ ��������� �� ������ ����� ���������� ���� TCP ��� UDP. ������ ���� multicasting-�� � ����������� ����� �� ������ � ������� ����� �� ����������, ��� ����� �������� ���������������� � �� ������� �������. ����� � �� �����, �� multicasting-�� � ������� � ����� ���������� �� ��������� �����, ������ ����� �� � �������� �� ��������. ���� �� ���-��������� ���������� �� ���� ��� ����������� � ��� ����������� �� �������� �� �������. ��� ��� multicast-����� ����� ����� �� �������� �� ������������� �� ������� ����� ���������, ����� ������� � �������, ������ �������� ������� �� ��������������� � �������� ����� ���������� �� ��������� �� ���� �����. ����� �������� �������� �� ��� �����, ��������� ������������ ����� �� ��������, ����� ������� ������ ���� ���� ����. ��������� �� ���� ��� ������� ���������� � ������������ (scalability) � ����������� �� ������� (fault tolerance). �������������� �� ������� �� ����, �� ����� ���������� �� ���������� ������� ���������, ����� ������ ������� ����� ��-����� ��������� ���� ������������� �������� � ���� �� ����������� �� ����� ��������, ��������� ����������� ������������������ �� ���������. ������������� �� ������� ��������� ���������� �� ���������, ���� �� ��� ���� �� ���������� � �������� �� ������� ��� �������� �� ���� �� ��������, ���������� �� ������ �������� ������ � ��������� �� ��������� �� �� �������� �� ���������. �� �� �� �������� ����, � ���������� � ������� �� ���� ������� ���������� �� �� ������� ���������, �.�. ����� �� ��� �� ��������� � �������� �����, � ����� ���������� � ����������, �� �� ���� ��� ���������� ������� �� ����� �� ��� �� ���� ������ �� �����. �� ��������������� �� ���� �����, ������ �� ������ � ������� �����, ���-����� �� ��������� multicast ������, ������ ��������� �� �������. ����� ���� ��� ��� ������ �������������� �� ��������� ������. �� ����� � ���������� ����������� � ������������ �� �������� � ������� �� ���� �� ����� �� ����� �����.

���� ���� ��������� � ���� ����� ����� � multicasting � �� ����� �� ��������, ���� �� ������� ���� ������ �� �������, � ����� ����������.

������ import java.net.*;

 

public class MulticastSender {

��� public static void main(String[] args)

��� throws Exception {

������� InetAddress multicastAddr =

����������� InetAddress.getByName("224.0.0.1");

������� MulticastSocket multiSock =

��� ������� new MulticastSocket();

������� multiSock.joinGroup(multicastAddr);

������� while (true) {

����������� String message = "Hello " +

��������������� new java.util.Date();

����������� DatagramPacket packet =

��������������� new DatagramPacket(

������������������� message.getBytes(),

������������������� message.length(),

������������������� multicastAddr, 2002);

����������� multiSock.send(packet);

����������� Thread.sleep(1000);

������� }

��� }

}

����� �������, ����� ����� ������� �� ���, ����� �������� UDP ������. ������������ ������� �, �� ������ ����� DatagramSocket �������� ����� MulticastSocket � ����� �� �������� ������������ �������� �������, ��� ����� �� �������. �������� ��������� �������� � �������� ����� ����� ���������, ��������� �������� ����, � ��� �� ����������� ������������ �� multicast ��������. �� �� �������� ������� � �������, �� � ��������� �������� ������� � ������� �����. �� �������� ��� ������� ����� � ����� �������� �� �������. ���-����� � ��� ����� ������� ��������� � ������ �� ����� �� ��� ������� ������� � ���� ������. ���� ����� ������ �� �������� ������ ��������� ������, � ����� ������ �� ����� �� ������ �������. ��� ������� ����� �� �� ������� � ��� multicast �����.

 

������ � URL �������

��� �� ���������, ������ � ���� ������������� ���� �� ������ �� ������, Java �� ����� � ���� ��, � ����� �������� �����������. ����������� �� ������� �� �������� � Java ���� �� ���� ������������ �����, ��� ���������� ����� java.net.URL. ���� ����� ������� ����� URL. ����� ��� ���������� ������, �� ��������� �� ������� ���� � ��������� ����� (�������, ����, �����, ����� � �.�.), ���� � � ��������, � ������� �� ���������� ������������� ������� World Wide Web (WWW), �� ������ �� ������� ������ � ��������� �����. �������� ��� WWW �� ������� URL. URL � ���������� �� Uniform Resource Locator � ������ ����� �� ������ � ��� ������� ������:

protocol://host[:port]/[resource]

Protocol � ����������, �� ����� � �������� ��������, �������� http, ftp � ��. Host � IP ������� ��� ����� �� ��������, �� ����� � �������� ��������, �������� www.nakov.com ��� 208.185.127.162. Port � �������������� ����, ����� ������ ������ �� ����a �� ��������, �������� � ������ host, �������� 80 ��� 8080. Resource � ������� ��� �� ������� ������, ���� �� ������� � ���� �� ����. ��� �� � �������, �� �������� �������������� �� ������. �������� ��� ��������, ����� ������ �� �������� � http://www.nakov.com/english/CV.html, ���������� � http, host-�� � www.nakov.com, ������ �� � ������� � �� ���������� �� � ����������� �� http � 80, � �������� � english/CV.html, ���� english/ � ����� �� �������, � CV.html � ����� ��.

��� ����������� ���������� URL ������ �� ������ �� �������� ������� � ��������. ������ ���������� ���������� Web-�������, ��� ����� � ������ �� ����� URL-�� �� �����, ����� ������ �� ������� � ��������� ������ �� ��������� �� ��������� �����. ������ ������� � Java �������� URL ���� ��������� ����� �� ����� java.net.URL, �������� ������ �� �������, �� ����� �� ������������ � �� ��������� ����������� ���������� ������ �����. ���� ����������� ���� � ���� ����� ������ � ��������, ����� ������� ��������� http://www.nakov.com/english/CV.html �� ��������� �� ����������� �����:

import java.net.*;

import java.io.*;

 

public class URLReaderExample {

��� public static void main(String[] args)

��� throws Exception {

������� URL cv = new URL(

������� "http://www.nakov.com/english/CV.html");

������� BufferedReader in = new BufferedReader(

����������� new InputStreamReader(

��������������� cv.openStream()));

������� String line;

������� while ((line = in.readLine()) != null)

����������� System.out.println(line);

������� in.close();

��� }

}

����� � ������ ������ � ���� � ������� �� ����� �� ����� URL, ���� �� �� ������ ������ �� �������, �� ����� ������ �� ��������� ������, ���� ���� �� ������ ������ ����� �� ������ �� ���� ������ � ������ openStream() � �� ���� ����� �� ������� ����� ������ ��� �� ���. ������� ��, ���������, �� �������� � ������� �������� � ������ �� ����� � ������� �����. ��� �������� �� �������� ������, ����� �� � �������, �������� ��������, �������� �� �� ����� � ������� �����.

������ URL, ������ � ����� URLConnection ����� �� �� ��������� �� ���� �� ������ �� �������, �� � �� ������ � �������. ��� ������ � ������ �� �������, ����� ���������� ���� ������ �� ������� ���������� �� ��� ����������, �������� �������� ���������, �� ����� � �������� ���� ������. ��� ���� ������ ��� �� ����� ����:

import java.io.*;

import java.net.*;

 

public class ReverseExample {

��� public static void main(String[] args)

��� throws Exception {

������� String s = URLEncoder.encode(

����������� "Svetlin Nakov");

������� URL url = new URL(

��� "http://java.sun.com/cgi-bin/backwards");

 

������� // Send request

������� URLConnection connection =

����������� url.openConnection();

������� connection.setDoOutput(true);

������� PrintWriter out = new PrintWriter(

����������� connection.getOutputStream());

������� out.println("string=" + s);

������� out.close();

 

������� // Retrieve response

������� BufferedReader in = new BufferedReader(

����������� new InputStreamReader(

��������������� connection.getInputStream()));

������� String line;

������� while ((line = in.readLine()) != null)

����������� System.out.println(line);

������� in.close();

��� }

}

� ������� �� ����������� ������ �� �������� http://java.sun.com/cgi-bin/backwards, �� ����� �� ������ ���� �������� ���, � �� �� ����� � ������� ���. ���� ������ � ����������� ���� ���������� CGI script, ����� �� ������� � ���������. ������ �������� ������ �� ��������� http, � ���������� ������, ����� � �� ������� �� �� ������ �������� http ���������, ���� �� �� ���� ���������� �� ���. �� ����� �� �������� ����� URLEncoder, � ����� ����������� ��� ������� �� ������� �� ��������� �� URL. ���� ���� �� ������ ������� ����� ��� ��������, ������� � �� ���������� ���������, ����� �� ������ ������ ����� � �� ������� ���������� �� ����. �� ������ � URL �� �������� ����� URLConnection, ����� �� ����� �� �������� � ������ openConnection().

����� ���������� ������� ��������� �� ������ ��� UDP ������, multicast ������ � URL ������ �� �������� � �������������� �� Java 2 Standard Edition 1.4 �� ����� �� Sun � http://java.sun.com/j2se/1.4/docs/api/index.html, � ���� � �� ����� �� ����� ��������� ������������ � Javahttp://inetjava.sourceforge.net.