Communication client/serveur avec sochet

Fermé
asmafejjari Messages postés 1 Date d'inscription dimanche 11 mars 2012 Statut Membre Dernière intervention 12 mars 2012 - 12 mars 2012 à 00:11
Bonjour,



normalment le client envoie des informations au serveur qui fait appel à un autre classe "recherche" .
cette classe traite les infos et envoyer au client ( tout ce qui est normal).
le probleme mon code ne marche pas bien je ne sais pas quelles modifications je dois les faire pour avoir un bon resultat


Pouvez vous m'iader s'il vous plait??
voila la code:
//class client


import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JOptionPane;


public class IG1 extends JFrame {
public IG1(){
super();
try{
JPanel pg = new JPanel();
pg.setLayout(new GridLayout(2, 1));
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();

JPanel p5 = new JPanel();
p1.setLayout(new GridLayout(3, 4));
p2.setLayout(new FlowLayout());
p3.setLayout(new GridLayout(2, 1));


final JLabel l1 = new JLabel(" Ville de départ :");
JLabel l2 = new JLabel(" Ville d'arrivée :");
JLabel l3 = new JLabel(" Date :");
JLabel l4 = new JLabel(" Catégorie :");
final JTextField t1 = new JTextField();
final JTextField t2 = new JTextField();
final JComboBox c = new JComboBox(new String[]{"enfant", "jeunesse", "adult",});


final JComboBox CB1 = new JComboBox();
for (int i = 1; i < 32; i++) {
CB1.addItem(i);
}
final JComboBox CB2 = new JComboBox();
for (int i = 1; i < 13; i++) {
CB2.addItem(i);
}
final JComboBox CB3 = new JComboBox();
for (int i = 1993; i < 2100; i++) {
CB3.addItem(i);
}

JButton A = new JButton("Chercher");

p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(t2);
p1.add(l3);
p1.add(CB1);
p1.add(CB2);
p1.add(CB3);
p1.add(l4);
p1.add(c);
p2.add(A);
p3.add(p1);
p3.add(p2);
p3.add(p5);
pg.setSize(600, 600);
this.setContentPane(p3);
A.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
String date = CB1.getSelectedItem().toString() + "/" + CB2.getSelectedItem().toString() + "/" + CB3.getSelectedItem().toString();
String categorie = c.getSelectedItem().toString();

String a =t1.getText().toString();
String c=t2.getText().toString();

InetAddress addr = null;

addr = InetAddress.getByName("localhost");
Socket socket = null;
socket = new Socket(addr,9092);
DataInputStream in = null;
in = new DataInputStream(socket.getInputStream());
PrintStream out = null;
out = new PrintStream(socket.getOutputStream());

out.println("ajouter");

a = in.readLine();
c = in.readLine();
date = in.readLine();
categorie = in.readLine();

out.flush();
socket.close();



}catch(Exception e0){ System.out.println("problème de connection à la BD!"+e0);}
}

});
}catch(Exception e){
JOptionPane.showMessageDialog(rootPane, "L'opération a echoué essayer de nouveau ", "ERROR", WIDTH, null);
e.getMessage();
}
}
public IG1(String numvoyage,String heure,String nombreplace,String prix){
JPanel p = new JPanel();
p.setLayout(new GridLayout(4, 1));
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1, 1));
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(3, 2));
JPanel p3 = new JPanel();
p3.setLayout(new FlowLayout());
JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(4, 1));
JLabel l5 = new JLabel(" N° du voyage :");
JLabel l6 = new JLabel(" Nombre du place disponible :");
JLabel l7 = new JLabel(" L'heure :");
JLabel l15 = new JLabel(" Le prix :");
final JLabel l18 = new JLabel(" ");
final JLabel l8 = new JLabel(" ");
final JLabel l9 = new JLabel("");
final JLabel l10 = new JLabel(" ");
JLabel l11 = new JLabel("Si vous voulez reserver une ticket(s) vous devez taper le N° de votre carte d'identité ''cin'', le N° de votre carte visa et le nombre de place à reserver.");
JLabel l12 = new JLabel(" CIN : ");
final JTextField t1 = new JTextField();
JLabel l13 = new JLabel(" N°carte visa : ");
final JTextField t2 = new JTextField();
JLabel l14 = new JLabel(" Nombre des places à reserver : ");
final JTextField t3 = new JTextField();
JButton b = new JButton("reserver");
JButton c = new JButton("Calculer Montant");
final JLabel l16 = new JLabel(" Montant à payer ");

p.add(l5);
p.add(l10);
p.add(l6);
p.add(l8);
p.add(l7);
p.add(l9);
p.add(l15);
p.add(l18);
p1.add(l11);
p2.add(l12);
p2.add(t1);
p2.add(l13);
p2.add(t2);
p2.add(l14);
p2.add(t3);
p3.add(b);
p3.add(c);
p5.add(p);
p5.add(p1);
p5.add(p2);
p5.add(p3);

this.setSize(890, 480);
this.setContentPane(p5);
l10.setText(numvoyage);
l8.setText(heure);
l9.setText(nombreplace);
l18.setText(prix);}
public static void main(String[] args) {
try{

IG1 I = new IG1();
I.setVisible(true);
I.setSize(1000, 600);
I.setTitle("BIENVENUE à l'ANV");

}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
//class serveur

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
public class serveur {
public static void main(String[] args) {

try{
int port=9092;
ServerSocket s = new ServerSocket(port);
System.out.println("le serveur demarre sur le port: "+port);
Socket socket = s.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
while(true){
String str = in.readLine();
if(str.equals("ajouter")){
String a = in.readLine();
String b = in.readLine();
String c = in.readLine();
String d = in.readLine();
String x = recherche.recherche(a,b,c,d);
System.out.println("cin et solde bien recus "+x+" "+c);
out.println("OK");
out.flush();

// String resultat = recherche.chercherBD(villedepart,villearrivee,date,categorie);
//out.println(""+resultat);
}}

}catch (Exception e0) {System.out.println("erreur"+e0);}

}
}
// class recherche



import java.io.DataInputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JLabel;
import javax.swing.*;


public class recherche extends JFrame {
static JLabel l1=new JLabel("");
static JLabel l2=new JLabel("");
static JLabel l3=new JLabel("");
static JLabel l4=new JLabel("");
static JLabel l5=new JLabel("");
static Connection con;
static Statement st;
static ResultSet rs;
static String adr;
static int port;
static Socket s;
public recherche(){
super();
}

public static Connection creerConnectionBD(){
try{

String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
String url = "jdbc:mysql://localhost/revervation";
String user = "root";
String password = "";
con = DriverManager.getConnection(url, user, password);

}catch(Exception e0){ System.out.println("problème de connection à la BD!"+e0);}
return con;}
public static String recherche(String villedepart,String villearrivee,String date,String categorie){
String numvoyage ="";
String heure ="";
String nbrplace ="";
String prix ="";
try{

con = recherche.creerConnectionBD();
st = con.createStatement();
String requ1 = "Select * From voyages where villedepart = '" +villedepart+"' "
+ "and villearrivee='" +villearrivee+ "'"
+ "and date ='" +date+ "'"
+ "and categorie='" +categorie+ "'" ;
rs = st.executeQuery(requ1);
while (rs.next()) {
l1.setText(rs.getString("numvoyage"));
numvoyage = rs.getString("numvoyage");
l2.setText(rs.getString("nombreplace"));
heure = rs.getString("nombreplace");
l3.setText(rs.getString("heure"));
nbrplace = rs.getString("heure");
l4.setText(rs.getString("prix"));
prix = rs.getString("prix");

}

IG1 h =new IG1(numvoyage,heure,nbrplace,prix);
h.setVisible(true);
}catch(Exception e){ System.out.println("problème d'accès à la BD!"+e);}

return(" "+numvoyage+""+heure+""+nbrplace+""+prix);
}

}