-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
169 lines (131 loc) · 4.81 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
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
import java.net.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.io.*;
public class Client extends JFrame {
Socket socket;
BufferedReader br;
PrintWriter out;
//Declare component
private JLabel heading = new JLabel("Client Area");
private JTextArea messageArea=new JTextArea();
private JTextField messageInput= new JTextField();
private Font font = new Font("Roboto", Font.PLAIN,20);
//constructor
public Client (){
try{
System.out.println("sending request to server");
socket = new Socket("192.168.1.6", 5555);
System.out.println("connection done");
createGUI();
handleEvents();
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
startReading();
//startWriting();
}catch(Exception e){
e.printStackTrace();
}
}
private void handleEvents(){
messageInput.addKeyListener(new KeyListener(){
@Override
public void keyTyped(KeyEvent e){
}
@Override
public void keyPressed(KeyEvent e){
}
@Override
public void keyReleased(KeyEvent e){
//System.out.println("key released"+e.getKeyCode());
if(e.getKeyCode()==10){
// System.out.println("You have pressed ENTER button");
String contentToSend=messageInput.getText();
messageArea.append("Me : "+contentToSend+"\n");
out.println(contentToSend);
out.flush();
messageInput.setText("");
messageInput.requestFocus();
}
}
});
}
private void createGUI(){
//GUI code
this.setTitle("Client Messanger[END]");
this.setSize(500,700);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//coding for component
heading.setFont(font);
messageArea.setFont(font);
messageInput.setFont(font);
heading.setIcon(new ImageIcon("new.png"));
heading.setHorizontalTextPosition(SwingConstants.CENTER);
heading.setVerticalTextPosition(SwingConstants.BOTTOM);
heading.setHorizontalAlignment(SwingConstants.CENTER);
heading.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
messageArea.setEditable(false);
messageInput.setHorizontalAlignment(SwingConstants.CENTER);
//frame layout
this.setLayout(new BorderLayout());
//adding components to frame
this.add(heading,BorderLayout.NORTH);
JScrollPane jScrollPane= new JScrollPane(messageArea);
this.add(jScrollPane,BorderLayout.CENTER);
this.add(messageInput,BorderLayout.SOUTH);
this.setVisible(true);
}
//start reading method
public void startReading(){
Runnable r1 = ()->{
System.out.println("reader started..");
try{
while(true){
String msg =br.readLine();
if(msg.equals("exit")){
System.out.println("server terminate the chat");
JOptionPane.showMessageDialog(this, "Server terminated the chat");
messageInput.setEnabled(false);
break;
}
//System.out.println("server : "+msg);
messageArea.append("Server : "+msg+"\n");
}
} catch(Exception e){
System.out.println("connection is closed");
//e.printStackTrace();
}
};
new Thread(r1).start();
}
// start writing method
public void startWriting(){
Runnable r2 =()->{
System.out.println("writer started...");
try{
while(!socket.isClosed()){
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
String context = br1.readLine();
out.println(context);
out.flush();
if(context.equals("exit")){
socket.close();
break;
}
}
System.out.println("connection is closed");
}catch(Exception e){
e.printStackTrace();
}
};
new Thread(r2).start();
}
public static void main(String[]args){
System.out.println("this is server ... going to start");
new Client();
}
}