Socket

Sharingane Messages postés 1 Statut Membre -  
BadGuitarist Messages postés 373 Statut Membre -
Bonjour à tous, j'essaye tant bien que mal d'envoyer l'identifiant et le mot de passe a mon serveur. quand je remplis l'interface client, le serveur ne renvoie que l'identifiant. je voudrai savoir comment on fait pour envoyer 2 chaine de caractères String au serveur et il nous les renvoie . merci

*****SERVEUR*****
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* Admin.java
*
* Created on 10 mai 2010, 14:13:55
*/

package adminjava;
import java.io.*;
import java.net.*;

/**
*
* @author UCHIHA_ERIC_
*/
public class Admin extends javax.swing.JFrame {

/** Creates new form Admin */
public Admin() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("Connection");

jLabel1.setText("ADMINISTRATEUR");

jLabel2.setText("LOGIN");

jLabel3.setText("MOT DE PASSE");

jButton2.setText("Annuler");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel3)))
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE))
.addGap(65, 65, 65))))
.addGroup(layout.createSequentialGroup()
.addGap(55, 55, 55)
.addComponent(jButton1)
.addGap(58, 58, 58)
.addComponent(jButton2)))
.addContainerGap(131, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 72, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton1))
.addGap(24, 24, 24))
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration

public static void main(String[] args) {

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Admin().setVisible(true);
}
});

try {
String message;
String message1;
ServerSocket socketServeur = new ServerSocket(81);
System.out.println("Le serveur écoute sur le port 81");
while (true) {
Socket socketClient = socketServeur.accept();

//récupération des flux de lecture et d'écriture permettant de communiquer avec le client
BufferedReader in = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
PrintStream out = new PrintStream(socketClient.getOutputStream());

//lecture du message provenant du client
message = in.readLine();
message1= in.readLine();

System.out.println("le serveur recoit : "+message);

//le serveur envoie la réponse au client
out.println("identifiant: "+message);
out.println("mot de passe: "+message1);
//fermeture connexion
socketClient.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

***************CLIENT*************
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* Clients.java
*
* Created on 10 mai 2010, 14:04:44
*/

package clientsjava;
import java.net.*;
import java.io.*;

/**
*
* @author UCHIHA_ERIC_
*/
public class Clients extends javax.swing.JFrame {

/** Creates new form Clients */
public Clients() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jLabel6 = new javax.swing.JLabel();
jButton3 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("connection");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
jButton1MouseEntered(evt);
}
});
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jLabel4.setText("Mot de passe");

jLabel3.setText("identifiant");

jButton2.setText("Annuler");

jLabel6.setText("CLIENT");

jButton3.setText("Inscription");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(198, Short.MAX_VALUE)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(119, 119, 119))
.addGroup(layout.createSequentialGroup()
.addGap(66, 66, 66)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jLabel3)
.addComponent(jButton1))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(jButton2)
.addGap(37, 37, 37)
.addComponent(jButton3))
.addGroup(layout.createSequentialGroup()
.addGap(83, 83, 83)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE))))
.addContainerGap(29, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(jLabel6)
.addGap(64, 64, 64)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel4)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 98, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3))
.addGap(66, 66, 66))
);

pack();
}// </editor-fold>

private void jButton1MouseEntered(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
//connexion au serveur
Socket socket = new Socket("localhost",81);

//récupération des flux de lecture et d'écriture permettant de communiquer avec le client
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream out = new PrintStream(socket.getOutputStream());

//on envoie une phrase au serveur
out.println(jTextField1.getText());
out.println (jTextField2.getText());
//lecture et affichage de la réponse du serveur
System.out.println(in.readLine());
System.out.println(in.readLine());
// fermeture de la connexion
socket.close();
} catch (Exception e) {
e.printStackTrace();
}

}

/**
* @param args the command line arguments
*/

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel6;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration

public static void main(String[] args) {

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Clients().setVisible(true);
}
});
try {
//connexion au serveur
Socket socket = new Socket("localhost",81);

//récupération des flux de lecture et d'écriture permettant de communiquer avec le client
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream out = new PrintStream(socket.getOutputStream());

//on envoie une phrase au serveur
out.println();
//lecture et affichage de la réponse du serveur
System.out.println(in.readLine());
System.out.println(in.readLine());
// fermeture de la connexion
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}
VOILA mes 2 codes merci

1 réponse

BadGuitarist Messages postés 373 Statut Membre 27
 
Bonsoir Sharingane,

Peut-être je vais enfoncer une porte ouverte :

si tu envoyais à ton serveur une seule chaîne constituée du login et du mot de passe séparés par un caractère imposé.

Par exemple :
tu envoies au serveur login/mot de passe.
Le serveur qui recoit cette information fera à son tour l'identification des 2 chaines login et mot de passe à l'aide de l'instruction suivante :
String connexion[]=infoRecue.split("/");

En espérant que cela a pu te donner une nouvelle piste de réflexion ...

0