-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
185 lines (147 loc) · 4.9 KB
/
Server.java
File metadata and controls
185 lines (147 loc) · 4.9 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
import java.net.*;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
class Server extends JFrame
{
ServerSocket server;
Socket socket;
BufferedReader br;
PrintWriter out;
//Declare Components
private JLabel heading =new JLabel("Server Area");
private JTextArea messageArea=new JTextArea();
private JTextField messageInput=new JTextField();
private Font font=new Font("Roboto",Font.PLAIN,20);
//constructor
public Server()
{
try{
server = new ServerSocket(6778);
System.out.println("Server is ready to accept connection request");
System.out.println("waiting..");
socket=server.accept();
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out= new PrintWriter(socket.getOutputStream());
createGUI();
handleEvents();
startReading();
// startWriting();
}catch(Exception e){
e.printStackTrace();
}
}
private void handleEvents() {
messageInput.addKeyListener(new KeyListener(){
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
//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("Server Messenger[END]");
this.setSize(600,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("clogo.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 set
this.setLayout(new BorderLayout());
//adding the 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("Client terminated the chat");
JOptionPane.showMessageDialog(this,"Client Terminated the chat");
messageInput.setEnabled(false);
socket.close();
break;
}
//System.out.println("Client : " + msg);
messageArea.append("Client : " + msg+"\n");
}
} catch(Exception e){
System.out.println("connection closed");
}
};
new Thread(r1).start();
}
//start writing send [Method]
public void startWriting() {
Runnable r2 = () -> {
System.out.println("Writer Started..");
try{
while (!socket.isClosed()) {
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
String content=br1.readLine();
out.println(content);
out.flush();
if(content.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 Server();
}
}