Client/serveur JAVA "petite messagerie" locale
Bonjour,
j'ai un Serveur TestServeurUDP et deux clients TestClientUDP1 et TestClientUDP2 et j'arrive bien à me connecter via les clients sur le serveur et a renvoyer bonjour "nom saisi sur chaque client" sur chaque client, mais je n'arrive pas à envoyer les infos saisies sur TestClientUDP1 su TestClientUDP2.
Merci de votre aide.
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
| package othello;
import java.io.*;
import java.net.*;
public class TestServeurUDP {
final static int port = 9632;
final static int port2 = 9638;
final static int taille = 1024;
final static int taille2 = 1024;
static byte buffer[] = new byte[taille];
static byte buffer2[] = new byte[taille2];
public static void main(String argv[]) throws Exception {
DatagramSocket socket = new DatagramSocket(port);
DatagramSocket socket2 = new DatagramSocket(port2);
String donnees = "";
String donnees2 = "";
String message = "";
String message2 = "";
int taille = 0;
int taille2 = 0;
System.out.println("Lancement du serveur");
while (true) {
DatagramPacket paquet = new DatagramPacket(buffer, buffer.length);
DatagramPacket paquet2 = new DatagramPacket(buffer2, buffer2.length);
DatagramPacket envoi = null;
DatagramPacket envoi2 = null;
System.out.println("receive paquet");
socket.receive(paquet);
System.out.println("receive paquet2");
socket2.receive(paquet2);
System.out.println("\n"+paquet.getAddress());
System.out.println("\n"+paquet2.getAddress());
taille = paquet.getLength();
taille2 = paquet2.getLength();
donnees = new String(paquet.getData(),0, taille);
donnees2 = new String(paquet2.getData(),0, taille2);
System.out.println("Donnees reçues = "+donnees);
System.out.println("Donnees reçues = "+donnees2);
message = "Bonjour client1 : "+donnees;
message2 = "Bonjour client2 : "+donnees2;
System.out.println("Donnees envoyees = "+message);
System.out.println("Donnees envoyees = "+message2);
envoi = new DatagramPacket(message.getBytes(),
message.length(), paquet.getAddress(), paquet.getPort()); // modif 1 en 2
envoi2 = new DatagramPacket(message2.getBytes(),
message2.length(), paquet2.getAddress(), paquet2.getPort());
//socket.send(envoi); // modif en 2
//socket2.send(envoi2);
System.out.println("envoi socket avec envoi2 = "+paquet2.getAddress()+" "+paquet2.getPort());
socket2.send(envoi2); // modif en 2
System.out.println("envoi socket avec envoi = "+paquet.getAddress()+" "+paquet.getPort());
socket.send(envoi);
}
}
} |
// *************** les clients
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
| package othello;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class TestClientUDP1 {
final static int port = 9632;
final static int taille = 1024;
static byte buffer[] = new byte[taille];
public static void main(String argv[]) throws Exception {
while (true) {
try {
//InetAddress serveur = InetAddress.getByName(argv[0]);
InetAddress serveur = InetAddress.getLocalHost();
//int length = argv[1].length();
//int length = 20;
//byte buffer[] = argv[1].getBytes();
System.out.println("Client 1 : Saisissez un Nom :");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine()+" client1 : ";
int length = str.length();
buffer = str.getBytes();
DatagramSocket socket = new DatagramSocket();
DatagramPacket donneesEmises = new DatagramPacket(buffer, length, serveur, port);
DatagramPacket donneesRecues = new DatagramPacket(new byte[taille], taille);
socket.setSoTimeout(30000);
socket.send(donneesEmises);
socket.receive(donneesRecues);
//socket.close();
System.out.println("Message : " + new String(donneesRecues.getData(),
0, donneesRecues.getLength()));
System.out.println("de : " + donneesRecues.getAddress() + ":" +
donneesRecues.getPort());
} catch (SocketTimeoutException ste) {
System.out.println("Le delai pour la reponse a expire");
} catch (Exception e) {
e.printStackTrace();
}
}
}
} |
// **********************
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
| package othello;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class TestClientUDP2 {
final static int port = 9638;
final static int taille = 1024;
static byte buffer[] = new byte[taille];
public static void main(String argv[]) throws Exception {
while (true) {
try {
//InetAddress serveur = InetAddress.getByName(argv[0]);
InetAddress serveur = InetAddress.getLocalHost();
//int length = argv[1].length();
//int length = 20;
//byte buffer[] = argv[1].getBytes();
//*******
System.out.println("Client 2 : Saisissez un Nom :");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine()+" client2 : ";
int length = str.length();
buffer = str.getBytes();
DatagramSocket socket = new DatagramSocket();
//socket.close();
DatagramPacket donneesEmises = new DatagramPacket(buffer, length, serveur, port);
DatagramPacket donneesRecues = new DatagramPacket(new byte[taille], taille);
socket.setSoTimeout(30000);
socket.send(donneesEmises);
socket.receive(donneesRecues);
System.out.println("Message : " + new String(donneesRecues.getData(),
0, donneesRecues.getLength()));
System.out.println("de : " + donneesRecues.getAddress() + ":" +
donneesRecues.getPort());
} catch (SocketTimeoutException ste) {
System.out.println("Le delai pour la reponse a expire");
} catch (Exception e) {
e.printStackTrace();
}
}
//*****************
}
} |