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
|
import java.io.*;
import java.net.*;
public class Client extends Thread
{
Socket socket =null;
int port=5000;
String adresse=null;
InetAddress ip=null;
public Client(InetAddress a){ip=a;}
public Client(String s){adresse=s;}
public void run()
{
try
{
System.out.println("Client actif");
Reader reader= new InputStreamReader(System.in);
BufferedReader clavier=new BufferedReader(reader);
if (ip!=null) socket = new Socket(ip, port);
else socket = new Socket(adresse, port);
System.out.println("SOCKET = " + socket);
PrintWriter buffer_out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str =null;
while (true )
{
str=clavier.readLine();
buffer_out.println(str);
if (str.equals("$")||str.equals("$$") ) break;
str=in.readLine();
if (str != null) {
System.out.println("lecture de " + str);
}
}
in.close()
buffer_out.close();
socket.close();
}
catch(IOException e)
{
System.out.println("Erreur client");
}
}
public static void main(String[] args)
{
Client client=new Client("127.0.0.1");
client.start();
}
} |
Partager