-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJabberClient.java
More file actions
101 lines (80 loc) · 2.28 KB
/
JabberClient.java
File metadata and controls
101 lines (80 loc) · 2.28 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.io.*;
import java.net.*;
public class JabberClient extends Connector{
//public static final int PORT = 8080; // ポート番号を設定する.
private BufferedReader in;
private PrintWriter out;
//private ServerSocket s;
private Socket socket;
JabberClient(int portN){
super(portN);
}
public void StartConnect()
throws IOException {
boolean socketConnect = false;
InetAddress addr =
InetAddress.getByName("localhost"); // IP アドレスへの変換
//System.out.println("addr = " + addr);
socket =
new Socket(addr, PORT); // ソケットの生成
try {
//System.out.println("socket = " + socket);
in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream())); // データ受信用バッファの設定
out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())), true); // 送信バッファ設定
socketConnect = true;
} finally {
if(!socketConnect){
//System.out.println("closing...");
socket.close();
}
}
}
@Override
public void Send(Message mes){
out.println(mes.type);
//System.out.println("test:送ったのは"+mes.type);
out.println(mes.txt);
//System.out.println("test:送ったのは"+mes.txt);
//BufferReaderではchar[]を読み込めないため、stringにして送る
String ansStr="";
for(int i=0;i<mes.ansData.length;i++){
ansStr = ansStr + mes.ansData[i];
}
out.println(ansStr);
//System.out.println("test:送ったのは"+ansStr);
}
@Override
public Message Wait(){
Message mes = new Message();
try{
mes.type = in.readLine();
mes.txt = in.readLine();
String ansStr = in.readLine();
for(int i=0;i<ansStr.length();i++){
mes.ansData[i] = ansStr.charAt(i);
}
return mes;
}catch(IOException e){
Close();
System.out.println(e);
mes.txt = "error";
return mes;
}
}
@Override
public void Close() {
//System.out.println("closing...");
try{
socket.close();
}catch(IOException e){
System.out.println(e);
}
}
}