Repository navigation
Expand file tree
/
Copy pathClientGUI.java
More file actions
200 lines (158 loc) · 5.2 KB
/
Copy pathClientGUI.java
File metadata and controls
200 lines (158 loc) · 5.2 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
////////////////////////////////////////////////////////////////////////
//
// File Name : ClientGUI.java
//
// Description : Connects to the server via socket to send and
// receive messages in the chat application.
//
// Author : Riddhi Dattatraya Sonawane
//
// Date : 21/11/2025
//
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//
// Header files inclusion
//
///////////////////////////////////////////////////////////////////////
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
///////////////////////////////////////////////////////////////////////
//
// Client side GUI
//
///////////////////////////////////////////////////////////////////////
class Client implements ActionListener
{
//Characteristics
JFrame fobj;
JLabel Lbl1, ResultLabel;
JButton bobj1;
JTextField tobj1;
Socket sobj;
PrintStream pobj;
BufferedReader bobj;
String str1,str2;
static int no;
File file;
FileWriter Writer;
DateTimeFormatter f1;
DateTimeFormatter f2;
String ts1;
String ts2;
String empty;
static
{
no = 200;
}
public Client(String title, int width , int height, Socket sobj)
{
this.sobj = sobj;
empty = "";
try
{
pobj = new PrintStream(sobj.getOutputStream());
bobj = new BufferedReader(new InputStreamReader(sobj.getInputStream()));
file = new File("Log.txt");
Writer = new FileWriter("Log.txt", true);
Writer.write(empty);
}
catch(Exception eobj)
{}
f1 = DateTimeFormatter.ofPattern("dd-MM-YYYY");
f2 = DateTimeFormatter.ofPattern("hh:mm a");
ts1 = LocalDateTime.now().format(f1);
ts2 = LocalDateTime.now().format(f2);
str1 = null;
str2 = null;
fobj = new JFrame(title);
Lbl1 = new JLabel("Message :");
Lbl1.setBounds(50,50,100,30);
tobj1 = new JTextField();
tobj1.setBounds(150,50,100,30);
bobj1 = new JButton("SEND");
bobj1.setBounds(100,100,100,30);
fobj.add(Lbl1);
fobj.add(tobj1);
fobj.add(bobj1);
bobj1.addActionListener(this);
fobj.setLayout(null);
fobj.setSize(width,height);
fobj.setVisible(true);
fobj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent aobj)
{
str2 = tobj1.getText();
pobj.println(str2);
ResultLabel = new JLabel("");
ResultLabel.setBounds(50,(int)no,440,30);
fobj.add(ResultLabel);
if(str2.equals("end"))
{
ResultLabel.setText("You have ended the chat");
return;
}
ResultLabel.setText("Myself : " + str2);
no = no + 20;
try
{
Writer.write("["+ts1+"]"+"["+ts2+"] ");
Writer.write("Client: " + str2+"\n");
Writer.flush();
}
catch (IOException eobj)
{}
}
public void display() throws IOException
{
while(true)
{
str1 = bobj.readLine();
ResultLabel = new JLabel("");
ResultLabel.setBounds(50,(int)no,440,30);
fobj.add(ResultLabel);
if(str1.equals("end"))
{
ResultLabel.setText("Server has ended the chat");
break;
}
ResultLabel.setText("Server : " + str1);
no = no + 20;
Writer.write("["+ts1+"]"+"["+ts2+"] ");
Writer.write("Server : " + str1+"\n");
Writer.flush();
}
}
}
///////////////////////////////////////////////////////////////////////
//
// Entry point function of project (main)
//
///////////////////////////////////////////////////////////////////////
class ClientGUI
{
public static void main(String A[])
{
try
{
/*
For using two different devices :
Connect to same network using wifi or LAN cable between your two devices
Get the ip address from the server device
Paste it below in place of hocalhost
and Run the server first then client
Note : For Windows allow an Java/Port through Firewall (As Windows Firewall may BLOCK this)
*/
Socket sobj = new Socket("localhost",5100);
Client mobj = new Client("Client",500,600,sobj);
mobj.display();
}
catch(Exception eobj)
{}
}
}