7ider5
Messages postés
5
Date d'inscription
lundi 19 novembre 2012
Statut
Membre
Dernière intervention
19 novembre 2012
6 mai 2008 à 17:18
voici le source :
package serveurlaser;
import javax.comm.*;
import java.util.*;
import java.lang.*;
import java.security.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.net.ssl.*;
import javax.net.*;
import java.net.Socket;
import java.net.ServerSocket;
/**
*
* @author pierre
*/
public class FServeur extends javax.swing.JFrame {
public static String carte;
private SerialPort serialPort;
CommPortIdentifier portId;
SSLServerSocket serverSocket = null;
SSLSocket clientSocket = null;
BufferedReader isClient = null;
PrintWriter osClient = null; int port 9999, i 0;
String msg_String = null;
int msg_hash = 0;
int Clientconnected = 0;
String[] Id = {"yassine bouslimani"};
String[] Pass = {"bouslimani"};
String[] CarteMgt = {"T1:%1 0106763 2?.A5"};
/** Creates new form FServeur */
public FServeur()
{
initComponents();
//listePortsDispo();
}
public void listePortsDispo()
{
Enumeration portList=CommPortIdentifier.getPortIdentifiers();
if (portList==null)
{
AreaEvent.append("Aucun port COM" );
ComPort.addItem("No COM");
return;
}
AreaEvent.append("recherche ports COM...\n" );
while (portList.hasMoreElements())
{
portId=(CommPortIdentifier)portList.nextElement();
//System.out.println("* "+portId.getName());
try
{
//serialPort
serialPort = (SerialPort) portId.open("ModeEvenement", 2000);
ComPort.addItem(portId.getName());
}
catch (PortInUseException e) {}
} //fin while
serialPort.close();
AreaEvent.append("Choisir port COM\n");
} //fin de la methode listePortsDispo()
// Création du serveur
public void creerServeur()
{
port = Integer.parseInt(TxtPort.getText());
try {
System.out.print(".");
String keystore = "serverkeys";
char keystorepass[] = "serverpwd".toCharArray();
char keypassword[] = "serverpwd".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(keystore), keystorepass);
KeyManagerFactory kmf =
KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, keypassword);
SSLContext sslcontext =
SSLContext.getInstance("SSLv3");
sslcontext.init(kmf.getKeyManagers(), null, null);
ServerSocketFactory ssf =
sslcontext.getServerSocketFactory();
serverSocket = (SSLServerSocket) ssf.createServerSocket(port);
AreaEvent.append("Lancé.\n\n");
}
catch (IOException e) {
AreaEvent.append(
"...failed.\nProbleme a la creation du SSLServerSocket : " + e);
System.exit(1);
}
catch (Exception e) {
AreaEvent.append(
"...failed.\nProbleme a la creation du SSLServerSocket : " + e);
System.exit(1);
}
}
// Attente de connexion d'un client
public void attendreConnexion()
{
try {
clientSocket = (SSLSocket) serverSocket.accept();
AreaEvent.append("Client accepté.");
Clientconnected = 1;
String[] supported = clientSocket.getSupportedCipherSuites();
AreaEvent.append("cipher suites autorisé : "+ supported);
clientSocket.setEnabledCipherSuites(supported);
}
catch (IOException e) {
AreaEvent.append(
"Impossible d'echanger avec BufferedReader et PrintWriter pour le clientSocket : " +
e);
System.exit(1);
}
}
// Récupération des flux d'entrée de sortie
public void obtenirFlux()
{
try {
isClient = new BufferedReader(new InputStreamReader(clientSocket.
getInputStream()));
osClient = new PrintWriter(clientSocket.getOutputStream());
}
catch (IOException e) {
AreaEvent.append("Impossible d'exanger les flux : " + e);
}
}
//Initialisation de la Frame
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
BtServeur = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
jScrollPane3 = new javax.swing.JScrollPane();
AreaClients = new javax.swing.JTextArea();
jPanel4 = new javax.swing.JPanel();
TxtIdentifiants = new javax.swing.JTextField();
TxtPassword = new javax.swing.JPasswordField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
BtIdentification = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
TxtPort = new javax.swing.JTextField();
ComPort = new javax.swing.JComboBox();
jLabel5 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
AreaComm = new javax.swing.JTextArea();
TxtComm = new javax.swing.JTextField();
BtEnvoi = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
AreaEvent = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Gestion du serveur", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12)));
BtServeur.setText("Démarrer Serveur");
BtServeur.setEnabled(false);
BtServeur.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtServeurActionPerformed(evt);
}
});
jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Clients", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12)));
AreaClients.setColumns(20);
AreaClients.setRows(5);
jScrollPane3.setViewportView(AreaClients);
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
);
jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Connexion", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12)));
jLabel1.setText("Identifiants");
jLabel2.setText("Mot de passe");
BtIdentification.setText("Identification");
BtIdentification.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtIdentificationActionPerformed(evt);
}
});
jLabel3.setText("IP Serveur");
jLabel4.setText("Port d'écoute");
TxtPort.setText("9999");
jLabel5.setText("Port lecteur carte");
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(BtIdentification)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3))
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(ComPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5)
.addComponent(jLabel4)))
.addGroup(jPanel4Layout.createSequentialGroup()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(TxtPassword)
.addComponent(TxtIdentifiants, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(TxtPort, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)))
.addContainerGap())
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel3)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(TxtIdentifiants, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(ComPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(TxtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(TxtPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(BtIdentification)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(BtServeur, javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
.addComponent(BtServeur)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Communication", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12)));
AreaComm.setColumns(20);
AreaComm.setRows(5);
jScrollPane1.setViewportView(AreaComm);
BtEnvoi.setText("Envoyer");
BtEnvoi.setEnabled(false);
BtEnvoi.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtEnvoiActionPerformed(evt);
}
});
AreaEvent.setColumns(20);
AreaEvent.setRows(5);
jScrollPane2.setViewportView(AreaEvent);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addComponent(TxtComm, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
.addGap(8, 8, 8)
.addComponent(BtEnvoi, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(TxtComm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(BtEnvoi))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void BtServeurActionPerformed(java.awt.event.ActionEvent evt) {
BtServeur.setText("Arrêter Serveur");
//copierCertificat();
AreaEvent.append("Lancement du serveur...");
creerServeur();
AreaEvent.append("OK\n");
AreaEvent.append("Attente d'un client...\n");
attendreConnexion();
//AreaEvent.append(connexion.getInetAddress().getHostName() + " s'est connecté\n");
AreaEvent.append("Obtention des flux...");
obtenirFlux();
AreaEvent.append("OK\n");
// Lecture des messages
while (true) {
try {
msg_String = isClient.readLine();
AreaEvent.append("Message du client client : " + msg_String);
}
catch (IOException e) {
AreaEvent.append("Impossible d'accerder au I/O: " + e);
System.exit(1);
}
if (msg_String.trim().equals("/bye")) {
Clientconnected = 0;
try
{
clientSocket.close();
serverSocket.close();
System.exit(0);
}catch (IOException e){}
}
if (Clientconnected == 1) {
try {
AreaEvent.append("\nComputing...");
msg_hash = msg_String.hashCode();
System.out.print("done");
osClient.println(msg_hash);
osClient.flush();
AreaEvent.append("...and sent.\n");
}
catch (Exception e) {
System.out.println(e);
System.exit(1);
}
}
}
}
private void BtEnvoiActionPerformed(java.awt.event.ActionEvent evt) {
}
private void BtIdentificationActionPerformed(java.awt.event.ActionEvent evt) {
for (int i=0 ; i