-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClient2.java
40 lines (37 loc) · 1.38 KB
/
Client2.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
package multiThread;
import java.net.*;
import java.io.*;
import java.util.Scanner;
/**
* This program demonstrates a simple TCP/IP socket client that reads input
* from the user and prints echoed message from the server.
*
*/
public class Client2 {
public static void main(String[] args) {
if (args.length < 2) return;
String hostname = args[0];
int port = Integer.parseInt(args[1]);
try (Socket socket = new Socket(hostname, port)) {
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
Console console = System.console();
String text;
do {
System.out.println("enter text");
Scanner s =new Scanner(System.in);
text=s.nextLine();
writer.println(text);
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String time = reader.readLine();
System.out.println(time);
} while (!text.equals("bye"));
socket.close();
} catch (UnknownHostException ex) {
System.out.println("Server not found: " + ex.getMessage());
} catch (IOException ex) {
System.out.println("I/O error: " + ex.getMessage());
}
}
}