-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
76 lines (67 loc) · 1.98 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
import java.io.*;
import java.net.*;
public class Server {
public static final int port = 28540; //port number
public static void main(String[] args) throws IOException {
MySocket mySocket = new MySocket();
mySocket.start();
}
private static class MySocket implements Runnable {
ServerSocket sSocket = null;
Thread sThread = null;
boolean isRun = false;
String client1Msg = " ";
String client2Msg = " ";
//IP address
final String client1IP = "127.0.0.1";
final String client2IP = "10.0.2.2";
MySocket() throws IOException {
sThread = new Thread(this);
sSocket = new ServerSocket(port);
}
private void start() {
isRun = true;
sThread.start();
}
@SuppressWarnings("deprecation")
private void stop() {
isRun = false;
sThread.stop();
}
@Override
public void run() {
while(isRun) { //received message
if(sSocket.isBound()) {
try {
Socket connection = sSocket.accept();
String remote = connection.getRemoteSocketAddress().toString();
System.out.println("Accept: " + remote);
byte[] dataBuf = new byte[8192];
InputStream input = connection.getInputStream();
input.read(dataBuf);
String data = new String(dataBuf,"UTF-8");
PrintStream printer = new PrintStream(connection.getOutputStream());
if(remote.contains(client1IP)) {
printer.println("Message: "+client2Msg);
client2Msg = " ";
if(data.length() > 1) {
client1Msg = data;
System.out.println("Message received: "+data);
}
} else if (remote.contains(client2IP)) {
printer.println("Message: "+client1Msg);
client1Msg = " ";
if(data.length() > 1) {
client2Msg = data;
System.out.println("Message received: "+data);
}
}
connection.close();
}
catch(Exception e) {
System.out.println(e.toString());
}
}
}
}
}}