-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClient.java
More file actions
84 lines (73 loc) · 2.86 KB
/
ChatClient.java
File metadata and controls
84 lines (73 loc) · 2.86 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
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatClient extends Thread
{
protected int serverPort = 1234;
public static void main(String[] args) throws Exception {
new ChatClient();
}
public ChatClient() throws Exception {
Socket socket = null;
DataInputStream in = null;
DataOutputStream out = null;
System.out.println("----------------------------------------------------------------------");
System.out.println(" Pozdravljen! \n\n Seznam uporabnikov, pridobis z ukazom: /userlist\n Privatna sporocila se posiljajo z ukazom: /pvt 'username' <sporocilo>\n ");
System.out.println("----------------------------------------------------------------------\n");
// connect to the chat server
try {
System.out.println("[system] connecting to chat server ...");
socket = new Socket("localhost", serverPort); // create socket connection
in = new DataInputStream(socket.getInputStream()); // create input stream for listening for incoming messages
out = new DataOutputStream(socket.getOutputStream()); // create output stream for sending messages
System.out.println("[system] connected");
System.out.println("Vnesite uporabnisko ime: ");
ChatClientMessageReceiver message_receiver = new ChatClientMessageReceiver(in); // create a separate thread for listening to messages from the chat server
message_receiver.start(); // run the new thread
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(1);
}
// read from STDIN and send messages to the chat server
BufferedReader std_in = new BufferedReader(new InputStreamReader(System.in));
String userInput;
String sporocilo;
while ((userInput = std_in.readLine()) != null) { // read a line from the console
//sporocilo = "<username> " + username + " </username>\n" + "<sporocilo> " + userInput + " </sporocilo>\n" ;
sporocilo = userInput;
this.sendMessage(sporocilo, out); // send the message to the chat server
}
// cleanup
out.close();
in.close();
std_in.close();
socket.close();
}
private void sendMessage(String message, DataOutputStream out) {
try {
out.writeUTF(message); // send the message to the chat server
out.flush(); // ensure the message has been sent
} catch (IOException e) {
System.err.println("[system] could not send message");
e.printStackTrace(System.err);
}
}
}
// wait for messages from the chat server and print the out
class ChatClientMessageReceiver extends Thread {
private DataInputStream in;
public ChatClientMessageReceiver(DataInputStream in) {
this.in = in;
}
public void run() {
try {
String message;
while ((message = this.in.readUTF()) != null) { // read new message
System.out.println("[RKchat] " + message); // print the message to the console
}
} catch (Exception e) {
System.err.println("[system] could not read message");
e.printStackTrace(System.err);
}
}
}