-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMulticastChat.java
More file actions
89 lines (69 loc) · 3.32 KB
/
MulticastChat.java
File metadata and controls
89 lines (69 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package multicastchat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.SocketException;
import java.util.Scanner;
/**
*
* @author Aninh
*/
public class MulticastChat {
private static final String TERMINATE = "Exit";
static String nome;
static volatile boolean finished = false;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
InetAddress ipGrupo = null; //Ip do multicast com o qual vai se comunicar
MulticastSocket socketServidor = null; //Socket do servidor
DatagramPacket sendPacket = null;
if(args.length != 2){
System.out.print("\nInvalid parameters!!\nUse MulticastChat <multicast-host> <server_port>");
System.exit(1);
}
try {
System.out.print("Starting MulticastChar.... configured to send data to "+ args[0]+" at por "+args[1]);
ipGrupo = InetAddress.getByName(args[0]); //Pega o endereco ip do host multicast
int portaServidor = Integer.parseInt(args[1]); //Passa a porta para int
Scanner input = new Scanner(System.in); //Cria um scanner
byte[] sendData = new byte [65507];
System.out.print("\nDigite seu nome: ");
nome = input.nextLine();
socketServidor = new MulticastSocket(portaServidor);//Inicializa nosso socket Multicast e passa como argumento a porta do servidor Multicast
socketServidor.setTimeToLive(0); //Seta um tempo de vide para enviar mesagem para o socket Multicast
socketServidor.joinGroup(ipGrupo);//Se conecta ao grupo multicast
Thread thread = new Thread(new ReadThread(socketServidor,ipGrupo,portaServidor));//Cria um novo thread para receber as mensagens
thread.start();
System.out.print("Escreva suas mensagens...\n");
while (true) {
String mensagem;
mensagem = input.nextLine();
//Se a mensagem for igual "Terminate" ele finaliza a comunicação
if(mensagem.equalsIgnoreCase(MulticastChat.TERMINATE)){
finished = true;
socketServidor.leaveGroup(ipGrupo);//Sai do grupo
socketServidor.close();//Fecha o socket de comunicação
break;
}
mensagem = nome + " : " + mensagem;
sendData = mensagem.getBytes(); //Transforma a mensagem em bytes
sendPacket = new DatagramPacket(sendData,sendData.length,ipGrupo,portaServidor);
socketServidor.send(sendPacket);
}
} catch (SocketException se) {
System.out.println("Erro ao criar o socket. ");
se.printStackTrace();
} catch (IOException ie){
System.out.println("Erro ao ler ou escrever no socket. ");
ie.printStackTrace();
}
}
}