Dialogue Server / Client

cs_Kaktus Messages postés 8 Date d'inscription dimanche 7 décembre 2003 Statut Membre Dernière intervention 20 juin 2004 - 20 juin 2004 à 19:18
wargre Messages postés 649 Date d'inscription mardi 8 juin 2004 Statut Membre Derniè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;

public Server() throws IOException
{
ServerSocket serverSocket = null;
boolean listening = true;
threadNb = 1;

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++;
}

String name1 = thread1.getAlias();
thread3.send("B" + name1 + "\n");
String name2 = thread2.getAlias();
thread4.send("B" + name2 + "\n");
String name3 = thread3.getAlias();
thread1.send("B" + name3 + "\n");
String name4 = thread4.getAlias();
thread2.send("B" + name4 + "\n");

thread1.setThreads(thread1, thread2, thread3, thread4);
thread2.setThreads(thread1, thread2, thread3, thread4);
thread3.setThreads(thread1, thread2, thread3, thread4);
thread4.setThreads(thread1, thread2, thread3, thread4);

thread1.go();
thread2.go();
thread3.go();
thread4.go();
}
}

SERVER - Class SERVERTHREAD

import java.net.*;
import java.io.*;

public class ServerThread extends Thread {

private Socket socket = null;
private int threadNb;
private ServerThread thread1, thread2, thread3, thread4;

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);
}
}

}
out.close();
in.close();
socket.close();

} catch (IOException e) {
e.printStackTrace();
}
}

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 void setThreads(ServerThread thread1, ServerThread thread2, ServerThread thread3, ServerThread thread4)
{
this.thread1 = thread1;
this.thread2 = thread2;
this.thread3 = thread3;
this.thread4 = thread4;
}

public void go()
{
//
}
}

CLIENT - Class THREADSOCKET

import java.net.*;
import java.io.*;

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;
}
}

1 réponse

wargre Messages postés 649 Date d'inscription mardi 8 juin 2004 Statut Membre Dernière intervention 9 septembre 2004 7
21 juin 2004 à 08:47
Deja y'a quoi comme erreur?

Sinon substring(0,0) ca renvoie toujours rien!!!!
0
Rejoignez-nous