Chat client serveur

nadjet22 - 9 mars 2013 à 12:34
 nadjet22 - 12 mars 2013 à 12:37
salut je suis nouveau au java mais je suis en train de créer un programme de chat client /serveur pour transfert des fichier à partir d'interface graphique moi j'ai utilisé netbeans pour le fonctionner

4 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
11 mars 2013 à 09:05
Salut,

Et quel est ton problème ?
0
salut
jé utilisé un code pour transmettre les msg et je veut faire la modification pour transfert des fichier
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
12 mars 2013 à 10:42
Si tu ne fais pas d'effort pour nous expliquer clairement ton problème, on ne pourra rien faire
0
salut,
voila le code qui jé utilisé et ma question c comment faire la modification pour envoyer des fichiers au lieu d'envoyer juste un texte , jé travaillé avec le netbeans .merci
code pour le serveur:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class serverform extends javax.swing.JFrame {


ServerSocket server = null;
Socket client = null;
DataOutputStream out = null ;
DataInputStream in = null ;
public serverform() {
initComponents();
}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

jTextField1 = new javax.swing.JTextField();
startlbl = new javax.swing.JButton();
envoiebtn = new javax.swing.JButton();
msgtxt = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
txt_recmsg = new javax.swing.JTextArea();

jTextField1.setText("jTextField1");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

startlbl.setText("start server");
startlbl.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startlblActionPerformed(evt);
}
});

envoiebtn.setText("Envoyer");
envoiebtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
envoiebtnActionPerformed(evt);
}
});

msgtxt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
msgtxtActionPerformed(evt);
}
});

txt_recmsg.setColumns(20);
txt_recmsg.setRows(5);
jScrollPane1.setViewportView(txt_recmsg);

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.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(startlbl)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jScrollPane1)))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(msgtxt, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(109, 109, 109)
.addComponent(envoiebtn)
.addGap(22, 22, 22))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(startlbl)
.addGap(51, 51, 51)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(msgtxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(envoiebtn))
.addGap(29, 29, 29))
);

pack();
}// </editor-fold>//GEN-END:initComponents

private void startlblActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startlblActionPerformed
try {
server = new ServerSocket(2000) ;
client = server.accept();
out = new DataOutputStream(client.getOutputStream());
in = new DataInputStream(client.getInputStream());
receptionmsg serverthread =new receptionmsg(in ,txt_recmsg);
serverthread.setDaemon(true);
serverthread.setName("");
serverthread.start();
JOptionPane.showMessageDialog(null, "requete du client est acceptée");
} catch (IOException ex) {
JOptionPane.showMessageDialog(null,"pas de client validé" );
Logger.getLogger(serverform.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_startlblActionPerformed

private void msgtxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_msgtxtActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_msgtxtActionPerformed

private void envoiebtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_envoiebtnActionPerformed
// TODO add your handling code here:
String msg = msgtxt.getText();
try {
out.writeUTF(msg);
} catch (IOException ex) {
Logger.getLogger(serverform.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_envoiebtnActionPerformed


public static void main(String args[]) {

try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(serverform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(serverform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(serverform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(serverform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new serverform().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton envoiebtn;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField msgtxt;
private javax.swing.JButton startlbl;
private javax.swing.JTextArea txt_recmsg;
// End of variables declaration//GEN-END:variables
}


le code de client

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


public class clientform extends javax.swing.JFrame {
Socket server = null;
DataInputStream in = null ;
DataOutputStream out = null;
/**
* Creates new form clientform
*/
public clientform() {
initComponents();
}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
connecterbtn = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
messagetex = new javax.swing.JTextArea();
receptbtn = new javax.swing.JButton();
envoiebtn = new javax.swing.JButton();
msg_txt = new javax.swing.JTextField();

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

connecterbtn.setText("connect");
connecterbtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
connecterbtnActionPerformed(evt);
}
});

messagetex.setColumns(20);
messagetex.setRows(5);
jScrollPane2.setViewportView(messagetex);

receptbtn.setText("Reception");
receptbtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
receptbtnActionPerformed(evt);
}
});

envoiebtn.setText("envoie");
envoiebtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
envoiebtnActionPerformed(evt);
}
});

msg_txt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
msg_txtActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(connecterbtn)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(receptbtn)
.addGap(31, 31, 31))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(msg_txt)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 86, Short.MAX_VALUE)
.addComponent(envoiebtn)
.addGap(48, 48, 48))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(25, 25, 25)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(connecterbtn)
.addComponent(receptbtn))
.addGap(28, 28, 28)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(envoiebtn, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(msg_txt, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(52, 52, 52))
);

pack();
}// </editor-fold>//GEN-END:initComponents

private void connecterbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connecterbtnActionPerformed
try {
server = new Socket("127.0.0.1", 2000) ;
in = new DataInputStream(server.getInputStream());
out = new DataOutputStream(server.getOutputStream());
receptionmsg clientthread= new receptionmsg(in , messagetex);
clientthread.setDaemon(true);
clientthread.setName("");
clientthread.start();
JOptionPane.showMessageDialog(null, "connecter avec le serveur");
} catch (UnknownHostException ex) {
JOptionPane.showMessageDialog(null, "");
Logger.getLogger(clientform.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "connection échoué");
Logger.getLogger(clientform.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_connecterbtnActionPerformed

private void receptbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_receptbtnActionPerformed
try {
// TODO add your handling code here:
String message = in.readUTF();
messagetex.append("\n" +message);
} catch (IOException ex) {
Logger.getLogger(clientform.class.getName()).log(Level.SEVERE, null, ex);
}


}//GEN-LAST:event_receptbtnActionPerformed

private void msg_txtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_msg_txtActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_msg_txtActionPerformed

private void envoiebtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_envoiebtnActionPerformed
try {
// TODO add your handling code here:
out.writeUTF(msg_txt.getText());
} catch (IOException ex) {
Logger.getLogger(clientform.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_envoiebtnActionPerformed


public static void main(String args[]) {

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(clientform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(clientform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(clientform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(clientform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new clientform().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton connecterbtn;
private javax.swing.JButton envoiebtn;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea messagetex;
private javax.swing.JTextField msg_txt;
private javax.swing.JButton receptbtn;
// End of variables declaration//GEN-END:variables
}


le code de la classe réception message:
import java.io.DataInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextArea;

public class receptionmsg extends Thread{
String message = "";
DataInputStream in = null;
JTextArea txt_area = null;
public receptionmsg(DataInputStream d,JTextArea a){
this.in = d;
this.txt_area = a;
}
public void run (){
while(true){
try {
message=in.readUTF();
txt_area.append("\n"+this.getName() + ":" + message);
} catch (IOException ex) {
Logger.getLogger(receptionmsg.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
0
Rejoignez-nous