-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReceiveDataBlocking.java
33 lines (28 loc) · 1.11 KB
/
ReceiveDataBlocking.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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class ReceiveDataBlocking {
public static void main(String[] args) throws Exception {
// Set up the socket
int port = 10000;
int backlog = 1;
InetAddress address = InetAddress.getByName("127.0.0.1");
ServerSocket server = new ServerSocket(port, backlog, address);
// Accept a connection
System.out.println("Accepting connections...");
Socket client = server.accept();
String clientAddress = client.getInetAddress().getHostAddress();
System.out.println("\r\nNew connection from " + clientAddress);
// Read in the message
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String data = null;
data = in.readLine();
// Print the message
System.out.println("\r\nMessage from " + clientAddress + ": " + data);
// Close the connection
server.close();
System.out.println("Done");
}
}