-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEchoClient.java
47 lines (37 loc) · 1.56 KB
/
EchoClient.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
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class EchoClient {
public static void main(String[] args) {
int port = 4243;
try {
//Create a connection to the server by creating a socket
Socket newSock = new Socket("127.0.0.1", port);
//Get the remote port open on the server
System.out.println("The open port on the server is " + newSock.getPort());
//Get the local port on the client
System.out.println("The local port is " + newSock.getLocalPort());
//Ask user what message to send to the server
Scanner input = new Scanner(System.in);
System.out.print("Please enter the message to send: ");
String message = input.nextLine();
//Send the message to the server
PrintWriter netOut = new PrintWriter(newSock.getOutputStream());
netOut.println(message);
//The output is buffered, so force to send it
netOut.flush();
//Create a stream to receive inputs from server
Scanner netIn = new Scanner(newSock.getInputStream());
String response = netIn.nextLine();
System.out.println(response);
//Close streams
netOut.close();
netIn.close();
newSock.close();
} catch (IOException ioe) {
System.out.println("Problem connecting" + ioe.getMessage());
}
}
}