السلام عليكم ورحمة الله تعالى وبركاته
قمت بعمل هذا البرنامج الصغير للشات في netbeans7.4 بين السيرفر وشخص آخر (زبون), لكن هذا السيرفر يعمل مع زبون واحد فقط وأريد أن أحوله إلى server multi-clients بحيث يستطيع التحدث مع عذة زبائن في نفس الوقت **لقد وضعت البرنامج في المرفقات أرجو المساعدة وشكرا**
ها هي واجهة السيرفر

;وهذه واجة client

شيفرة السيرفر :
import java.awt.Component;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class Server extends javax.swing.JFrame {
private static ServerSocket serverSocket;
private static int porte;
public static DataInputStream getin;
private static PrintStream getout;
private static String name;
private static String Clientname;
public static Socket socket ;
public static String message;
public Server() {
setTitle("Server");
setBounds(700,100,372, 468);
setResizable(false);
initComponents();
Btnarreter.setEnabled(false);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
PL1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
Tporte = new javax.swing.JTextField();
Tnom = new javax.swing.JTextField();
Btnarreter = new javax.swing.JButton();
Btndemarer = new javax.swing.JButton();
PL2 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
Tmessage = new javax.swing.JTextField();
Btnenvoyer = new javax.swing.JButton();
PL3 = new javax.swing.JPanel();
listeclient = new java.awt.List();
jLabel4 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
Message = new javax.swing.JTextArea();
jLabel5 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
PL1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel1.setText("Porte :");
jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel2.setText("Nom :");
Tporte.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
Tnom.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
Btnarreter.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
Btnarreter.setText("Arrèter");
Btnarreter.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnarreterActionPerformed(evt);
}
});
Btndemarer.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
Btndemarer.setText("Démarer");
Btndemarer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtndemarerActionPerformed(evt);
}
});
javax.swing.GroupLayout PL1Layout = new javax.swing.GroupLayout(PL1);
PL1.setLayout(PL1Layout);
PL1Layout.setHorizontalGroup(
PL1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PL1Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(PL1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(27, 27, 27)
.addGroup(PL1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Tporte, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Tnom, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(PL1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Btnarreter, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Btndemarer, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
PL1Layout.setVerticalGroup(
PL1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PL1Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(PL1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Tnom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Btndemarer))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(PL1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Tporte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Btnarreter))
.addContainerGap(28, Short.MAX_VALUE))
);
PL2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel3.setText("Méssage à envoyer :");
Btnenvoyer.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
Btnenvoyer.setText("Envoyer");
Btnenvoyer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnenvoyerActionPerformed(evt);
}
});
javax.swing.GroupLayout PL2Layout = new javax.swing.GroupLayout(PL2);
PL2.setLayout(PL2Layout);
PL2Layout.setHorizontalGroup(
PL2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PL2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(PL2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PL2Layout.createSequentialGroup()
.addComponent(Tmessage)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Btnenvoyer, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(PL2Layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
PL2Layout.setVerticalGroup(
PL2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PL2Layout.createSequentialGroup()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(PL2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PL2Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
.addComponent(Btnenvoyer)
.addGap(87, 87, 87))
.addGroup(PL2Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Tmessage)
.addContainerGap())))
);
PL3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel4.setText(" Liste de client");
Message.setColumns(20);
Message.setRows(5);
jScrollPane1.setViewportView(Message);
jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel5.setText("Message réçu :");
javax.swing.GroupLayout PL3Layout = new javax.swing.GroupLayout(PL3);
PL3.setLayout(PL3Layout);
PL3Layout.setHorizontalGroup(
PL3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PL3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(PL3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PL3Layout.createSequentialGroup()
.addGap(0, 9, Short.MAX_VALUE)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel5))
.addGroup(PL3Layout.createSequentialGroup()
.addComponent(listeclient, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
PL3Layout.setVerticalGroup(
PL3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PL3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(PL3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1)
.addGroup(PL3Layout.createSequentialGroup()
.addGroup(PL3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PL3Layout.createSequentialGroup()
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(PL3Layout.createSequentialGroup()
.addGap(0, 12, Short.MAX_VALUE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(1, 1, 1)))
.addComponent(listeclient, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(PL1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(PL2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(PL3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(PL1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(PL2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(PL3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void BtndemarerActionPerformed(java.awt.event.ActionEvent evt) {
// String j;
// j=Tporte.getText();
// porte = Integer.parseInt(j) ;
porte = Integer.valueOf(Tporte.getText()).intValue();
name = Tnom.getText();
try{
serverSocket = new ServerSocket(porte);
socket = serverSocket.accept();
getin = new DataInputStream(socket.getInputStream());
getout = new PrintStream(socket.getOutputStream());
getout.println(name);
Clientname = getin.readLine();
Message.append("You are connecting with : "+Clientname+"\n");
Message.append("\n");
listeclient.addItem(Clientname);
}catch(Exception ex){
JOptionPane.showMessageDialog(this, "Error in connecting...!!! ");
}
Thread th = new acceptread();
th.start();
Btndemarer.setEnabled(false);
Btnarreter.setEnabled(true);
}
private void BtnenvoyerActionPerformed(java.awt.event.ActionEvent evt) {
try{
getout.println(Tmessage.getText());
Tmessage.setText("");
}catch(Exception ex){
JOptionPane.showMessageDialog(this, "Error in sending the message.....!!! ");
}
}
private void BtnarreterActionPerformed(java.awt.event.ActionEvent evt) {
try{
socket.close();
serverSocket.close();
}catch(Exception ex){
JOptionPane.showMessageDialog(this, "Error in Disconnecting.....!!! ");
}
Btndemarer.setEnabled(true);
}
class acceptread extends Thread{
public void run(){
while(true){
try{
message= getin.readLine();
if (message == null){
Message.append(Clientname+" say : good bye"+ "\n");
break;
}
else{
Message.append(Clientname+" say : "+message+ "\n");
}
}catch(Exception ex){
System.out.println(" Error in reading the message...!!!");
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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(Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Server.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 Server().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Btnarreter;
private javax.swing.JButton Btndemarer;
private javax.swing.JButton Btnenvoyer;
public static javax.swing.JTextArea Message;
private javax.swing.JPanel PL1;
private javax.swing.JPanel PL2;
private javax.swing.JPanel PL3;
private javax.swing.JTextField Tmessage;
private javax.swing.JTextField Tnom;
private javax.swing.JTextField Tporte;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JScrollPane jScrollPane1;
private java.awt.List listeclient;
// End of variables declaration
}شيفرة الزبون :
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JOptionPane;
public class Client extends javax.swing.JFrame {
private static int porte;
public static DataInputStream getin;
private static PrintStream getout;
private static String name;
private static String ip;
private static String servername;
public static Socket socket ;
public Client() {
setBounds(200,100,425,468);
setResizable(false); //on intérde la redimentionnement de fenètre
initComponents();
Btndeconnecter.setEnabled(false);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
PL1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
Tnom = new javax.swing.JTextField();
Tip = new javax.swing.JTextField();
Tporte = new javax.swing.JTextField();
Btnconnecter = new javax.swing.JButton();
Btndeconnecter = new javax.swing.JButton();
PL2 = new javax.swing.JPanel();
jLabel4 = new javax.swing.JLabel();
Tmessage = new javax.swing.JTextField();
Btnenvoyer = new javax.swing.JButton();
PL3 = new javax.swing.JPanel();
jLabel5 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
Messagerecu = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Client");
PL1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel1.setText("IP :");
jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel2.setText("Porte :");
jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel3.setText("Nom :");
Tnom.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
Tip.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
Tporte.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
Btnconnecter.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
Btnconnecter.setText("Connect");
Btnconnecter.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnconnecterActionPerformed(evt);
}
});
Btndeconnecter.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
Btndeconnecter.setText(" Disconnect ");
Btndeconnecter.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtndeconnecterActionPerformed(evt);
}
});
javax.swing.GroupLayout PL1Layout = new javax.swing.GroupLayout(PL1);
PL1.setLayout(PL1Layout);
PL1Layout.setHorizontalGroup(
PL1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PL1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(PL1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PL1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jLabel2))
.addGap(19, 19, 19)
.addGroup(PL1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(Tip, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
.addComponent(Tnom, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Tporte))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(PL1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(Btnconnecter, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Btndeconnecter, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
PL1Layout.setVerticalGroup(
PL1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PL1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(PL1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Tnom))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(PL1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Tip))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(PL1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Tporte))
.addGap(33, 33, 33))
.addGroup(PL1Layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(Btnconnecter)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Btndeconnecter)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
PL2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel4.setText("Méssage à envoyer :");
Tmessage.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
Btnenvoyer.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
Btnenvoyer.setText("Envoyer");
Btnenvoyer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnenvoyerActionPerformed(evt);
}
});
javax.swing.GroupLayout PL2Layout = new javax.swing.GroupLayout(PL2);
PL2.setLayout(PL2Layout);
PL2Layout.setHorizontalGroup(
PL2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PL2Layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(Tmessage)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Btnenvoyer, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(PL2Layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
PL2Layout.setVerticalGroup(
PL2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PL2Layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
.addGroup(PL2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Tmessage, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Btnenvoyer))
.addContainerGap())
);
PL3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
PL3.setForeground(new java.awt.Color(5, 0, 5));
jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel5.setText("Méssage reçu :");
Messagerecu.setColumns(20);
Messagerecu.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
Messagerecu.setRows(5);
jScrollPane1.setViewportView(Messagerecu);
javax.swing.GroupLayout PL3Layout = new javax.swing.GroupLayout(PL3);
PL3.setLayout(PL3Layout);
PL3Layout.setHorizontalGroup(
PL3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PL3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PL3Layout.createSequentialGroup()
.addContainerGap(103, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
PL3Layout.setVerticalGroup(
PL3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(PL3Layout.createSequentialGroup()
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(PL1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(PL2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(PL3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(PL1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(PL2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(PL3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void BtnconnecterActionPerformed(java.awt.event.ActionEvent evt) {
name = Tnom.getText();
ip = Tip.getText();
porte = Integer.valueOf(Tporte.getText()).intValue();
try{
socket = new Socket(ip, porte);
getin = new DataInputStream(socket.getInputStream());
getout = new PrintStream(socket.getOutputStream());
getout.println(name);
servername = getin.readLine();
Messagerecu.append("You are connecting with : "+servername+"\n");
Messagerecu.append("\n");
}catch(Exception ex){
JOptionPane.showMessageDialog(this, "Error in connecting....!!! ");
}
Thread th = new acceptread();
th.start();
Btnconnecter.setEnabled(false);
Btndeconnecter.setEnabled(true);
}
private void BtndeconnecterActionPerformed(java.awt.event.ActionEvent evt) {
try{
socket.close();
}catch(Exception ex){
JOptionPane.showMessageDialog(this, "Error in Diconnecting....!!! ");
}
Btnconnecter.setEnabled(true);
}
private void BtnenvoyerActionPerformed(java.awt.event.ActionEvent evt) {
try{
getout.println(Tmessage.getText());
Tmessage.setText("");
}catch(Exception ex){
JOptionPane.showMessageDialog(this, "Error in sending the message....!!! ");
}
}
class acceptread extends Thread{
public void run(){
while(true){
try{
message= getin.readLine();
if (message == null){
Messagerecu.append(servername+" say : Good bye"+ "\n");
break;
}
else{
Messagerecu.append(servername+" say : "+message+ "\n");
}
}catch(Exception ex){
System.out.println(" Error in reading the message..!!");
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Client.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 Client().setVisible(true);
}
});
}
private static String message;
// Variables declaration - do not modify
private javax.swing.JButton Btnconnecter;
private javax.swing.JButton Btndeconnecter;
private javax.swing.JButton Btnenvoyer;
public static javax.swing.JTextArea Messagerecu;
private javax.swing.JPanel PL1;
private javax.swing.JPanel PL2;
private javax.swing.JPanel PL3;
private javax.swing.JTextField Tip;
private javax.swing.JTextField Tmessage;
private javax.swing.JTextField Tnom;
private javax.swing.JTextField Tporte;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}وشكرا جزيلا لكم جميعا
بارك الله فيكم
