-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
273 lines (220 loc) · 7.56 KB
/
Server.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Scanner;
import javax.swing.*;
import static jdk.nashorn.tools.ShellFunctions.input;
public class Server extends JFrame
{
private JTextField userText;
private JTextArea chatWindow;
public String data;
private String ip[];
private int port[];
private String sip;
private int sport;
ServerSocket receiver;
Socket soc;
Socket in;
Socket outt;
int key;
String name;
private int count;
public Runnable conn;
public Runnable inpt;
public Runnable out;
public Server(int i)
{
super("Server");
data=null;
name="SERVER -";
userText = new JTextField();
userText.setEditable(true);
userText.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
chatsendM(name+event.getActionCommand());
userText.setText("");
}
}
);
add(userText, BorderLayout.NORTH);
chatWindow = new JTextArea();
add(new JScrollPane(chatWindow));
setSize(300, 150); //Sets the window size
setVisible(true);
count=0;
key=i;
ip=new String[4];
port=new int[4];
sport=7780;
}
public void chatinpt()
{
while(true){
try {
in=receiver.accept();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
ObjectInputStream input=null;
try {
input = new ObjectInputStream(in.getInputStream());
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
String n="";
try {
n=(String)input.readObject();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
showMessage(n);
try {
input.close();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void inpt()
{
DatagramSocket datagramSocket=null;
try {
datagramSocket = new DatagramSocket(sport);
} catch (SocketException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
while(true){
try {
byte[] buffer = new byte[10000];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
datagramSocket.receive(packet);
buffer = packet.getData();
String dat=new String(buffer);
data=dat;
} catch (SocketException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void conn(int n, String st, String no)
{
for(int j=0;j<n;j++)
{
try {
Socket s=receiver.accept();;
System.out.println("Connected");
for(int i=0;i<count;i++)
{
System.out.println(port[i]);
}
ip[count]=s.getInetAddress().toString();
port[count]=sport+count+1;
try {
ObjectOutputStream output;
output = new ObjectOutputStream(s.getOutputStream());
output.writeInt(count);
output.flush();
output.writeInt(n-j-1);
output.flush();
output.writeObject(st);
output.flush();
output.writeObject(no);
output.flush();
for(int i=0;i<count;i++)
{
System.out.println(ip[i]);
output.writeObject(ip[i]);
output.flush();
}
count++;
output.flush();
output.close();
System.out.println("Connected");
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public String receiveD()
{
return data;
}
public void chatsendM(String n)
{
showMessage(n);
for(int i=0;i<count;i++)
{
try{
Socket oi;
oi=new Socket(InetAddress.getByName(ip[i].substring(1)),port[i]);
ObjectOutputStream o=new ObjectOutputStream(oi.getOutputStream());
o.writeObject(n);
o.flush();
o.close();
} catch (UnknownHostException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void sendMessage(String n){
for(int i=0;i<count;i++)
{
try {
DatagramSocket datagramSocket = new DatagramSocket();
byte[] buffer =n.getBytes();
InetAddress receiverAddress = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(
buffer, buffer.length, InetAddress.getByName(ip[i].substring(1)), port[i]);
datagramSocket.send(packet);
} catch (SocketException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnknownHostException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void inputt()
{
inpt();
}
public void host(int portAdd, int n, String s, String no)
{
try {
sport=portAdd;
receiver=new ServerSocket(portAdd,9);
System.out.println("serverhosted");
conn(n,s,no);
System.out.println("now");
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void showMessage(final String message){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatWindow.append("\n"+message);
}
}
);
}
}