synchronized su awarenes e chat via tcp

This commit is contained in:
2014-12-02 21:50:57 +01:00
parent 1ef2207245
commit 40a630623a
7 changed files with 296 additions and 2 deletions

View 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;
}

View 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;
}

View 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();
}
}

View 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();
}
}

View 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;
}

View 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>();
}