Witam
Chcę przesyłać pomiędzy klientem dane za każdym razem gdy kliknę na planszy w jakiś JButton.
Problem polega na tym że nie wiem w którym miejscu w wywoływać funkcje z klienta i z serwera żeby po kliknięciu w przycisk serwer wysyłał do tego samego klienta nowe dane.
Poniżej zamieszczam fragmenty kodu. Prosze o pomoc.
Tutaj mam pobieranie tych danych które chce wysłać
public static int r = -1;
public static int c = -1;
@Override
public void actionPerformed(ActionEvent a) {
//Object s = ((JButton)a.getSource()).getText();
Object s = a.getSource();
for (int row = 0; row < przy.length; row++) {
for (int col = 0; col < przy[row].length; col++) {
if (przy[row][col] == a.getSource()) {
r = row;
c = col;
}
}
}
send = new Ruch(r,c,licznik%2);
To moja klasa z serwerem
public void run() {
System.out.println("Sender Start");
try {
ssChannel = ServerSocketChannel.open();
ssChannel.configureBlocking(true);
int port = 12345;
ssChannel.socket().bind(new InetSocketAddress(port));
send();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println("Connection ended");
}
public static void send(){ // <----------------------------------- Nie wiem gdzie wywoływać ta funkcje by serwer cały czas wysyłał dane gdy klikne w JButton
while (true) {
try {
SocketChannel sChannel = ssChannel.accept();
ObjectOutputStream oos = new
ObjectOutputStream(sChannel.socket().getOutputStream());
oos.writeObject(TestFrame.send);
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
No a tak odpalam ten serwer
Thread starter = new Thread(new Sender());
starter.start();
A o to mój klient który odbiera :
public class Klient {
public SocketChannel sChannel;
public Klient()
throws IOException, ClassNotFoundException {
System.out.println("Receiver Start");
SocketChannel sChannel = SocketChannel.open();
sChannel.configureBlocking(true);
if (sChannel.connect(new InetSocketAddress("localhost", 12345))) {
ObjectInputStream ois =
new ObjectInputStream(sChannel.socket().getInputStream());
read(ois); // <<--------------------------- nie wiem gdzie to wywoływać żeby aktoalizować odbieranie
}
System.out.println("End Receiver");
}
public static void read(ObjectInputStream d) throws ClassNotFoundException, IOException{
Ruch s = (Ruch)d.readObject();
int x = s.getX();
System.out.println("String is: '" + x + "'");
}