next up previous contents
Next: 3.14 Dimnik Up: 3 Aplikativne naloge Previous: 3.12 Podatkovna baza

3.13 Java razredi za komunikacijo s streznikom Hylafax

Za streznik cad.lecadnet s TCP portom st: 4559 obstaja enostaven protokol kot tudi izvorna koda s primerom komunikacije. Za neodvisnost od platforme se lahko uporabi jezik Java, ki ima vgrajeno mrezno komunikacijo. Tako je mozno odpreti mrezno datoteko in poslati podatke strezniku, ki sprejete podatke interpretira in poslje datoteko naprej do streznika faksov. Primer programa, ki uporablja paketni protokol UDP za odpiranje vrat v laboratoriju LECAD je naslednji:

// Import the package names used.
import java.io.*;    
import java.net.*;
import java.util.*;
import java.text.*;

/**
 * This is an application to obtain the GATE
 * opening  from
 * remote system via UDP and.
 * @author Leon  Kos
 * @date jan 1998
 * @version 0.1
 */
public class gate {

private static final int GATE_PORT = 8909;

 // A socket to send and receive datagrams.
 DatagramSocket GATESocket = null;
 // An array of addresses to the machines
 private InetAddress remoteMachine;

 /**
  * This method starts the application.
  * @param args Command line arguments
  */
 public static void main(String[] args) {
    if (args.length < 1) {
       System.out.println(
          "Usage: gate host");
       System.exit(1);
    }

    gate door = new gate(args[0]);

    System.out.println("Vrata odprta.");
    System.exit(0);            // Exit.
 }

 /**
  * The constructor looks up the remote 
  * hosts and creates a UDP socket.
  * @param hosts The hosts to contact.
  */
 public gate(String host) {
  try {
    remoteMachine = InetAddress.getByName(host);
   } catch(UnknownHostException excpt) {
      remoteMachine = null;
       System.err.println("Unknown host " +
                 host + ": " + excpt);
   }

 try {
     GATESocket = new DatagramSocket();
 } catch(SocketException excpt) {
   System.err.println(
     "Unable to bind UDP socket: " 
       +  excpt);
     System.exit(1);
  }
    // Perform the UDP communications.
    openGate();
 }

 public void openGate() {
   DatagramPacket GATEQuery; 
   DatagramPacket response;  
   byte[] buffer;            
   int datagramsSent = 0;    

    // Send out a small UDP datagram,
    // asking it to respond with its GATE.

   if (remoteMachine != null) {
     try {
       buffer = new byte[30];
       String from = unknown@" +
       InetAddress.getLocalHost().getHostName();
       buffer = from.getBytes();

       GATEQuery = new DatagramPacket(
            buffer, buffer.length,
            remoteMachine, GATE_PORT);
         GATESocket.send(GATEQuery);
         datagramsSent++;
     } catch(IOException excpt) {
       System.err.println(
         "Unable to send to " +
         remoteMachine + ": " + excpt);
     }
    }

    // Close the socket.
    GATESocket.close();
    GATESocket = null;
}
Pomoc pri izdelavi in programiranju nudi Janez Vrhovec.



Leon Kos
Tue Mar 24 18:24:32 CET 1998