-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
132 lines (112 loc) · 4.45 KB
/
Server.java
File metadata and controls
132 lines (112 loc) · 4.45 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
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Server extends Thread
{
int port;
ServerSocket server;
String ipadress;
private Socket s;
boolean flag, connection;
private String str, sadr, sport;
private Reader r;
private BufferedReader buf;
Server(int p) {
this.port = p;
}
public void checkFlag() {
try {
if (!Interface.clientFlag) {
sadr = buf.readLine();
sport = buf.readLine();
Interface.status.setText("You are with stranger from " + sadr);
Interface.cli = new Client(sadr, sport);
Interface.cli.start();
try {Thread.sleep(100);} catch (Exception e) {}
Interface.cli.getMessage(ipadress);
} else {
//socket = s.getInetAddress();
//sadr = socket.toString().substring(1);
sadr = buf.readLine();
Interface.status.setText("You are with stranger from " + sadr);
}
} catch (Exception e) {}
}
public void run() {
int i = 0;
try {
while (!flag) {
try {
server = new ServerSocket(port);
Interface.connectionStatus.setText("You are available");
Interface.connectionStatus1.setText("on port " + port);
System.out.println(server);
//System.out.println(server.getLocalPort());
flag = true;
} catch (BindException e) {
port = port + 1;
}
}
ipadress = server.getInetAddress().getLocalHost().getHostAddress();
Interface.yourIp.setText("Your IP is " + ipadress);
s = server.accept();
r = new InputStreamReader(s.getInputStream());
buf = new BufferedReader(r);
checkFlag();
connection = true;
ConnectionAction.connection();
while (true) {
if (connection) {
r = new InputStreamReader(s.getInputStream());
str = buf.readLine();
if (str != null) {
if (str.equals("//disable")) {
connection = false;
} else {
Date d = new Date();
SimpleDateFormat format1 = new SimpleDateFormat("dd.MM.yyyy kk:mm:ss");
String text = str;
text = text + "\r\n";
Interface.textArea.append("Stranger: " + text);
StringBuilder sb = new StringBuilder();
sb.append("[");
sb.append(format1.format(d));
sb.append(" | Str]: ");
sb.append(text);
TextFile.update(sb.toString());
}
}
} else {
//============================================DISCONNECTING===========================================
Interface.cli.closing();
server.close();
DisconnectionAction.disconnection();
Interface.clientFlag = false;
TextAction.online = false;
Interface.status.setText("Connect to somebody");
flag = false;
while (!flag) {
try {
server = new ServerSocket(port);
Interface.connectionStatus.setText("You are available");
Interface.connectionStatus1.setText("on port " + port);
flag = true;
} catch (BindException e) {
port = port + 1;
}
}
s = server.accept();
r = new InputStreamReader(s.getInputStream());
buf = new BufferedReader(r);
checkFlag();
connection = true;
ConnectionAction.connection();
}
}
} catch (Exception e) {
System.out.println("initt error: " + e);
}
}
}