-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServidorTCP.java
More file actions
109 lines (92 loc) · 3.44 KB
/
ServidorTCP.java
File metadata and controls
109 lines (92 loc) · 3.44 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.net.Socket;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.net.ServerSocket;
import java.io.*;
public class ServidorTCP
{
private static void broadcast(String message, Collection<Socket> online, Socket conAtual){
online.stream().filter(skt->!skt.equals(conAtual)).forEach((skt) -> {
try {
//CRIA UM PACOTE DE SA�DA PARA ENVIAR MENSAGENS, ASSOCIANDO-O � CONEX�O (p)
ObjectOutputStream sSerOut = new ObjectOutputStream(skt.getOutputStream());
sSerOut.writeObject(message); //ESCREVE NO PACOTE
System.out.println(" -S- Enviando broadcast...");
sSerOut.flush(); //ENVIA O PACOTE
} catch (Exception e) {
System.out.println(e.toString());
}
});
}
private static void send(String message, Socket conSocket){
try {
//CRIA UM PACOTE DE SA�DA PARA ENVIAR MENSAGENS, ASSOCIANDO-O � CONEX�O (p)
ObjectOutputStream sSerOut = new ObjectOutputStream(conSocket.getOutputStream());
sSerOut.writeObject(message); //ESCREVE NO PACOTE
System.out.println(" -S- Enviando mensagem..");
sSerOut.flush(); //ENVIA O PACOTE
} catch (Exception e) {
System.out.println(e.toString());
}
}
private static Object receive(Socket conSocket) throws Exception{
return new ObjectInputStream(conSocket.getInputStream()).readObject();
}
//METODO PRINCIPAL DA CLASSE
public static void main (String args[])
{
while(true)
{
try
{
int PortaServidor = 7000;
//INICIALIZA UM SERVI�O DE ESCUTA POR CONEX�ES NA PORTA ESPECIFICADA
System.out.println(" -S- Aguardando conexao (P:"+PortaServidor+")...");
ServerSocket socktServ = new ServerSocket(PortaServidor);
Boolean fakeBoolean = true;
Set<Socket> online = new HashSet<Socket>();
while (fakeBoolean) {
//ESPERA (BLOQUEADO) POR CONEX�ES
Socket newConSer = socktServ.accept(); //RECEBE CONEX�O E CRIA UM NOVO CANAL (p) NO SENTIDO CONTR�RIO (SERVIDOR -> CLIENTE)
System.out.println(" -S- Conectado ao cliente ->" + newConSer.toString());
online.add(newConSer);
final Socket conSer = newConSer;
Thread thread = new Thread( () -> {
try {
ObjectOutputStream sServOut = new ObjectOutputStream(conSer.getOutputStream());
sServOut.writeObject("Insira seu nome de exibição:");
sServOut.flush();
String name = (String) receive(conSer);
broadcast(name + " entrou na conversa", online, conSer);
Object msgIn;
do {
msgIn = receive(conSer);
System.out.println(" -S- Recebido: " + msgIn.toString());
broadcast(
name + (!msgIn.equals("sair") ? (": " + (String)msgIn) : " está saindo... ;-;") ,
online, conSer
);
} while (!msgIn.equals("sair"));
send("%%d#i#s#c#o#n#n#e#c#t%%", conSer);
//FINALIZA A CONEX�O
online.remove(conSer);
conSer.close();
System.out.println(" -S- Conexao finalizada...");
} catch(EOFException e){
System.out.println("Conexão com cliente perdida abruptamente: " + e.toString());
} catch (Exception e) {
System.out.println(e.toString());
}
});
thread.start();
}
socktServ.close();
}
catch(Exception e) //SE OCORRER ALGUMA EXCESS�O, ENT�O DEVE SER TRATADA (AMIGAVELMENTE)
{
System.out.println(" -S- O seguinte problema ocorreu : \n" + e.toString());
}
}
}
}