synchronized su awarenes e chat via tcp
This commit is contained in:
83
socket_chat_tcp/Client.java
Normal file
83
socket_chat_tcp/Client.java
Normal file
@@ -0,0 +1,83 @@
|
||||
package socket_chat_tcp;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Created with xgiovio.macbookair.
|
||||
* User: xgiovio
|
||||
* Date: 02/12/14
|
||||
* Time: 13:54
|
||||
*/
|
||||
public class Client {
|
||||
|
||||
private static final Logger l = Logger.getLogger(ClientInputManagement.class.getName());
|
||||
|
||||
public Client (String inome ,String iaddress, int iport) {
|
||||
|
||||
address = iaddress;
|
||||
port= iport;
|
||||
nome = inome;
|
||||
|
||||
}
|
||||
|
||||
public void launch (){
|
||||
|
||||
Socket connection = null;
|
||||
|
||||
try {
|
||||
connection = new Socket(address,port);
|
||||
DataOutputStream o = new DataOutputStream(new BufferedOutputStream(connection.getOutputStream()));
|
||||
DataInputStream i = new DataInputStream(new BufferedInputStream(connection.getInputStream()));
|
||||
BufferedReader userinput = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
ClientInputManagement cim = new ClientInputManagement(i);
|
||||
cim.start();
|
||||
|
||||
o.writeUTF(nome);
|
||||
o.flush();
|
||||
|
||||
String command = command(userinput);
|
||||
for (; !("!quit".equals(command)) ;){
|
||||
o.writeUTF(command);
|
||||
o.flush();
|
||||
command = command(userinput);
|
||||
}
|
||||
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
finally {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private String command (BufferedReader i){
|
||||
try {
|
||||
System.out.print(">> ");
|
||||
return i.readLine();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
private final String address;
|
||||
private final int port;
|
||||
private final String nome;
|
||||
|
||||
|
||||
}
|
||||
38
socket_chat_tcp/ClientInputManagement.java
Normal file
38
socket_chat_tcp/ClientInputManagement.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package socket_chat_tcp;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Created with xgiovio.macbookair.
|
||||
* User: xgiovio
|
||||
* Date: 02/12/14
|
||||
* Time: 14:04
|
||||
*/
|
||||
public class ClientInputManagement extends Thread {
|
||||
|
||||
private static final Logger l = Logger.getLogger(ClientInputManagement.class.getName());
|
||||
|
||||
public ClientInputManagement (DataInputStream input){
|
||||
i=input;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true){
|
||||
try {
|
||||
String ridden = i.readUTF();
|
||||
System.out.println(ridden);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Bye");
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private final DataInputStream i;
|
||||
|
||||
}
|
||||
24
socket_chat_tcp/LaunchClient.java
Normal file
24
socket_chat_tcp/LaunchClient.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package socket_chat_tcp;
|
||||
|
||||
/**
|
||||
* Created with xgiovio.macbookair.
|
||||
* User: xgiovio
|
||||
* Date: 02/12/14
|
||||
* Time: 17:28
|
||||
*/
|
||||
public class LaunchClient {
|
||||
|
||||
public static void main (String[] args) {
|
||||
|
||||
Client c1 = new Client("mario","localhost",20000);
|
||||
c1.launch();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
20
socket_chat_tcp/LaunchServer.java
Normal file
20
socket_chat_tcp/LaunchServer.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package socket_chat_tcp;
|
||||
|
||||
/**
|
||||
* Created with xgiovio.macbookair.
|
||||
* User: xgiovio
|
||||
* Date: 02/12/14
|
||||
* Time: 18:20
|
||||
*/
|
||||
public class LaunchServer {
|
||||
|
||||
public static void main (String[] args) {
|
||||
|
||||
Server s = new Server(20000);
|
||||
s.launch();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
43
socket_chat_tcp/Server.java
Normal file
43
socket_chat_tcp/Server.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package socket_chat_tcp;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
/**
|
||||
* Created with xgiovio.macbookair.
|
||||
* User: xgiovio
|
||||
* Date: 02/12/14
|
||||
* Time: 17:30
|
||||
*/
|
||||
public class Server {
|
||||
|
||||
public Server ( int iport){
|
||||
port = iport;
|
||||
}
|
||||
|
||||
|
||||
public void launch (){
|
||||
|
||||
try {
|
||||
ServerSocket socket = new ServerSocket(port);
|
||||
|
||||
while (true) {
|
||||
Socket accepted = socket.accept();
|
||||
|
||||
ServerClientHandler sch = new ServerClientHandler(accepted);
|
||||
sch.start();
|
||||
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private final int port;
|
||||
|
||||
}
|
||||
86
socket_chat_tcp/ServerClientHandler.java
Normal file
86
socket_chat_tcp/ServerClientHandler.java
Normal file
@@ -0,0 +1,86 @@
|
||||
package socket_chat_tcp;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.util.Iterator;
|
||||
import java.util.Vector;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Created with xgiovio.macbookair.
|
||||
* User: xgiovio
|
||||
* Date: 02/12/14
|
||||
* Time: 17:36
|
||||
*/
|
||||
public class ServerClientHandler extends Thread {
|
||||
|
||||
|
||||
|
||||
public ServerClientHandler (Socket isclient){
|
||||
sclient=isclient;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
synchronized (vs) {
|
||||
vs.add(sclient);
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
DataInputStream i = new DataInputStream(new BufferedInputStream(sclient.getInputStream()));
|
||||
name = i.readUTF();
|
||||
broadcast(name + " si e' connesso");
|
||||
while (true){
|
||||
String m = i.readUTF();
|
||||
broadcast(name + ": " + m);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
|
||||
}
|
||||
|
||||
finally {
|
||||
try {
|
||||
sclient.close();
|
||||
synchronized (vs){
|
||||
vs.remove(sclient);
|
||||
}
|
||||
broadcast(name + " si e' disonnesso");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void broadcast (String s){
|
||||
Iterator<Socket> it = vs.iterator();
|
||||
for (;it.hasNext();){
|
||||
Socket next = it.next();
|
||||
if (next != sclient) {
|
||||
try {
|
||||
DataOutputStream o = new DataOutputStream(new BufferedOutputStream(next.getOutputStream()));
|
||||
o.writeUTF(s);
|
||||
o.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private String name;
|
||||
private final Socket sclient;
|
||||
private static final Vector<Socket> vs = new Vector<Socket>();
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user