Serveur et client TCP fonctionnel
This commit is contained in:
parent
4391f2c419
commit
fae7931a77
6 changed files with 19 additions and 162 deletions
|
@ -2,5 +2,4 @@ package clavardage;
|
||||||
|
|
||||||
public class gestionnaireClavardage {
|
public class gestionnaireClavardage {
|
||||||
sessions =
|
sessions =
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package reseau;
|
package reseau;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,6 +10,7 @@ public class TCPServer {
|
||||||
ServerSocket socket;
|
ServerSocket socket;
|
||||||
private String host;
|
private String host;
|
||||||
private int port;
|
private int port;
|
||||||
|
private List<Socket> clients = new ArrayList<Socket>();
|
||||||
private InetAddress address;
|
private InetAddress address;
|
||||||
|
|
||||||
public TCPServer (String host, int backlog, int port) {
|
public TCPServer (String host, int backlog, int port) {
|
||||||
|
@ -30,37 +33,26 @@ public class TCPServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Socket accept() throws IOException{
|
public void accept() {
|
||||||
return this.socket.accept();
|
Socket ssocket = null;
|
||||||
|
try {
|
||||||
|
ssocket = this.socket.accept();
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
System.out.print("Erreur lors de la connexion avec un client");
|
||||||
|
}
|
||||||
|
new TCPServerThread(ssocket);
|
||||||
|
this.clients.add(ssocket);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
TCPServer server = new TCPServer("localhost", 5, 1999);
|
TCPServer server = new TCPServer("localhost", 5, 1999);
|
||||||
Socket link = null;
|
|
||||||
BufferedReader input = null;
|
while(true) {
|
||||||
PrintWriter output = null;
|
server.accept();
|
||||||
try {
|
|
||||||
link = server.accept();
|
|
||||||
input = new BufferedReader(new InputStreamReader(link.getInputStream()));
|
|
||||||
output = new PrintWriter(link.getOutputStream(),true);
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
System.out.print("Error while connecting to client");
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
String message = input.readLine();
|
|
||||||
output.print(message);
|
|
||||||
output.flush();
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
System.out.print("Error while communicating with client");
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
link.close();
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
System.out.print("Error while closing connection");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ import java.io.PrintWriter;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
public class TCPServerThread extends tcpClient implements Runnable{
|
public class TCPServerThread extends TCPClient implements Runnable{
|
||||||
|
|
||||||
Thread thread;
|
Thread thread;
|
||||||
|
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
package reseau;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.*;
|
|
||||||
|
|
||||||
public class TCPServerWithThreads extends TCPServer{
|
|
||||||
private TCPServerThread[] clientThreads;
|
|
||||||
|
|
||||||
public TCPServerWithThreads (String host, int backlog, int port) {
|
|
||||||
super(host, backlog, port);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Socket accept() {
|
|
||||||
Socket ssocket = null;
|
|
||||||
try {
|
|
||||||
ssocket = this.socket.accept();
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
|
|
||||||
}
|
|
||||||
new TCPServerThread(ssocket);
|
|
||||||
return ssocket;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
|
|
||||||
TCPServerWithThreads server = new TCPServerWithThreads("localhost", 5, 19999);
|
|
||||||
|
|
||||||
while(true) {
|
|
||||||
server.accept();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,11 +0,0 @@
|
||||||
package reseau;
|
|
||||||
|
|
||||||
public class broadcast {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
System.out.print("Quelquechose");
|
|
||||||
System.out.print("Quelquechose 2 le retour");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,91 +0,0 @@
|
||||||
package reseau;
|
|
||||||
import java.net.*;
|
|
||||||
import java.io.*;
|
|
||||||
|
|
||||||
public class tcpClient {
|
|
||||||
String host = null;
|
|
||||||
int port = 0;
|
|
||||||
Socket socket = null;
|
|
||||||
BufferedReader input = null;
|
|
||||||
PrintWriter output = null;
|
|
||||||
|
|
||||||
public tcpClient() {
|
|
||||||
this.host = null;
|
|
||||||
this.port = 0;
|
|
||||||
this.socket = null;
|
|
||||||
this.input = null;
|
|
||||||
this.output = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public tcpClient(String host, int port) {
|
|
||||||
this.host = host;
|
|
||||||
this.port = port;
|
|
||||||
try {
|
|
||||||
this.socket = new Socket(host, port);
|
|
||||||
}
|
|
||||||
catch (UnknownHostException e) {
|
|
||||||
System.out.print("Could not find host");
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
System.out.print("Could not create socket");
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
this.input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
System.out.print("Error while reading input stream");
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
this.output = new PrintWriter(socket.getOutputStream(),true);
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
System.out.print("Error while reading output stream");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void send(String message) {
|
|
||||||
this.output.print(message);
|
|
||||||
this.output.flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String receive() {
|
|
||||||
String message = null;
|
|
||||||
try {
|
|
||||||
message = input.readLine();
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
System.out.print("Error while reading buffer");
|
|
||||||
}
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stop() {
|
|
||||||
try {
|
|
||||||
this.socket.close();
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
System.out.print("Error while closing connection");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void changePort(int port) throws IOException {
|
|
||||||
this.stop();
|
|
||||||
this.port = port;
|
|
||||||
try {
|
|
||||||
this.socket = new Socket(host, port);
|
|
||||||
}
|
|
||||||
catch (UnknownHostException e) {
|
|
||||||
System.out.print("Could not find host");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
tcpClient client = new tcpClient("localhost", 19999);
|
|
||||||
client.send("Bonjour\n");
|
|
||||||
System.out.print(client.receive());
|
|
||||||
client.stop();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue