-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
155 lines (135 loc) · 4.4 KB
/
Client.java
File metadata and controls
155 lines (135 loc) · 4.4 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
//Client program
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.Font;
import java.awt.BorderLayout;
import javax.swing.border.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import static java.lang.System.out;
public class Client extends JFrame
{
//for gui
public JLabel heading=new JLabel("Client");
public JTextArea msgbox =new JTextArea();
public JTextField msginput=new JTextField();
public Font font =new Font("Roboto",Font.PLAIN,20);
public void gui()
{
this.setTitle("CHAT APPLICATION");
this.setSize(400,600);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
heading.setFont(font);
msgbox.setFont(font);
msginput.setFont(font);
this.setLayout(new BorderLayout());
this.add(heading,BorderLayout.NORTH);
heading.setHorizontalAlignment(SwingConstants.CENTER);
heading.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
this.add(msgbox,BorderLayout.CENTER);
msgbox.setEnabled(false);
this.add(msginput,BorderLayout.SOUTH);
this.setVisible(true);
}
public void eventhandler()
{
msginput.addKeyListener(new KeyListener()
{
public void keyTyped (KeyEvent e)
{
}
public void keyPressed (KeyEvent e)
{
}
public void keyReleased (KeyEvent e)
{
//for debugging
// System.out.println("keyreleased "+e.getKeyCode());
if(e.getKeyCode()==10)
{
String contentToSend =msginput.getText();
msgbox.append("ME: "+contentToSend+"\n");
System.out.println(contentToSend);
write.println(contentToSend);
write.flush();
out.flush();
msginput.setText("");
msginput.requestFocus();
}
}
});
}
//
Socket socket;
BufferedReader read;// for reading
PrintWriter write;// for Writing
public void start_read()
{
//creating threads
Runnable r1=()->{
while(true)
{
try {
String msg=read.readLine();
if(msg.equals("exit"))
{
System.out.println("CHAT ENDED(server)");
System.out.println("[SESSION TERMINATED]");
msginput.setEnabled(false);
socket.close();
System.exit(0);
break;
}
System.out.println("Server: "+msg);
msgbox.append("Server: "+msg+"\n");
}
catch(Exception e) {
//e.printStackTrace();
System.exit(0);
}
}
};
new Thread(r1).start();
}
public void start_write()
{
Runnable r2=()->{
while(true)
{
try {
BufferedReader console=new BufferedReader(new InputStreamReader(System.in));
String content= console.readLine();
write.println(content);
write.flush();
}
catch(Exception e) {
e.printStackTrace();
}
}
};
new Thread(r2).start();
}
public Client()
{
try{
System.out.println("[SENT REQUEST]");
socket=new Socket("127.0.0.1",1230);
System.out.println("[connected]");
read= new BufferedReader(new InputStreamReader(socket.getInputStream()));
write= new PrintWriter(socket.getOutputStream());
gui();
eventhandler();
start_read();
start_write();
}catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("[Client Started]");
new Client();
}
}