implementazine chat udp multicast
This commit is contained in:
83
socket_chat_udp/Client.java
Normal file
83
socket_chat_udp/Client.java
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
package socket_chat_udp;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.*;
|
||||||
|
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, int ittl) {
|
||||||
|
|
||||||
|
address = iaddress;
|
||||||
|
port= iport;
|
||||||
|
nome = inome;
|
||||||
|
ttl=ittl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void launch (){
|
||||||
|
|
||||||
|
MulticastSocket connection = null;
|
||||||
|
DatagramPacket packet = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
connection = new MulticastSocket(port);
|
||||||
|
connection.setTimeToLive(ttl);
|
||||||
|
connection.joinGroup(InetAddress.getByName(address));
|
||||||
|
|
||||||
|
BufferedReader userinput = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
|
||||||
|
ClientInputManagement cim = new ClientInputManagement(connection);
|
||||||
|
cim.start();
|
||||||
|
|
||||||
|
String message = nome + " si e' connesso";
|
||||||
|
packet = new DatagramPacket(message.getBytes(),message.length(),InetAddress.getByName(address),port);
|
||||||
|
connection.send(packet);
|
||||||
|
|
||||||
|
String command = command(userinput);
|
||||||
|
for (; !("!quit".equals(command)) ;){
|
||||||
|
packet = new DatagramPacket(command.getBytes(),command.length(),InetAddress.getByName(address),port);
|
||||||
|
connection.send(packet);
|
||||||
|
command = command(userinput);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
finally {
|
||||||
|
connection.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
private int ttl ;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
45
socket_chat_udp/ClientInputManagement.java
Normal file
45
socket_chat_udp/ClientInputManagement.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package socket_chat_udp;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.DatagramPacket;
|
||||||
|
import java.net.MulticastSocket;
|
||||||
|
import java.util.Arrays;
|
||||||
|
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 (MulticastSocket input){
|
||||||
|
|
||||||
|
i=input;
|
||||||
|
buffer = new byte[100];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (true){
|
||||||
|
try {
|
||||||
|
DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
|
||||||
|
Arrays.fill(buffer, new Integer(0).byteValue());
|
||||||
|
i.receive(packet);
|
||||||
|
System.out.println(new String(buffer).trim());
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Bye");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final MulticastSocket i;
|
||||||
|
private byte[] buffer;
|
||||||
|
|
||||||
|
}
|
||||||
24
socket_chat_udp/LaunchClient.java
Normal file
24
socket_chat_udp/LaunchClient.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package socket_chat_udp;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with xgiovio.macbookair.
|
||||||
|
* User: xgiovio
|
||||||
|
* Date: 02/12/14
|
||||||
|
* Time: 17:28
|
||||||
|
*/
|
||||||
|
public class LaunchClient {
|
||||||
|
|
||||||
|
public static void main (String[] args) {
|
||||||
|
|
||||||
|
|
||||||
|
// start vm with -Djava.net.preferIPv4Stack=true
|
||||||
|
Client c1 = new Client("mario","239.10.11.12",40000,10);
|
||||||
|
c1.launch();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user