-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
63 lines (50 loc) · 1.62 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
package Server;
import java.net.*;
import java.io.*;
/**
* One more time use inheritance to apply threads and encapsulated variable
*/
public class Server extends Thread
{
private ServerSocket server;
private Socket socket;
private DataInputStream in;
int port;
/** the class created a server in one determinate port, then when now connection
* established the client can to send message to server.
*i used constructor to send parameter of port
*/
public Server (int port2){
port = port2;
}
/**
* i used while to always keep server started and only stop server connection when you send message "EXIT"
*
*/
@Override
public void run() {
try {
server = new ServerSocket(port);
System.out.println("Server start in port:" + port);
System.out.println("Waiting for client...");
socket = new Socket();
socket = server.accept();
System.out.println("Client Connect");
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
String line = " ";
while (!line.equals("exit")) {
try {
line = in.readUTF();
System.out.println(line);
} catch (Exception e) {
System.out.println(e);
}
}
System.out.println("closing connection");
socket.close();
in.close();
socket.close();
} catch(Exception e) {
}
}
}