Classe client
package client;
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
public class Client {
private static PrintWriter sortie = null;
public static void main(String[] args) throws IOException {
Socket clientSocket = null;
PrintWriter sortie = null;
BufferedReader entree = null;
int compteur = 0;
try {
clientSocket = new Socket("localhost", 9540);
sortie = new PrintWriter(clientSocket.getOutputStream(), true);
entree = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Host inconnu"+ e);
} catch (IOException e) {
System.err.println("erreur connection" + e);
}
JOptionPane.showMessageDialog(null,"Connexion établie", "INFORMATION",JOptionPane.INFORMATION_MESSAGE);
InterfaceClient inter = new InterfaceClient();
inter.setVisible(true);
BufferedReader stdEntree = new BufferedReader(new InputStreamReader(System.in));
String fromServer = "Rien pour l'instant
Serveur";
String fromUser = "Rien pour l'instant User";
while ((fromServer = entree.readLine()) != null) {
System.out.println("Server: " + fromServer);
boolean resul = inter.setListe(fromServer, compteur);
compteur++;
if (fromServer.equals("Base Command Out")){
}
else if (fromServer.equals("protocole incorrect")){
inter.reinitialiserListe();
resul = inter.setListe("protocole incorrect", 0);
compteur = 1;
}
else{
fromUser = stdEntree.readLine();
fromUser = inter.getReponseClient();
}
if (fromUser != null) {
if (fromUser.equalsIgnoreCase("thunder") || fromUser.equalsIgnoreCase("easy Company, 506th, 101st Airborne Division")){
System.out.println("Easy Compagny : " + fromUser);
resul = inter.setListe(fromUser, compteur);
compteur++;
sortie.println(fromUser);
}
else if (fromUser.equalsIgnoreCase("We're Oscar Mike. easy Company Out.")){
JOptionPane.showMessageDialog(null,"Communication réussit", "SUCCEES",JOptionPane.INFORMATION_MESSAGE);
}
System.out.println("Easy Compagny : " + fromUser);
sortie.println(fromUser);
}
}
sortie.close();
entree.close();
stdEntree.close();
clientSocket.close();
}
public static void envoyerMessageUser(String message){
sortie.println(message);
}
//public void affichage()
}
classe interface
package client;
import utilitaire.Message;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.ArrayList;
public class InterfaceClient extends JFrame {
private JPanel panneauNord = null,
panneauSud = null;
private JLabel statusConnection = null,
etat = null;
private JScrollPane scroller = null;
private JList liste = null;
private EcouteurFenetre ecouteurFenetre = null;
private String[] listeCommunication = new String[8];
private JTextField reponse = new JTextField();
private JButton btnEnvoyer = new JButton();
private String reponseClient;
public InterfaceClient(){
this.getContentPane().setLayout(null);
this.setTitle("Communication CLIENT");
this.setLocation(100, 100);
this.setSize(700, 390);
this.setResizable(false);
Color vert = new Color(54, 213, 36);
reponse.setBounds(new Rectangle(1, 300, 500, 25));
etat = new JLabel("État :");
etat.setBounds(new Rectangle(3,325,250,25));
statusConnection = new JLabel("Connecter");
statusConnection.setForeground(vert);
statusConnection.setBounds(new Rectangle(35,326,250,25));
btnEnvoyer.setText("Valider");
btnEnvoyer.setBounds(new Rectangle(170, 45, 80, 25));
btnEnvoyer.setFont(new Font("Dialog", 1, 12));
btnEnvoyer.addActionListener(new ActionListener() //Ajout de l'écouteur d'événement
{
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed
* (java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e)
{
btnEnvoyer_actionPerformed(e);
}
});
//Instancier les écouteurs
ecouteurFenetre = new EcouteurFenetre();
//Relier l'écouteur à la fenêtre
this.addWindowListener(new EcouteurFenetre());
//Initialiser le panneau principal de la fenêtre au conteneur par défaut
Container panPrincipal = this.getContentPane();
//Créer les composantes graphique de la zone de texte panneau centre
//et les ajouter
liste = new JList(listeCommunication);
Color jauneclair = new Color(255, 255, 212);
Color bleu = new Color(0, 85, 255);
liste.setFont(new Font("Serif",Font.PLAIN,12));
liste.setBackground(jauneclair);
liste.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scroller = new JScrollPane(liste);
scroller.setBounds(new Rectangle(1,50,500,250));
this.getContentPane().add(reponse,null);
this.getContentPane().add(statusConnection);
this.getContentPane().add(scroller);
this.getContentPane().add(etat);
this.getContentPane().add(btnEnvoyer);
/* panneauSud = new JPanel(new BorderLayout());
panneauSud.add(scroller);
panneauSud.add(reponse);*/
/*panneauNord = new JPanel(new BorderLayout());
panneauNord.add(statusConnection);
panPrincipal.add(panneauSud, BorderLayout.CENTER);
panPrincipal.add(panneauNord, BorderLayout.SOUTH); */
}
/**
* A utiliser pour recevoir la liste
* @return liste
*/
public JList getListe(){
return liste;
}
public String getReponseClient(){
return reponseClient;
}
/**
* A utiliser pour modifier la liste
* @return vrai si la liste a été modifiée et faux sinon
*/
public boolean setListe (String mess, int pos) {
listeCommunication[pos] = mess;
liste.setListData(listeCommunication);
return true;
}
public void reinitialiserListe(){
for(int i = 0; i < 7; i++){
listeCommunication[i] = " ";
}
}
private void btnEnvoyer_actionPerformed(ActionEvent e){
reponseClient = reponse.getText()+ "a";
Client.envoyerMessageUser(reponseClient);
JOptionPane.showMessageDialog(null,"Ok evenement correct");
}
/**
* Classe d'écouter de la fenêtre
*/
private class EcouteurFenetre extends WindowAdapter{
/* (non-Javadoc)
* @see java.awt.event.WindowAdapter#windowClosing(java.awt.event.WindowEvent)
*/
public void windowClosing(WindowEvent evt){
System.exit(0);
}
}
public static void main(String[] args)
{
//InterfaceServeur inter = new InterfaceServeur();
//inter.setVisible(true);
}
}
Classe Serveur
package serveur;
import protocole.Protocole;
import java.net.*;
import java.io.*;
public class Serveur{
public static void main(String[] args)throws IOException{
ServerSocket serSocket = null;
try{
serSocket = new ServerSocket(9540);
}
catch(IOException e){
System.out.println("Erreur lors connection sur port 9540" + e);
}
Socket connection = null;
try{
connection = serSocket.accept();
}
catch (IOException e){
System.out.println("Erreur lors de la connection avec le client" + e);
}
PrintWriter sortie = null;
BufferedReader entree = null;
sortie = new PrintWriter(connection.getOutputStream(),true);
entree = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String ligneEntree,
ligneSortie;
Protocole protocol = new Protocole();
ligneSortie = protocol.messageEntrant(null);
sortie.println(ligneSortie);
while((ligneEntree = entree.readLine())!= null){
ligneSortie = protocol.messageEntrant(ligneEntree);
sortie.println(ligneSortie);
if(ligneSortie.equals("We're Oscar Mike. Easy Company Out.")){
System.out.println("ok on se rend jusqu'a la fin maintenant les else");
}
}
sortie.close();
entree.close();
connection.close();
serSocket.close();
}
}