Programmation réseau "client/serveur"

cs_sana83 Messages postés 29 Date d'inscription samedi 17 février 2007 Statut Membre Dernière intervention 9 novembre 2011 - 18 nov. 2009 à 20:39
chouchounoussa11 Messages postés 7 Date d'inscription vendredi 26 mars 2010 Statut Membre Dernière intervention 12 avril 2010 - 3 avril 2010 à 00:07
je suis debutante en programmation réseau je veut faire un programme qui envoi des messages entre 1 client et 1 serveur j'ai le code :

code client

import java.io.*;
import java.net.*;
public class Client {
static final int port= 8081;
public static void main(String []argv)throws Exception{
String str="salut serveur",rec1;
Socket s1=new Socket("127.0.0.1",port);
BufferedReader in1=new BufferedReader(new InputStreamReader(s1.getInputStream()));
PrintWriter out1=new PrintWriter(new BufferedWriter(new OutputStreamWriter(s1.getOutputStream())),true);
out1.println(str);
rec1=in1.readLine();
System.out.println(rec1);
s1.close();
}
}
le serveur va lire le message du client et lui envoie salut client

code serveur
import java.io.*;
import java.net.*;
public class Serveur {
static final int port =8081;
public static void main(String []argv) throws Exception
{String str;
ServerSocket serv = new ServerSocket(port);
System.out.println("serveur en attente de connexion sur lengthport:"+port);
Socket serr=serv.accept();
System.out.println("connexion acceptée pour le client");
BufferedReader in=new BufferedReader(new InputStreamReader(serr.getInputStream()));
PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(serr.getOutputStream())),true);
System.out.println("flux ouvert,serveur pret a recevoir");
str=in.readLine();
out.println("salut client");
serr.close();
serv.close();
}
}
je veux ajouter dans la partie serveur "quel est votre nom" (le serveur pose cet question) et le client répond "mon nom est nom_CLIENT"
pouvez vous m'aidez ??
merci pour votre collaboration

2 réponses

hosni83 Messages postés 1 Date d'inscription mercredi 3 janvier 2007 Statut Membre Dernière intervention 2 avril 2010
2 avril 2010 à 17:07
je suis treeeees faché
aloui hosni
0
chouchounoussa11 Messages postés 7 Date d'inscription vendredi 26 mars 2010 Statut Membre Dernière intervention 12 avril 2010
3 avril 2010 à 00:07
chouou
Salut,
J'ai modifié, un petit peu,votre code et j'ai utilisé deux fenetre(Serveur/Client)avec deux champs de texte pour chacune afin de saisir le msg à envoyer et à recevoir:
/////***********************Client:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

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

/*
* Client.java
*
* Created on 2 avr. 2010, 22:28:44
*/

/**
*
*
*/
public class Client extends javax.swing.JFrame {
int port=8081;
/** Creates new form Client */
public Client() {
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();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Client");

jButton1.setText("envoyer");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

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(47, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(115, 115, 115)
.addComponent(jButton1)
.addGap(27, 27, 27))
.addGroup(layout.createSequentialGroup()
.addGap(102, 102, 102)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(123, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(48, 48, 48)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(74, 74, 74)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(72, Short.MAX_VALUE))
);

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

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
String rec1;
Socket s1=new Socket("127.0.0.1",port);
BufferedReader in1=new BufferedReader(new InputStreamReader(s1.getInputStream()));
PrintWriter out1=new PrintWriter(new BufferedWriter(new OutputStreamWriter(s1.getOutputStream())),true);

out1.println(jTextField1.getText());
rec1=in1.readLine();
jTextField2.setText(rec1);
s1.close();
}
catch(Exception e){}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Client().setVisible(true);
}
});
}

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

/////////////*********************Serveur:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

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

/*
* Serveur.java
*
* Created on 2 avr. 2010, 22:31:42
*/

/**
*
*
*/
public class Serveur extends javax.swing.JFrame {
int port=8081;


/** Creates new form Serveur */
public Serveur() throws IOException {
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();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();


setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("envoyer");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("quitter");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setText("cancel");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

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()
.addGap(93, 93, 93)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(90, 90, 90))
.addGroup(layout.createSequentialGroup()
.addGap(123, 123, 123)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2))
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(jButton3)))
.addContainerGap(38, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGap(55, 55, 55)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(71, 71, 71)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(90, 90, 90)
.addComponent(jButton2)
.addGap(44, 44, 44)
.addComponent(jButton3)))
.addContainerGap(45, Short.MAX_VALUE))
);

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

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
ServerSocket serv = new ServerSocket(port);

Socket serr=serv.accept();

BufferedReader in=new BufferedReader(new InputStreamReader(serr.getInputStream()));
PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(serr.getOutputStream())),true);

jTextField2.setText(in.readLine());
out.println(jTextField1.getText());
serr.close();
serv.close();

}
catch(Exception e){}
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jTextField2.setText("");
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try{
new Serveur().setVisible(true);}
catch(IOException e){}
}
});
}

// Variables declaration - do not modify

private javax.swing.JButton jButton3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration

}
///**********vous pouvez amiliorer plus ce code
0
Rejoignez-nous