-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerExample.java
58 lines (50 loc) · 2.16 KB
/
ServerExample.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
package sockets;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/** ServerExample class, to create a simple example to connect two computer via a
* TCPIP connection
* */
public class ServerExample {
/*We keep the port in a constant*/
private final static int PORT = 5005;
public static void main(String[] args) {
try {
Scanner tec = new Scanner(System.in);
//Server Socket to wait for network requests
ServerSocket server = new ServerSocket(PORT);
System.out.println("Server started");
//Client Socket
Socket client;
System.out.println("Server waiting for a client...");
client = server.accept();
System.out.println("connected to client: " + client.getInetAddress());
//setSoLinger closes the socket giving 10mS to receive the remaining data
client.setSoLinger (true, 10);
//an input reader to read from the socket
BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()));
//to print data out
PrintStream output = new PrintStream(client.getOutputStream());
//we read a line from the client
String line = input.readLine();
//and we print it
System.out.println("Server -> read line: " + line);
// now we read a line from the keyboard
System.out.println("Server -> type a sentence to send to the client:");
String line2= tec.nextLine();
System.out.println("Server -> read line (keyboard): " + line);
output.flush();//empty contents
output.println(line2);
System.out.println("Server -> send the line to the client");
//close connection
client.close();
server.close();
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}
}