Java

DX Cluster in Java

La classe ClusterHost

package model;

/**
 * Classe per l'host
 * @author Lorenzo IZ0KBA
 *
 */
public class ClusterHost {
	
	private String name;
	private final String host;
	private final int port;
	
	public ClusterHost(String host, int port) {
		this.host = host;
		this.port = port;
	}
	
	/**
	 * Restituisce il valore dell'hostname
	 * @return
	 */
	public String getHost() {
		return host;
	}
	
	/**
	 * Restituisce il valore della porta
	 * @return
	 */
	public int getPort() {
		return port;
	}
	
	public String toString() {
		return host + " port: " + port;
	}

}

La classe DxClient

package model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Observable;

public class DxClient extends Observable {
	
	private ClusterHost host;
	private Socket s = new Socket();
	private BufferedReader s_in;
	private PrintWriter s_out;
	private static final String myCall = "IZ0KBA";
	
	public DxClient(ClusterHost clusterHost) {
		this.host = clusterHost;
	}
	
	public void run() throws IOException {
		System.out.println("Tentativo di connesione a: " + host);
		try {			
			s.connect(new InetSocketAddress(host.getHost(), host.getPort()));
			s_out = new PrintWriter(s.getOutputStream(), true);
			s_in = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
			s_out.println(myCall);
		} catch (final IOException e) {
			System.err.println("Impossibile connettersi all'Host " + host);
		} finally {
			System.out.println("Connessione stabilita!");
		}
		
		String response;
		while ((response = s_in.readLine()) != null) {
			for (String line : response.split("\n")) {
				System.out.println(line);
			}
		}
		
	}

	public static void main(String[] args) throws IOException {
		DxClient dxc = new DxClient(new ClusterHost("dxfun.com", 8000));
		dxc.run();
	}

}

Lascia una risposta

Il tuo indirizzo email non sarà pubblicato.