-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
122 lines (110 loc) · 4.56 KB
/
Client.java
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
package test;
import java.io.IOException;
import java.io.PrintStream;
import java.net.*;
import java.util.Scanner;
public class Client {
static byte[] buf = new byte[1024];
private static DatagramSocket socketUdp;
private static boolean running = true;
//private boolean servidorEstadoOnline=true;
private Socket socket;
private String addressServer;
private InetAddress address;
public Client(String address) throws SocketException,
UnknownHostException {
this.addressServer = address;
// socketUdp = new DatagramSocket();
this.address = InetAddress.getByName(address);
}
public static String received() throws IOException {
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socketUdp.receive(packet);
String received = new String(packet.getData(), 0, packet.getLength());
return received;
}
public static void main(String args[]) throws Exception {
// if(args.length!=1){
// System.err.println("usage: java EchoClientThread <host>");
// System.exit(1);
// }
System.out.println("MENU CLIENTE\n" +
"0 - Menu Inicial\n" +
"1 - Listar utilizadores online\n" +
"2 - Enviar mensagem a um utilizador\n" +
"3 - Enviar mensagem a todos os utilizadores\n" +
"4 - lista branca de utilizadores\n" +
"5 - lista negra de utilizadores\n" +
"99 - Sair\n");
String host = args[0];
String cmd, cmd2, cmd3, msg;
Socket socket = new Socket(host, 6500);
socketUdp = new DatagramSocket(9031);
PrintStream output = new PrintStream(socket.getOutputStream(), true);
while (true) {
/*byte[] receiveBuffer = new byte[1024];
byte[] sendBuffer = new byte[1024];
DatagramPacket rcvdpkt = new DatagramPacket(receiveBuffer, receiveBuffer.length);
//socketUdp.receive(rcvdpkt);
InetAddress IP = rcvdpkt.getAddress();
int portno = rcvdpkt.getPort();
String clientData = new String (rcvdpkt.getData());
System.out.println("Cliente: " + clientData);
System.out.println("Servidor: " + clientData);
Scanner scan = new Scanner(System.in);
String serverData = scan.nextLine();
sendBuffer = serverData.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, IP, portno); */
Scanner scan = new Scanner(System.in);
System.out.println("Opção?");
System.out.print(host + ":" + 6500 + "&>");
cmd = scan.nextLine();
if (cmd.equals("0")) {
System.out.println("MENU CLIENTE\n" +
"0 - Menu Inicial\n" +
"1 - Listar utilizadores online\n" +
"2 - Enviar mensagem a um utilizador\n" +
"3 - Enviar mensagem a todos os utilizadores\n" +
"4 - lista branca de utilizadores\n" +
"5 - lista negra de utilizadores\n" +
"99 - Sair\n");
}
if (cmd.equals("1")) {
output.println(1);
System.out.println(received());
}
if (cmd.equals("2")) {
System.out.println("Utilizador?: ");
cmd2 = scan.nextLine();
System.out.println("Mensagem?: ");
cmd3 = scan.nextLine();
msg = cmd + "-" + cmd2 + "-" + cmd3;
output.println(msg);
System.out.println(received());
}
if (cmd.equals("3")) {
System.out.println("Mensagem?: ");
cmd2 = scan.nextLine();
msg = cmd + "-" + cmd2;
output.println(msg);
System.out.println(received());
}
if (cmd.equals("4")) {
output.println(4);
System.out.println(received());
}
if (cmd.equals("5")) {
output.println(5);
System.out.println(received());
}
if (cmd.equalsIgnoreCase("99")) {
System.out.println("a sair...");
break;
}
}
running = false;
output.close();
socket.close();
socketUdp.close();
}
}