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
| import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.*;
public class Socket1 extends Applet {
Socket connexion;
BufferedReader entree;
Label erreurconnect = new Label ("erreur connexion");
Label erreurenvoi = new Label ("erreur envoi");
Label erreurreception = new Label ("erreur réception");
Label erreurfermeture = new Label ("erreur fermeture");
String s;
Label erreurconnect2 = new Label (s);
// Méthode appelée par le navigateur lorsque l'applet est chargée
public void init (){
setBackground(Color.gray);
}
public void connect(int num_port){
try {
URL urlServer = getCodeBase();
s=urlServer.getHost();
connexion = new Socket(s,num_port);
add (erreurconnect2);
}
catch (Throwable t)
{
add (erreurconnect);
}
}
public void envoi(String données_envoi){
try {
PrintWriter sortie = new PrintWriter( connexion.getOutputStream(), true);
sortie.write(données_envoi );
sortie.flush();
}
catch (IOException e)
{
add (erreurenvoi);
}
}
public String reception(){
char[] cbuf=new char[10];
String retour="";
try {
entree = new BufferedReader( new InputStreamReader(connexion.getInputStream()) );
int nbre=entree.read(cbuf);
for (int i=0;i<nbre;i++){
retour=retour+cbuf[i];
}
return retour;
}
catch (IOException e)
{
add (erreurreception);
return "0";
}
}
public void fermer(){
try {
connexion.close();
}
catch (IOException e)
{
add (erreurfermeture);
}
}
} |
Partager