-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClient.java
More file actions
118 lines (108 loc) · 4.34 KB
/
ChatClient.java
File metadata and controls
118 lines (108 loc) · 4.34 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import java.net.Socket;
import java.util.Base64;
public class ChatClient {
//Server IP and port to connect to
private static final String SERVER_IP = "XXX.XXX.XXX.XXX";
private static final int SERVER_PORT = 8080;
//these are the swing components to make the GUI
private JFrame frame;
private JTextField messageField;
private JTextArea chatArea;
private PrintWriter writer;
//this is a contructor to initialize a chat client
public ChatClient() {
//here we set up the main frame
frame = new JFrame("Chat Client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLayout(new BorderLayout());
//create and set up the message input field at the bottom
messageField = new JTextField();
frame.add(messageField, BorderLayout.SOUTH);
//create and set up the chat area in the center with scrolling
chatArea = new JTextArea();
chatArea.setEditable(false);
frame.add(new JScrollPane(chatArea), BorderLayout.CENTER);
//create a send button on the right
JButton sendButton = new JButton("Send");
sendButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
frame.add(sendButton, BorderLayout.EAST);
//here we add an action listener to the text field for the enter key
messageField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
//here we make the frame visible
frame.setVisible(true);
//here we get the input from the using a dialog box
String userName = JOptionPane.showInputDialog(frame, "Enter your username:");
if (userName == null || userName.trim().isEmpty()) {
JOptionPane.showMessageDialog(frame, "Username cannot be empty. Exiting.");
System.exit(0);
}
try {
//we connect the server using the specified IP and port
Socket socket = new Socket(SERVER_IP, SERVER_PORT);
//set input and output streams for communication with the server
BufferedReader serverReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
//send the username to the server
writer.println(userName);
//here we start a thread to continuously receive messages from the server and update the chat area
Thread receiveThread = new Thread(() -> {
try {
String serverMessage;
while ((serverMessage = serverReader.readLine()) != null) {
chatArea.append(serverMessage + "\n");
}
} catch (IOException ex) {
ex.printStackTrace();
}
});
receiveThread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
//this is a method to send a message to the server
private void sendMessage() {
String message = messageField.getText();
if (!message.isEmpty()) {
//send the message to the server and clear the message field
writer.println(message);
messageField.setText("");
}
}
private void sendImage(String imagePath){
try {
File imageFile = new File(imagePath);
FileInputStream fis=new FileInputStream(imageFile);
byte[]imageData = new byte[(int)imageFile.length()];
fis.read(imageData);
fis.close();
String base64Image= Base64.getEncoder().encodeToString(imageData);
writer.println(base64Image);
}
catch (IOException e) {
e.printStackTrace();
}
}
//this is the main method to launch the chat client
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ChatClient());
}
}