tp1 P3 p2 p2p
tp1 P3 p2 p2p
import java.io.*;
import java.net.*;
senderThread.start();
receiverThread.start();
}
}
@Override
public void run() {
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
while (true) {
message = userInput.readLine();
out.println(message);
if (message.equals("exit")) {
break;
}
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
try {
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String message;
while (true) {
message = in.readLine();
if (message == null) {
break;
}
System.out.println("Server: " + message);
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
code serveur :
package s;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Nouveau client connecté!");
String message;
while (true) {
message = in.readLine();
if (message == null || message.equals("exit")) {
break;
}
System.out.println("Client: " + message);
serveur.broadcast("Client: " + message);
}
socket.close();
serveur.clients.remove(this);
} catch (IOException e) {
e.printStackTrace();
}
}