Repository navigation
Expand file tree
/
Copy pathServidor.java
More file actions
157 lines (113 loc) · 3.69 KB
/
Copy pathServidor.java
File metadata and controls
157 lines (113 loc) · 3.69 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.io.*;
import java.net.*;
import java.util.*;
public class Servidor {
static Scanner sc = new Scanner(System.in);
private static int PORTA = 5050; //porta padrao
private static String IP;
private static Map<String, PrintStream> clientes = new HashMap<>();
public static void main(String[] args){
System.out.println("Digite o IP em que deseja abrir o servidor: ");
IP = sc.nextLine();
System.out.println("Digite a porta que deseja abrir: ");
PORTA = sc.nextInt();
System.out.println("Servidor iniciado.");
InetAddress endereco;
try{
ServerSocket server = new ServerSocket(PORTA);
endereco = server.getInetAddress();
while(true){
Socket cliente = server.accept();
System.out.println("Um cliente esta tentando se conectar...");
new Thread(new ClienteHandler(cliente)).start();
}
} catch(IOException e){
System.out.println(e + "Erro");
}
}
private static class ClienteHandler implements Runnable {
Socket socket;
PrintStream saida;
String nome;
public ClienteHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run(){
try (Scanner entrada = new Scanner(socket.getInputStream())) {
saida = new PrintStream(socket.getOutputStream());
saida.println("Digite seu nome");
nome = entrada.nextLine();
System.out.println(nome + " conectou na porta: [" + socket.getInetAddress().getHostAddress() + ":" + socket.getLocalPort() + "]");
synchronized(clientes) {
clientes.put(nome, saida);
}
serverSendAll(nome + " entrou no chat");
String msg;
while(entrada.hasNextLine()){
msg = entrada.nextLine();
System.out.println("LOG: " + nome + ": " + msg);
if("/on".equalsIgnoreCase(msg)){
online();
} else if("/help".equalsIgnoreCase(msg)) {
help();
} else if("/roll".equalsIgnoreCase(msg)) {
enviarParaTodos(saida, msg);
roll();
} else {
enviarParaTodos(saida, msg);
}
}
} catch (IOException e){
} finally {
try {
socket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
synchronized(clientes){
clientes.remove(nome);
}
serverSendAll(nome + " Saiu do chat");
System.out.println(nome + " se desconectou");
}
}
private void enviarParaTodos(PrintStream remetente, String msg){
synchronized(clientes){
for(PrintStream clienteSaida : clientes.values()){
if(clienteSaida != remetente){
clienteSaida.println(nome + "> " + msg);
}
}
}
}
private void serverSendAll(String msg){
synchronized(clientes){
for(PrintStream clienteSaida : clientes.values()){
clienteSaida.println("Server> " + msg);
}
}
}
private void online(){
StringBuilder sb = new StringBuilder("--Online-- \n");
synchronized(clientes){
for(String nomeCliente: clientes.keySet()){
sb.append("* ").append(nomeCliente).append("\n");
}
}
saida.println(sb.toString());
}
private void roll(){
String msg = "";
int num;
Random rdm = new Random();
num = rdm.nextInt(100);
serverSendAll("Numero aleatorio -> " + String.valueOf(num));
}
private void help(){
saida.println("/on - Ver quem esta online \n" +
"/help - Ver os comandos\n" +
"/roll - Sorteia um numero de 1 ate 100");
}
}
}