Application client serveur de chat avec JAVA
Cet article propose en détaille une application client serveur de chat avec JAVA.
Vous pourrez télécharger le fichier au format zip il contient cle ode complet pour le client et le serveur.
Etapes d’tilisation de l’application de discussion client-serveur très simple :
- Premièrement nous devons exécuter le serveur avant le client, car celui-ci attend que le client soit connecté.
- Le serveur continue d'écouter le client sur une adresse IP et un port attribués
- Pour établir la connexion, le client doit connaître l'adresse IP et le port du serveur.
- Lorsque nous démarrons l'application cliente, une connexion au serveur est créée.
- Une fois la connexion établie, les applications client et serveur peuvent envoyer et recevoir des messages.
Exemple du code source :
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JOptionPane;
public class Client extends javax.swing.JFrame {
private ObjectOutputStream output;
private ObjectInputStream input;
private String message="";
private String serverIP;
private Socket connection;
private int port = 6789;
public Client(String s) {
initComponents();
this.setTitle("Client");
this.setVisible(true);
status.setVisible(true);
serverIP = s;
}
@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
chatArea = new javax.swing.JTextArea();
jLabel2 = new javax.swing.JLabel();
status = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
jPanel1.setLayout(null);
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jPanel1.add(jTextField1);
jTextField1.setBounds(30, 50, 270, 30);
jButton1.setText("Send");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel1.add(jButton1);
jButton1.setBounds(310, 50, 80, 30);
chatArea.setColumns(20);
chatArea.setRows(5);
jScrollPane1.setViewportView(chatArea);
jPanel1.add(jScrollPane1);
jScrollPane1.setBounds(30, 110, 360, 270);
jLabel2.setForeground(new java.awt.Color(255, 255, 255));
jLabel2.setText("Write your text here");
jPanel1.add(jLabel2);
jLabel2.setBounds(30, 30, 150, 20);
status.setText("...");
jPanel1.add(status);
status.setBounds(30, 80, 300, 40);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bg7.jpg"))); // NOI18N
jPanel1.add(jLabel1);
jLabel1.setBounds(0, 0, 420, 410);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
);
setSize(new java.awt.Dimension(414, 428));
setLocationRelativeTo(null);
}// //GEN-END:initComponents
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
sendMessage(jTextField1.getText());
jTextField1.setText("");
}//GEN-LAST:event_jTextField1ActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
sendMessage(jTextField1.getText());
jTextField1.setText("");
}//GEN-LAST:event_jButton1ActionPerformed
public void startRunning()
{
try
{
status.setText("Attempting Connection ...");
try
{
connection = new Socket(InetAddress.getByName(serverIP),port);
}catch(IOException ioEception)
{
JOptionPane.showMessageDialog(null,"Server Might Be Down!","Warning",JOptionPane.WARNING_MESSAGE);
}
status.setText("Connected to: " + connection.getInetAddress().getHostName());
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
whileChatting();
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
}
private void whileChatting() throws IOException
{
jTextField1.setEditable(true);
do{
try
{
message = (String) input.readObject();
chatArea.append("\n"+message);
}
catch(ClassNotFoundException classNotFoundException)
{
}
}while(!message.equals("Client - END"));
}
private void sendMessage(String message)
{
try
{
output.writeObject("Client - " + message);
output.flush();
chatArea.append("\nClient - "+message);
}
catch(IOException ioException)
{
chatArea.append("\n Unable to Send Message");
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextArea chatArea;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
private javax.swing.JLabel status;
// End of variables declaration//GEN-END:variables
}
…
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MainServer extends javax.swing.JFrame {
private ObjectOutputStream output;
private ObjectInputStream input;
private Socket connection;
private ServerSocket server;
private int totalClients = 100;
private int port = 6789;
public MainServer() {
initComponents();
this.setTitle("Server");
this.setVisible(true);
status.setVisible(true);
}
public void startRunning()
{
try
{
server=new ServerSocket(port, totalClients);
while(true)
{
try
{
status.setText(" Waiting for Someone to Connect...");
connection=server.accept();
status.setText(" Now Connected to "+connection.getInetAddress().getHostName());
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
whileChatting();
}catch(EOFException eofException)
{
}
}
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
}
private void whileChatting() throws IOException
{
String message="";
jTextField1.setEditable(true);
do{
try
{
message = (String) input.readObject();
chatArea.append("\n"+message);
}catch(ClassNotFoundException classNotFoundException)
{
}
}while(!message.equals("Client - END"));
}
@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
chatArea = new javax.swing.JTextArea();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
status = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setLayout(null);
chatArea.setColumns(20);
chatArea.setRows(5);
jScrollPane1.setViewportView(chatArea);
jPanel1.add(jScrollPane1);
jScrollPane1.setBounds(30, 110, 360, 270);
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jPanel1.add(jTextField1);
jTextField1.setBounds(30, 50, 270, 30);
jButton1.setText("Send");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel1.add(jButton1);
jButton1.setBounds(310, 50, 80, 30);
status.setText("...");
jPanel1.add(status);
status.setBounds(30, 80, 300, 40);
jLabel2.setForeground(new java.awt.Color(255, 255, 255));
jLabel2.setText("Write your text here");
jPanel1.add(jLabel2);
jLabel2.setBounds(30, 30, 150, 20);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bg7.jpg"))); // NOI18N
jPanel1.add(jLabel1);
jLabel1.setBounds(0, 0, 420, 405);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 417, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)
);
setSize(new java.awt.Dimension(417, 425));
setLocationRelativeTo(null);
}// //GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
sendMessage(jTextField1.getText());
jTextField1.setText("");
}//GEN-LAST:event_jButton1ActionPerformed
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
sendMessage(jTextField1.getText());
jTextField1.setText("");
}//GEN-LAST:event_jTextField1ActionPerformed
private void sendMessage(String message)
{
try
{
output.writeObject("Server - " + message);
output.flush();
chatArea.append("\nServer - "+message);
}
catch(IOException ioException)
{
chatArea.append("\n Unable to Send Message");
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextArea chatArea;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
private javax.swing.JLabel status;
// End of variables declaration//GEN-END:variables
}
...