- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
-
- class Client2 extends Frame {
-
- //Declaration des attributs
- TextField t1;
- TextField adIP;
- Label adrIP;
- Label Nom;
- TextField nom;
- Button b1;
- static int a;
- Socket mySock;
- PrintStream ps;
- Label lText;
- Label lBvenue;
- String ligne;
- static TextArea ta;
-
-
- public Client2() {
-
-
- //Titre de la fenetre
- setTitle("Client");
-
- //Fermeture de la fenetre
- addWindowListener( new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
-
- //Instanciation des attributs
- t1 = new TextField();
- adIP = new TextField();
- nom = new TextField();
- b1 = new Button("Envoyer");
- lText = new Label("Texte à envoyer :");
- adrIP = new Label("Entrez l'adresse IP ");
- Nom = new Label("Entrez votre nom ");
- lBvenue = new Label("Bienvenue sur votre application chat");
- ta = new TextArea(10,6);
- ta.setEditable(false);
- a=1;
-
-
- //Definir le layout et creer ses composants
- setLayout(new GridBagLayout());
- GridBagConstraints c = new GridBagConstraints();
- c.gridx = 1;c.gridy = 0;c.gridheight = 1;c.gridwidth = 1;
- c.fill = GridBagConstraints.BOTH;
- c.weightx = 1.0;c.weighty = 1.0;
- c.insets = new Insets(5,5,5,5);
- add(lBvenue,c);
- c.gridy = 1;
- add(Nom,c);
- c.gridy = 2;
- add(nom,c);
- c.gridy = 3;
- add(adrIP,c);
- c.gridy = 4;
- add(adIP,c);
- c.gridy = 5;
- add(lText,c);
- c.gridy = 6;
- add(t1,c);
- c.gridy = 7;
- add(b1,c);
- c.gridy = 8;
- add(ta,c);
-
- //Action sur le bouton b1
- b1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
-
- try
- {
- if (a==1)
- {
- mySock = new Socket(adIP.getText(), 11111);
- ps= new PrintStream(mySock.getOutputStream());
- }
-
- //System.out.println("creation socket");
- ps.print(t1.getText());
- ta.append("<" + nom.getText() + "> " + t1.getText() + "\n");
- ps.println();
- a=1;
- ps.close();
- mySock.close();
- t1.setText("");
- adIP.setEditable(false);
- nom.setEditable(false);
-
- }
- catch(Exception err){err.printStackTrace();}
- }
- });
-
- //Taille de la fenetre
- setBackground(Color.cyan);
- pack();
- setLocation(350,350);
- //rendre la fenetre visible
- setVisible(true);
-
-
-
- }
-
- //Programme principal
- public static void main (String [] args)
- {
- new Client2();
- Socket sock2;
- ServerSocket ss2;
- DataInputStream dis2;
- String ligne;
- String temp = "";
- try
- { ss2 = new ServerSocket(8080);
-
-
- while(true)
-
- {
-
-
- temp = "<Client> ";
- sock2 = ss2.accept();
- dis2 = new DataInputStream(sock2.getInputStream());
- while((ligne =dis2.readLine())!= null)
- {
- temp += ligne;
- }
- temp += "\n";
- ta.append(temp);
- dis2.close();
- sock2.close();
-
-
- }
- }
- catch(Exception err){err.printStackTrace();}
-
-
- }
-
- }
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class Client2 extends Frame {
//Declaration des attributs
TextField t1;
TextField adIP;
Label adrIP;
Label Nom;
TextField nom;
Button b1;
static int a;
Socket mySock;
PrintStream ps;
Label lText;
Label lBvenue;
String ligne;
static TextArea ta;
public Client2() {
//Titre de la fenetre
setTitle("Client");
//Fermeture de la fenetre
addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//Instanciation des attributs
t1 = new TextField();
adIP = new TextField();
nom = new TextField();
b1 = new Button("Envoyer");
lText = new Label("Texte à envoyer :");
adrIP = new Label("Entrez l'adresse IP ");
Nom = new Label("Entrez votre nom ");
lBvenue = new Label("Bienvenue sur votre application chat");
ta = new TextArea(10,6);
ta.setEditable(false);
a=1;
//Definir le layout et creer ses composants
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 1;c.gridy = 0;c.gridheight = 1;c.gridwidth = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;c.weighty = 1.0;
c.insets = new Insets(5,5,5,5);
add(lBvenue,c);
c.gridy = 1;
add(Nom,c);
c.gridy = 2;
add(nom,c);
c.gridy = 3;
add(adrIP,c);
c.gridy = 4;
add(adIP,c);
c.gridy = 5;
add(lText,c);
c.gridy = 6;
add(t1,c);
c.gridy = 7;
add(b1,c);
c.gridy = 8;
add(ta,c);
//Action sur le bouton b1
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try
{
if (a==1)
{
mySock = new Socket(adIP.getText(), 11111);
ps= new PrintStream(mySock.getOutputStream());
}
//System.out.println("creation socket");
ps.print(t1.getText());
ta.append("<" + nom.getText() + "> " + t1.getText() + "\n");
ps.println();
a=1;
ps.close();
mySock.close();
t1.setText("");
adIP.setEditable(false);
nom.setEditable(false);
}
catch(Exception err){err.printStackTrace();}
}
});
//Taille de la fenetre
setBackground(Color.cyan);
pack();
setLocation(350,350);
//rendre la fenetre visible
setVisible(true);
}
//Programme principal
public static void main (String [] args)
{
new Client2();
Socket sock2;
ServerSocket ss2;
DataInputStream dis2;
String ligne;
String temp = "";
try
{ ss2 = new ServerSocket(8080);
while(true)
{
temp = "<Client> ";
sock2 = ss2.accept();
dis2 = new DataInputStream(sock2.getInputStream());
while((ligne =dis2.readLine())!= null)
{
temp += ligne;
}
temp += "\n";
ta.append(temp);
dis2.close();
sock2.close();
}
}
catch(Exception err){err.printStackTrace();}
}
}