0% found this document useful (0 votes)
14 views3 pages

Serveur Multithread Entier

The document describes a client-server program for socket communication in Java. The ServeurMT class creates a server socket that listens for client connections on port 12345. When a client connects, a new ServeurMT thread is launched to handle communication with that client. The client code connects to the server, writes a number, and reads the response. The updated server code counts the number of connected clients and prints client connection information.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Serveur Multithread Entier

The document describes a client-server program for socket communication in Java. The ServeurMT class creates a server socket that listens for client connections on port 12345. When a client connects, a new ServeurMT thread is launched to handle communication with that client. The client code connects to the server, writes a number, and reads the response. The updated server code counts the number of connected clients and prints client connection information.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package MyServeurMT;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServeurMT extends Thread {


private Socket s;

public static void main(String[] args) throws IOException {


ServerSocket ss = new ServerSocket(12345); // Crée un serveur écoutant sur
le port 12345
System.out.println("En attente de la connexion...");

Socket clientSocket = ss.accept(); // Attente de la connexion du client

ServeurMT smt = new ServeurMT(clientSocket);


smt.start();
}

public ServeurMT(Socket socket) {


this.s = socket;
}

public void run() {


try {
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
System.out.println("J'attends de lire le nombre");
int nb = is.read();
System.out.println("J'ai reçu le nombre " + nb);
int rslt = nb + 1;
System.out.println("J'envoie le nombre " + rslt);
os.write(rslt);
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

-------------------------------------------------------------------

package MyClient;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Client {

public static void main(String[] args) throws IOException{


Socket soc = new Socket("localhost",12345); // Connexion au serveur
// Crée des flux de communication avec le serveur
InputStream is = soc.getInputStream();
OutputStream os = soc.getOutputStream();
// Envoie un message au serveur
os.write(3);
// Lit la réponse du serveur
int r = is.read();
System.out.println("la rpns "+r);
// Fermeture des flux et de la socket
soc.close();

-------------------------------------

package MyServeurMT;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServeurMT extends Thread {


private static int clientCount = 0;
private Socket s;

public static void main(String[] args) throws IOException {


ServerSocket ss = new ServerSocket(12345); // Crée un serveur écoutant sur
le port 12345
System.out.println("En attente de la connexion...");

while (true) {
Socket clientSocket = ss.accept(); // Attente de la connexion du client
System.out.println("Client connecté depuis l'adresse IP : " +
clientSocket.getInetAddress() +
", Port : " + clientSocket.getLocalPort());

clientCount++;
System.out.println("Nombre total de clients : " + clientCount);

ServeurMT smt = new ServeurMT(clientSocket);


smt.start();
}
}

public ServeurMT(Socket socket) {


this.s = socket;
}

public void run() {


try {
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
System.out.println("J'attends de lire le nombre");
int nb = is.read();
System.out.println("J'ai reçu le nombre " + nb);
int rslt = nb + 1;
System.out.println("J'envoie le nombre " + rslt);
os.write(rslt);
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

You might also like