tp1 p4 p2p
tp1 p4 p2p
tp1 p4 p2p
package tp1.p4.cherbal;
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 serveur;
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é!");
@Override
public void run() {
try {
out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
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();
}
}