cs_Kaktus
Messages postés8Date d'inscriptiondimanche 7 décembre 2003StatutMembreDernière intervention20 juin 2004
-
20 juin 2004 à 19:18
wargre
Messages postés649Date d'inscriptionmardi 8 juin 2004StatutMembreDernière intervention 9 septembre 2004
-
21 juin 2004 à 08:47
J'aimerai faire communiquer 4 PC avec un serveur.
Apparement, ça ne marche pas et je vois pas pourquoi...
Merci d'avance.
Voici les codes sources:
SERVEUR - Class SERVER
import java.net.*;
import java.io.*;
public class Server
{
final static int PORT = 5100;
public int threadNb;
public ServerThread thread1, thread2, thread3, thread4;
try {
serverSocket = new ServerSocket(PORT);
} catch (IOException e) {
System.err.println("Could not listen on port: 5100.");
System.exit(-1);
}
while (listening) {
switch (threadNb) {
case 1:
thread1 = new ServerThread(serverSocket.accept(), threadNb);
thread1.start();
case 2:
thread2 = new ServerThread(serverSocket.accept(), threadNb);
thread2.start();
case 3:
thread3 = new ServerThread(serverSocket.accept(), threadNb);
thread3.start();
case 4:
thread4 = new ServerThread(serverSocket.accept(), threadNb);
thread4.start();
listening = false;
serverSocket.close();
}
threadNb++;
}
public ServerThread(Socket socket, int threadNb)
{
super("ServerThread");
this.socket = socket;
this.threadNb = threadNb;
}
public void run()
{
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String input, output;
while ((input = in.readLine()) != null) {
String s0 = input.substring(0, 0);
if (s0 == "A") {
// int i = 1;
// String s;
// String name = new String("");
// while ((String s = input.substring(i, i)) != "\n") {
// name = name + s;
// i++;
// }
} else if (s0 == "F") {
String text = input.substring(0, 4);
switch (threadNb) {
case 1: thread3.send(text);
case 2: thread4.send(text);
case 3: thread1.send(text);
case 4: thread2.send(text);
}
} else if (s0 == "O") {
String text = input.substring(0, 4);
switch (threadNb) {
case 1: thread2.send(text);
case 2: thread1.send(text);
case 3: thread4.send(text);
case 4: thread3.send(text);
}
}
public void send(String text)
{
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(text);
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
public String getAlias()
{
send("A");
String name = new String("");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
for (int i = 1; i < 50; i++) {
String s = in.readLine().substring(i, i); if (s !"\n") {name name + s;}
else return name;
}
} catch (IOException e) {
e.printStackTrace();
}
return name;
}
public class ThreadSocket
{
private Socket socket;
private ChessEngine chessEngine;
public ThreadSocket(Socket socket)
{
this.socket = socket;
}
public void run()
{
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String input, output;
while ((input = in.readLine()) != null) {
String s0 = input.substring(0, 0);
if (s0 == "A") {
out.println("A" + chessEngine.getPlayer().getName() + "\n");
} else if (s0 == "B") {
for (int i = 1; i < 50; i++) {
String name = new String("");;
String s = in.readLine().substring(i, i); if (s !"\n") {name name + s;}
else chessEngine.getPlayer().setFriendName(name);
}
} else if (s0 == "F") {
String s1 = input.substring(1, 1);
String s2 = input.substring(2, 2);
String s3 = input.substring(3, 3);
String s4 = input.substring(4, 4);
if (s1 == "9") {
Piece piece = new Piece(chessEngine.getPlayer().getColor(), Integer.parseInt(input));
chessEngine.getPlayer().addPiece(piece);
}
} else {
String s1 = input.substring(1, 1);
String s2 = input.substring(2, 2);
String s3 = input.substring(3, 3);
String s4 = input.substring(4, 4);
if (s1 == "9") {
Piece piece = new Piece(Integer.parseInt(s2), (chessEngine.getPlayer().getColor()+1)%2);
int[] to = new int[] {Integer.parseInt(s3), Integer.parseInt(s4)};
chessEngine.getChessboard().setPiece(piece, to);
} else if (s1 == "8") {
//promotion
} else {
int[] from = new int[] {Integer.parseInt(s1), Integer.parseInt(s2)};
int[] to = new int[] {Integer.parseInt(s3), Integer.parseInt(s4)};
int[][] move = new int[][] {from, to};
chessEngine.movePiece(move);
}
}
}
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void setChessEngine(ChessEngine chessEngine)
{
this.chessEngine = chessEngine;
}
}