- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericTerminal.java
More file actions
Latest commit
70 lines (61 loc) · 1.58 KB
/
Copy pathGenericTerminal.java
File metadata and controls
70 lines (61 loc) · 1.58 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
importjava.io.*;
importjava.net.*;
publicclassGenericTerminal{
privateSocketsock = null;
privateOutputStreamsock_out = null;
privateInputStreamsock_in = null;
//接続を確立
publicvoidestConnetion(Stringhostname, intport) throwsIOException, UnknownHostException{
this.sock = newSocket(hostname,port);
this.sock_in = newBufferedInputStream(this.sock.getInputStream());
this.sock_out = this.sock.getOutputStream();
}
//ストリームを繋ぐ
publicvoidcommunicate() throwsIOException{
StreamConnecterstdin_to_sock = newStreamConnecter(System.in, sock_out);
StreamConnecterstdout_to_sock = newStreamConnecter(sock_in, System.out);
(newThread(stdin_to_sock)).start();
(newThread(stdout_to_sock)).start();
}
publicstaticvoidmain(String[] args) {
GenericTerminalgt = null;
try{
gt = newGenericTerminal();
gt.estConnetion(args[0], Integer.parseInt(args[1]));
gt.communicate();
}catch(Exceptione){
e.printStackTrace();
System.exit(1);
}finally{
try{
if(gt.sock.isClosed()){
gt.sock.close();
}
}catch(IOExceptione){
e.printStackTrace();
System.exit(1);
}
}
}
}
classStreamConnecterimplementsRunnable{
privateInputStreamin = null;
privateOutputStreamout = null;
publicStreamConnecter(InputStreamin, OutputStreamout){
this.in = in;
this.out = out;
}
publicvoidrun(){
byte[] buff = newbyte[1024];
intlen = 0;
try{
while(true){
len = in.read(buff);
if(len > 0) out.write(buff,0,len);
}
}catch(IOExceptione){
e.printStackTrace();
System.exit(1);
}
}
}