système de messagerie multithreadé bidirectionnel de l'espace
This commit is contained in:
parent
a9b579af61
commit
8362b82a88
6 changed files with 101 additions and 56 deletions
BIN
src/chat/ReceiveThread.class
Normal file
BIN
src/chat/ReceiveThread.class
Normal file
Binary file not shown.
BIN
src/chat/SendThread.class
Normal file
BIN
src/chat/SendThread.class
Normal file
Binary file not shown.
Binary file not shown.
|
|
@ -9,26 +9,108 @@ import java.net.ServerSocket;
|
|||
import java.net.Socket;
|
||||
import java.net.DatagramSocket;
|
||||
|
||||
class SendThread extends Thread {
|
||||
String username;
|
||||
String address;
|
||||
String message;
|
||||
int port;
|
||||
|
||||
SendThread(String in_username, String in_address, String in_message, int in_port)
|
||||
{
|
||||
username = in_username;
|
||||
address = in_address;
|
||||
message = in_message;
|
||||
port = in_port;
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
Socket link;
|
||||
Boolean connected = false;
|
||||
PrintWriter out;
|
||||
|
||||
while(!connected)
|
||||
{
|
||||
try
|
||||
{
|
||||
link = new Socket(address, port);
|
||||
out = new PrintWriter(link.getOutputStream(),true);
|
||||
out.println(username + ":" + message);
|
||||
|
||||
connected = true;
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("nik1!");
|
||||
connected = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class ReceiveThread extends Thread {
|
||||
|
||||
int port;
|
||||
ReceiveThread(int in_port)
|
||||
{
|
||||
port = in_port;
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
ServerSocket servSocket = new ServerSocket(port);
|
||||
Boolean exit = false;
|
||||
|
||||
while(!exit)
|
||||
{
|
||||
Socket link = servSocket.accept();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));
|
||||
String message = in.readLine();
|
||||
if(message == "exit")
|
||||
exit = true;
|
||||
else
|
||||
System.out.println(message);
|
||||
|
||||
link.close();
|
||||
}
|
||||
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("nik2 !");
|
||||
}
|
||||
}
|
||||
}
|
||||
public class socket_client {
|
||||
public static void main (String [] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
System.out.println("Connecting to server...");
|
||||
Socket link = new Socket("localhost", 1234);//remplacer localhost par l'adresse IP du serveur
|
||||
System.out.println("Establishing I/O streams...");
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));
|
||||
PrintWriter out = new PrintWriter(link.getOutputStream(),true);
|
||||
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
|
||||
while(true)
|
||||
{
|
||||
String message = console.readLine();
|
||||
out.println("CLIENT:" + message);
|
||||
}
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("nik!");
|
||||
}
|
||||
/* System.out.println("Connecting to server...");
|
||||
System.out.println("Establishing I/O streams...");*/
|
||||
try
|
||||
{
|
||||
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
|
||||
System.out.print("Entrez l'adresse de destination:");
|
||||
String address = console.readLine();
|
||||
System.out.print("Entrez le numéro de port source:");
|
||||
int sport = Integer.parseInt(console.readLine());
|
||||
System.out.print("Entrez le numéro de port destination:");
|
||||
int dport = Integer.parseInt(console.readLine());
|
||||
System.out.print("Entrez votre nom d'utilisateur:");
|
||||
String username = console.readLine();
|
||||
SendThread t1;
|
||||
ReceiveThread t2 = new ReceiveThread(sport);
|
||||
t2.start();
|
||||
while(true)
|
||||
{
|
||||
String message = console.readLine();
|
||||
t1 = new SendThread(username, address, message, dport);
|
||||
t1.start();
|
||||
}
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("nik3!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -1,37 +0,0 @@
|
|||
//package chat;
|
||||
import java.net.*;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public class socket_server {
|
||||
public static void main (String [] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
Boolean exit = false;
|
||||
ServerSocket servSocket = new ServerSocket(1234);
|
||||
System.out.println("Awaiting connection ... ");
|
||||
Socket link = servSocket.accept();
|
||||
System.out.println("Awaiting data ... ");
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));
|
||||
PrintWriter out = new PrintWriter(link.getOutputStream(),true);
|
||||
|
||||
while(!exit)
|
||||
{
|
||||
String message = in.readLine();
|
||||
if(message == "exit")
|
||||
exit = true;
|
||||
else
|
||||
System.out.println(message);
|
||||
}
|
||||
link.close();
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("nik !");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue