View Single Post
Old 09-06-2010, 12:41   #4
franksisca
Senior Member
 
L'Avatar di franksisca
 
Iscritto dal: May 2005
Città: Roma
Messaggi: 7938
classe client
Codice:
package online;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

import labirinti.Labirinto;

public class Client {

	public static void main(String[] args) throws IOException {

		MulticastSocket socket = new MulticastSocket(4446);
		InetAddress address = InetAddress.getByName("224.0.0.1");
		socket.joinGroup(address);

		DatagramPacket packet;

		byte[] buf = new byte[1024];
		packet = new DatagramPacket(buf, buf.length);
		socket.receive(packet);
		buf=packet.getData();
		

		boolean flag = true;
		while (flag) {

			String received = new String(packet.getData(), 0, packet
					.getLength());
			System.out.println("Quote of the Moment: " + received);
		}

		socket.leaveGroup(address);
		socket.close();
	}

}
classe quoteserverthread
Codice:
package online;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Date;




public class QuoteServerThread extends Thread {

    protected DatagramSocket socket = null;
    protected BufferedReader in = null;
    protected boolean altroTesto = true;

    public QuoteServerThread() throws IOException {
	this("QuoteServerThread");
    }

    public QuoteServerThread(String name) throws IOException {
        super(name);
        socket = new DatagramSocket(4444);

        try {
            in = new BufferedReader(new FileReader("one-liners.txt"));
        } catch (FileNotFoundException e) {
            System.err.println("Could not open quote file. Serving time instead.");
        }
    }

    public void run() {

        while (altroTesto) {
            try {
                byte[] buf = new byte[1024];

                    // receive request
                DatagramPacket packet = new DatagramPacket(buf, buf.length);
                socket.receive(packet);

                    // figure out response
                String dString = null;
                if (in == null)
                    dString = new Date().toString();
                else
                    dString = getNextQuote();
                buf = dString.getBytes();

		    // send the response to the client at "address" and "port"
                InetAddress address = packet.getAddress();
                int port = packet.getPort();
                packet = new DatagramPacket(buf, buf.length, address, port);
                socket.send(packet);
            } catch (IOException e) {
                e.printStackTrace();
		altroTesto = false;
            }
        }
        socket.close();
    }

    protected String getNextQuote() {
        String returnValue = null;
        try {
            if ((returnValue = in.readLine()) == null) {
                in.close();
		altroTesto = false;
                returnValue = "No more quotes. Goodbye.";
            }
        } catch (IOException e) {
            returnValue = "IOException occurred in server.";
        }
        return returnValue;
    }
}
classe server
Codice:
package online;

public class Server {
	public static void main(String[] args) throws java.io.IOException {
		new ServerThread(new ServerMakerLabirinto()).start();
	}
}

classe servermakerlabirinto
Codice:
package online;

import labirinti.Labirinto;
import labirinti.LabirintoSemplice;

public class ServerMakerLabirinto {

	private Labirinto labirinto;

	public ServerMakerLabirinto(){
		this.labirinto=new LabirintoSemplice();

	}
}
classe server thread
Codice:
package online;
import java.io.*;
import java.net.*;
import java.util.*;


public class ServerThread extends QuoteServerThread  {
	private long tempo = 5000;
	private ServerMakerLabirinto serverMakerLabirinto;

	public ServerThread(ServerMakerLabirinto serverMakerLabirinto) throws IOException {
		super("ServerThread");
		this.serverMakerLabirinto=serverMakerLabirinto;
	}

	public void run() {
		while (altroTesto) {
			try {
				byte[] buf = new byte[256];

				// construct quote
				String dString = null;
				if (in == null)
					dString = new Date().toString();
				else
					dString = getNextQuote();
				buf = dString.getBytes();

				// send it
				InetAddress group = InetAddress.getByName("224.0.0.1");
				DatagramPacket packet = new DatagramPacket(buf, buf.length,
						group, 4446);
				socket.send(packet);

				// sleep for a while
				try {
					sleep((long) (Math.random() * tempo));
				} catch (InterruptedException e) {
				}
			} catch (IOException e) {
				e.printStackTrace();
				altroTesto = false;
			}
		}
		socket.close();
	}

}
il funzionamento è "semplice":

il servermaker costruisce il labirinto e lo passa al server.

quando al server arrivano le richieste di 32 player (è un 1v1 il gioco) manda ad entrambi lo stesso labirinto, alchè i client costruiscono il proprio mondo 3d e mandano info continuamente al server che aggiorna il labirinto con le posizioni dei due pg e le rimanda ad entrambi.

credo che la strutura sia corretta, ma non sò come ricostruire il labirinto una volta arrivati sui client
__________________
My gaming placement
franksisca è offline   Rispondi citando il messaggio o parte di esso