PDA

View Full Version : [JAVA3D] Comunicazione online


franksisca
11-05-2010, 15:46
ok, ora che ho il mio mondo 3d, con i miei bei personaggi, devo farli comunicare tra loro, ma non ho idea di come fare.


avevo pensato (essendo 1v1) ad uno dei due client che funge anche da server, apre il mondo e dà il suo ip, l'altro client si collega, e poi si scambiano info con il server, tramite socket.

la domanda è, come li gestisco???
o meglio, è il server che si deve occupare degli spostamenti o sono i 2 player che se ne devono occupare???

suggerimenti o progeti simili che implementano questa cosa???
(ricordo che c'era un doom sviluppato interamente in java...ma non lo trovo)

lupoxxx87
11-05-2010, 18:50
a logica...dovrebbe esserci un server che riceve le informazioni e calcola gli spostamenti degli oggetti nel tuo mondo, e due client che visualizzano quanto accaduto.

puoi può succedere che uno dei due client collassi sul server, divenendo un server-client...però io lo farei così

franksisca
17-05-2010, 09:20
a logica...dovrebbe esserci un server che riceve le informazioni e calcola gli spostamenti degli oggetti nel tuo mondo, e due client che visualizzano quanto accaduto.

puoi può succedere che uno dei due client collassi sul server, divenendo un server-client...però io lo farei così

ma, ovviamente, devo usare i datagrampacket giusto?

sono a corto di programmazione online...però a rigor di logica devo usare una connessione udp.

hai/avete qualche tutorial diversi da quelli sun che mi sto leggendo ora?

franksisca
29-05-2010, 10:32
riuppo questa per non aprirne un'altra.

ho approntato le classi per la gestione della comunicazione online, alle quali passero il labiritnto, però per ora ho problemi sulla comunicazione.

mi aiutate pls :D

posto l'elenco delle classi

Classe Server
package online;

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



class ServerThread

package online;
import java.io.*;
import java.net.*;
import java.util.*;


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

public ServerThread() throws IOException {
super("ServerThread");
}

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("192.168.1.243");
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();
}

}


classe QuoteServerThread

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[256];

// 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 Client
package online;

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


public class Client {

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

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

DatagramPacket packet;

// get a few quotes
for (int i = 0; i < 5; i++) {

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

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

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

}




dovendoli farli girare momentaneamente sulla stessa macchina, credo che il problema sia sulle porte e sull'ip...consideratte che in questo momento mi trovate altamente rinco...quindi magari ho scritto qualche castroneria!!!


cmq, la mia domanda è:

ip e porta sono configurati correttametne?