-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServer.java
More file actions
55 lines (43 loc) · 1.4 KB
/
Copy pathServer.java
File metadata and controls
55 lines (43 loc) · 1.4 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
import java.io.*;
import java.net.*;
public class Server {
//initialize socket and input stream
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream socketInputReader = null;
private DataOutputStream out = null;
// constructor with port
public Server(int port) {
// starts server and waits for a connection
try {
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted");
// takes input from the client socket
socketInputReader =
new DataInputStream(new BufferedInputStream(socket.getInputStream()));
out = new DataOutputStream(socket.getOutputStream());
String socketLine = "";
// reads message from client until "exit" is sent
while (!socketLine.equals("exit")) {
try {
socketLine = socketInputReader.readUTF();
System.out.println("Client: " + socketLine);
} catch (IOException i) {
System.out.println(i);
}
}
System.out.println("Closing connection");
// close connection
socket.close();
socketInputReader.close();
} catch (IOException i) {
System.out.println(i);
}
}
public static void main(String args[]) {
Server server = new Server(5000);
}
}