質問編集履歴

1

コードを追加しました

2017/01/13 09:30

投稿

luna12
luna12

スコア47

test CHANGED
File without changes
test CHANGED
@@ -54,7 +54,7 @@
54
54
 
55
55
 
56
56
 
57
- ###該当のソースコード
57
+ ###クライアントプログラム
58
58
 
59
59
  ```java
60
60
 
@@ -86,6 +86,110 @@
86
86
 
87
87
 
88
88
 
89
+ ###サーバープログラム
90
+
91
+ ```
92
+
93
+ /**
94
+
95
+ * コンストラクタ
96
+
97
+ * @throws IOException
98
+
99
+ */
100
+
101
+ public TransDummy() throws IOException {
102
+
103
+ String file="./conf/conf.properties";
104
+
105
+ conf = new Properties();
106
+
107
+ conf.load(new FileInputStream(file));
108
+
109
+
110
+
111
+ ss = new ServerSocket(Integer.parseInt(conf.getProperty("trans_port")));
112
+
113
+
114
+
115
+ //接続待ち
116
+
117
+ for(;;){
118
+
119
+ connect();
120
+
121
+ }
122
+
123
+ }
124
+
125
+
126
+
127
+ /**
128
+
129
+ * との接続を行う
130
+
131
+ * @throws IOException
132
+
133
+ */
134
+
135
+ private void connect() throws IOException{
136
+
137
+ logger.info("Connection開始");
138
+
139
+ //ここでとまって接続待ち状態になります
140
+
141
+ ★s = ss.accept();
142
+
143
+ InputStream is = s.getInputStream();
144
+
145
+ DataInputStream input = new DataInputStream(is);
146
+
147
+ byte[] msg = new byte[12];
148
+
149
+ input.read(msg, 0, 12);
150
+
151
+
152
+
153
+ //受信メッセージ内容確認
154
+
155
+ System.out.println("受信メッセージ内容:");
156
+
157
+ for(int i = 0; i< msg.length; i++){
158
+
159
+ System.out.print(Integer.toHexString(msg[i] & 0xff) + " ");
160
+
161
+ }
162
+
163
+ System.out.println();
164
+
165
+
166
+
167
+ //受信メッセージの長さだけチェック
168
+
169
+ if(msg.length == 12){
170
+
171
+ //メッセージを受信した場合の処理です
172
+
173
+ response(Integer.parseInt(conf.getProperty("trans_response")), msg);
174
+
175
+ } else{
176
+
177
+ logger.warning("受信データエラー");
178
+
179
+ System.exit(1);
180
+
181
+ }
182
+
183
+
184
+
185
+ logger.info("Connection終了");
186
+
187
+ }
188
+
189
+ ```
190
+
191
+
192
+
89
193
  ###試したこと
90
194
 
91
195
  色々通信の設定を変えて試してみたり、