(Sockets) Serveur / Client(s)
Bonjour à tous,
J'ai un projet à faire en utilisant un serveur, on nous fournit une libraire (ici : http://www.pps.jussieu.fr/~mathias/t..._2010/Deug.tgz ) et on doit faire communiquer client(s) et serveurs.
Le problème est pour la communication Serveur -> Clients
Voilà mes fichiers :
(Versions jar :
(Serveur : http://bokobok.free.fr/Projet/download.php?fileid=30
(Client : http://bokobok.free.fr/Projet/download.php?fileid=29
Serveur :
- Serveur_Main
-- Serveur_Thread
--- Serveur_Thread_Envoi
--- Serveur_Thread_Reception
Client :
- Client_Main
-- Client_Thread
--- Client_Thread_Envoi
--- Client_Thread_Reception
Serveur_Main :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package Serveur;
import deug.DeugConsole;
public class Serveur_Main{
public static void main(String[] args) {
DeugConsole console;
for(int i=1;true;i++){
console = new DeugConsole();
Serveur_Thread.Serveur_Connect(i,console);
console.NewLine();
console.WriteString("Client " + i + " connecte au serveur");
console.NewLine();
}
}
} |
Serveur_Thread :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package Serveur;
import deug.DeugConsole;
import deug.DeugSocket;
public class Serveur_Thread {
public static void Serveur_Connect(int Joueur,DeugConsole console){
DeugSocket s = new DeugSocket();
Serveur_Thread_Reception r = new Serveur_Thread_Reception(Joueur,s,console);
r.start();
s = new DeugSocket();
Serveur_Thread_Envoi e = new Serveur_Thread_Envoi(s,console);
e.start();
}
} |
Serveur_Thread_Envoi :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package Serveur;
import deug.DeugConsole;
import deug.DeugSocket;
public class Serveur_Thread_Envoi extends Thread {
private DeugSocket s;
private DeugConsole console;
Serveur_Thread_Envoi(DeugSocket s,DeugConsole console){
this.s=s;
this.console=console;
}
public void run(){
String text;
while(true){
console.NewLine();
text = console.ReadString("Serveur > ");
s.WriteString(text);
}
}
} |
Serveur_Thread_Reception :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package Serveur;
import deug.DeugConsole;
import deug.DeugSocket;
public class Serveur_Thread_Reception extends Thread {
private int nb;
private DeugSocket s;
private DeugConsole console;
Serveur_Thread_Reception(int nb, DeugSocket s,DeugConsole console){
this.nb=nb;
this.s=s;
this.console=console;
}
public void run(){
String text;
while(true){
text = s.ReadString();
console.NewLine();
console.WriteString("Client " + nb + ": " + text);
console.NewLine();
}
}
} |
Client_Thread :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package Client;
import deug.DeugSocket;
import deug.DeugConsole;
public class Client_Thread extends Thread {
public static void Client_Connect(DeugConsole console){
DeugSocket c = new DeugSocket("localhost");
Client_Thread_Envoi e = new Client_Thread_Envoi(c,console);
e.start();
c = new DeugSocket("localhost");
Client_Thread_Reception r = new Client_Thread_Reception(c,console);
r.start();
}
} |
Client_Thread_Envoi :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package Client;
import deug.DeugConsole;
import deug.DeugSocket;
public class Client_Thread_Envoi extends Thread {
private DeugSocket c;
private DeugConsole console;
Client_Thread_Envoi(DeugSocket c,DeugConsole console){
this.c=c;
this.console=console;
}
public void run(){
String text;
console.WriteString("Vous etes connecte au serveur");
while(true){
text = console.ReadString("> ");
c.WriteString(text);
console.NewLine();
}
}
} |
Client_Thread_Reception :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package Client;
import deug.DeugConsole;
import deug.DeugSocket;
public class Client_Thread_Reception extends Thread {
private DeugSocket c;
private DeugConsole console;
Client_Thread_Reception(DeugSocket c,DeugConsole console){
this.c=c;
this.console=console;
}
public void run(){
String text;
while(true){
text = c.ReadString();
console.NewLine();
console.WriteString("Serveur : " + text);
console.NewLine();
}
}
} |
Librairies :
DeugSocket :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
| package deug;
/**
*
*/
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
/**
* Very simple network handling class.
*
* The constructors for this class, will either create a DeugSocket which will immediately connects itself to another DeugSocket, or a DeugSocket waiting for a connection.
* Once two DeugSocket have been created and are connected together, they can exchange data.
* @author Mathias Kende
*/
public class DeugSocket implements DeugIO {
private Socket socket = null;
private DataInputStream in_stream = null;
private DataOutputStream out_stream = null;
private static final int default_port = 5000;
private void SetStream(Socket sock)
{
try {
socket = sock;
in_stream = new DataInputStream(socket.getInputStream());
out_stream = new DataOutputStream(socket.getOutputStream());
} catch (Exception e) {
System.err.println("Erreur lors de l'ouverture de la connection : " + e.toString());
e.printStackTrace();
System.exit(1);
}
}
private void SetClient(String hostname, int port)
{
try
{
SetStream(new Socket(hostname,port));
}
catch (IOException e)
{
System.err.println("Erreur lors de l'ouverture de la connection client : " + e.toString());
e.printStackTrace();
System.exit(1);
}
}
public DeugSocket(String hostname, int port)
{
SetClient(hostname, port);
}
/**
* This constructor creates a DeugSocket connected to another DeugSocket waiting at the given hostname.
* @param host The hostname to connect to. It can be of the form "hostname" or "hostname:port". In the former case the default port number is used.
*/
public DeugSocket(String host)
{
//try
{
StringTokenizer st = new StringTokenizer(host,":");
String hostname = st.nextToken();
int port = default_port;
if (st.hasMoreTokens())
{
port = Integer.parseInt(st.nextToken());
}
SetClient(hostname, port);
// SetStream(new Socket(hostname,port));
}
/* catch (Exception e)
{
//System.err.println("Erreur lors de l'ouverture de la connection client : " + e.toString());
//e.printStackTrace();
System.exit(1);
}*/
}
private void SetServer(int port)
{
try
{
ServerSocket server = new ServerSocket(port);
SetStream(server.accept());
server.close();
}
catch (Exception e)
{
System.err.println("Erreur lors de l'ouverture de la connection serveur : " + e.toString());
e.printStackTrace();
System.exit(1);
}
}
/**
* This constructor creates a DeugSocket waiting for a connection on the given port.
* @param port The port on which the conection is waited for.
*/
public DeugSocket(int port)
{
SetServer(port);
}
/**
* This constructor creates a DeugSocket waiting for a connection on the default port.
*/
public DeugSocket()
{
SetServer(default_port);
}
public void finalize()
{
in_stream = null;
out_stream = null;
try {
socket.close();
} catch (IOException e) {
System.err.println("Erreur lors de la fermeture d'une socket : " + e.toString());
e.printStackTrace();
System.exit(1);
}
}
/**
* Send an int to the other DeugSocket
* @param i
*/
public void WriteInt(int i)
{
try {
out_stream.writeInt(i);
} catch (IOException e) {
System.err.println("Erreur lors de l'envoie d'un entier : " + e.toString());
e.printStackTrace();
System.exit(1);
}
}
/**
* Receive an int from the other DeugSocket
* @return
*/
public int ReadInt()
{
try {
return in_stream.readInt();
} catch (IOException e) {
System.err.println("Erreur lors de la réception d'un entier : " + e.toString());
e.printStackTrace();
System.exit(1);
return 0; // Java is even dumber than ugly
}
}
/**
* Send a character to the other DeugSocket
* @param c
*/
public void WriteChar(char c)
{
try {
out_stream.writeChar(c);
} catch (IOException e) {
System.err.println("Erreur lors de l'envoie d'un caractère : " + e.toString());
e.printStackTrace();
System.exit(1);
}
}
/**
* Receive a character from the other DeugSocket
* @return
*/
public char ReadChar()
{
try {
return in_stream.readChar();
} catch (IOException e) {
System.err.println("Erreur lors de la réception d'un caractère : " + e.toString());
e.printStackTrace();
System.exit(1);
return 0; // Java is even dumber than ugly
}
}
/**
* Send a string to the other DeugSocket
* @param s
*/
public void WriteString(String s)
{
/* SendInt(s.length);
out_stream.write(s,0,s.length); */
try {
out_stream.writeUTF(s);
} catch (IOException e) {
System.err.println("Erreur lors de l'envoie d'une chaîne : " + e.toString());
e.printStackTrace();
System.exit(1);
}
}
/**
* Receive a string from the other DeugSocket
* @return
*/
public String ReadString()
{
/* int len = GetInt();
byte[] s = new byte[len];
in_stream.readFully(s);
in_stream.
return s; */
try {
return in_stream.readUTF();
} catch (IOException e) {
System.err.println("Erreur lors de la réception d'une chaîne : " + e.toString());
e.printStackTrace();
System.exit(1);
return null; // Java is even dumber than ugly
}
}
/**
* Send a string terminated by a new line.
* @param s
*/
public void WriteLine(String s)
{
/* SendInt(s.length);
out_stream.write(s,0,s.length); */
try {
out_stream.writeBytes(s);
NewLine();
} catch (IOException e) {
System.err.println("Erreur lors de l'envoie d'une chaîne : " + e.toString());
e.printStackTrace();
System.exit(1);
}
}
/**
* Read a string terminated by a new line.
* @return
*/
@SuppressWarnings("deprecation")
public String ReadLine()
{
/* int len = GetInt();
byte[] s = new byte[len];
in_stream.readFully(s);
in_stream.
return s; */
try {
String readLine = in_stream.readLine();
return readLine;
} catch (IOException e) {
System.err.println("Erreur lors de la réception d'une chaîne : " + e.toString());
e.printStackTrace();
System.exit(1);
return null; // Java is even dumber than ugly
}
}
private static final byte[] endl = {13, 10};
/**
* Send a new line character (or string) to the other end.
*/
public void NewLine()
{
try {
out_stream.write(endl);
} catch (IOException e) {
System.err.println("Erreur lors de l'envoie d'un retour Ã* la ligne : " + e.toString());
e.printStackTrace();
System.exit(1);
}
}
} |
DeugIO:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package deug;
public interface DeugIO {
/**
* Send an int to the other DeugSocket
* @param i
*/
public abstract void WriteInt(int i);
/**
* Receive an int from the other DeugSocket
* @return
*/
public abstract int ReadInt();
/**
* Send a character to the other DeugSocket
* @param c
*/
public abstract void WriteChar(char c);
/**
* Receive a character from the other DeugSocket
* @return
*/
public abstract char ReadChar();
/**
* Send a string to the other DeugSocket
* @param s
*/
public abstract void WriteString(String s);
/**
* Receive a string from the other DeugSocket
* @return
*/
public abstract String ReadString();
/**
* Send a new line character (or string) to the other end.
*/
public abstract void NewLine();
} |
DeugConsole :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| package deug;
import java.io.*;
public class DeugConsole implements DeugIO {
private Console c; // = System.console();
private BufferedReader r;
private boolean use_console;
private static DeugConsole m;
public DeugConsole() {
c = System.console();
if (c == null) {
/* Inside the Eclipse Console panel, this Console class is not accessible ! */
r = new BufferedReader(new InputStreamReader(System.in));
use_console = false;
//System.err.println("Impossible de créer une console ici.");
//System.exit(1);
} else {
use_console = true;
}
}
@Override
public char ReadChar() {
byte[] b = new byte[1];
try {
System.in.read(b,1,0);
} catch (IOException e) {
System.err.println("Erreur lors de la lecture d'un caractère : " + e.toString());
e.printStackTrace();
System.exit(1);
}
return (char) b[0];
}
@Override
public int ReadInt() {
String line = null; /* This is the dumbest statistical analysis ever... */
if (use_console) {
line = c.readLine();
} else {
try {
line = r.readLine();
} catch (IOException e) {
System.err.println("Erreur lors de la lecture d'un entier : " + e.toString());
e.printStackTrace();
System.exit(1);
}
}
return Integer.parseInt(line);
}
public String ReadString(String s) {
if (use_console) {
return c.readLine(s);
} else {
try {
WriteString(s);
return r.readLine();
} catch (IOException e) {
System.err.println("Erreur lors de la lecture d'une chaîne : " + e.toString());
e.printStackTrace();
System.exit(1);
return null;
}
}
}
@Override
public String ReadString() {
return ReadString("");
}
@Override
public void WriteChar(char c) {
System.out.print(c);
}
@Override
public void WriteInt(int i) {
System.out.print(i);
}
@Override
public void WriteString(String s) {
System.out.print(s);
}
/**
* Prints a new-line on the console.
*/
public void NewLine() {
System.out.println();
}
// I fear that this is very poor design pattern...
public static DeugConsole console()
{
if (m == null)
m = new DeugConsole();
return m;
}
} |
Le serveur reçoit très bien ce que les clients envoient, mais lors de l'envoi d'une chaîne, un seul client (pas toujours le même) reçoit le message.
Savez vous d'où ça vient?
Merci d'avance!