-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaEchoClient.java
More file actions
70 lines (60 loc) · 1.93 KB
/
JavaEchoClient.java
File metadata and controls
70 lines (60 loc) · 1.93 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// JavaEchoClient.java
import java.util.Scanner;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.Socket;
public class JavaEchoClient {
public static void main(String[] args) {
try {
String serverIP = "127.0.0.1"; // 127.0.0.1 & localhost
String serverPort = "30000";
if (args.length == 2) {
serverIP = args[0];
serverPort = args[1];
}
System.out.println("Connecting " + serverIP + " " + serverPort);
Socket socket = new Socket(serverIP, Integer.parseInt(serverPort));
System.out.println("Connected.");
OutputStream out = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
InputStream in = socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
Scanner sc = new Scanner(System.in);
String buf;
while (true) {
// keyboard에서 읽고
System.out.print("Input string : ");
buf = sc.nextLine();
// dos.writeUTF(buf);
byte[] bb;
bb = buf.getBytes("euc-kr");// 한글 완성형
dos.write(bb,0, bb.length);
if (buf.contains("exit"))
break;
// server에서 수신하고
String msg;
//msg = dis.readUTF();
byte[] b = new byte[128];
dis.read(b, 0, 128);
msg = new String(b, "euc-kr");
//msg = msg.trim(); // 앞뒤 blank 제거
// 화면에 출력
System.out.println(msg);
}
dos.close();
dis.close();
sc.close();
socket.close();
} catch (ConnectException ce) {
ce.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} // try - catch
} // main
} //