1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
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.Socket;
import java.net.UnknownHostException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Irc
{
public static JFrame f;
public static void main (String args[]) throws UnknownHostException, IOException
{
f = new JFrame();
f = creationFenetre();
f.setVisible(true);
if(connexion("irc.epiknet.org")==true)
{
System.out.println("Connexion réussi");
}
else
{
System.out.println("Epik fail");
}
}
/* fonction pour créer la fenêtre de chat */
public static JFrame creationFenetre()
{
JFrame w = new JFrame();
JPanel p = new JPanel();
/* servira a afficher la liste des connectes */
JEditorPane co = new JEditorPane();
co.setSize(190,430);
co.setEditable(false);
/* servira a afficher la discussion */
JEditorPane disc = new JEditorPane ();
disc.setSize(600,430);
disc.setName("disc");
disc.setAutoscrolls(true);
disc.setEditable(false);
/* sera la zone ou l'utilisateur tapera son texte */
JEditorPane edit = new JEditorPane();
edit.setSelectedTextColor(Color.yellow);
p.setLayout(new BorderLayout());
/* on positionne les 3 champs fraichement créés */
p.add(co,BorderLayout.WEST);
p.add(disc,BorderLayout.EAST);
p.add(edit,BorderLayout.SOUTH);
w.setTitle("Irc v0.0");
w.setResizable(false);
Dimension d = new Dimension(800,500);
w.setSize(d);
w.setContentPane(p);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return w;
}
public static boolean connexion(String serv) throws UnknownHostException, IOException
{
Thread thread = new Thread();
try
{
Socket s = new Socket(serv,6667);
String str;
String temp;
String nick="test_de_nick_avec_toto";
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter output = new PrintWriter(s.getOutputStream());
/* envoie de la commande NICK */
output.println("NICK "+nick);
while(thread != null && (str = input.readLine()) != null)
{
/* test pour savoir si le message recu est un message PING */
if (str.indexOf("PING")==0)
{
String temp2[]= str.split(":");
output.println("PONG :"+temp2[1]);
output.flush();
}
else
{
/* sinon on traite le message */
/* on récupère les anciens message*/
temp=((JEditorPane)f.getContentPane().getComponents()[1]).getText();
temp+="\n";
temp+=str;
/* et on les réaffiche avec le nouveau en plus */
((JEditorPane)f.getContentPane().getComponents()[1]).setText(temp);
}
}
return(true);
}
catch(UnknownHostException e)
{
System.out.println("Erreur lors de la connexion");
return(false);
}
}
} |
Partager