###前提・実現したいこと
初めて投稿致します。
プログラミングを始めて3ヵ月のド素人です。
現在某教則本の学習中で、
Tcpクライアントのjavaプログラムを作成しております。
サーバーはApache Loungeよりhttpd-2.4.25-win64-VC14.zipを入手して使っております。
OSはwindows8.1です。
写経したTcpクライアントのコードは下記よりご参照ください。
client_send.txtよりリクエストを送信し、
client_recv.txtにレスポンスが保存される仕組みですが、
このレスポンスが何度試しても408のタイムアウトが返ってきます。
###発生している問題・エラーメッセージ
client_recv.txt
HTTP/1.1 408 Request Timeout Date: Wed, 15 Feb 2017 11:03:30 GMT Server: Apache/2.4.25 (Win64) Content-Length: 221 Connection: close Content-Type: text/html; charset=iso-8859-1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>408 Request Timeout</title> </head><body> <h1>Request Timeout</h1> <p>Server timeout waiting for the HTTP request from the client.</p> </body></html>
ちなみにアクセスログはこんな感じです。
access.log
127.0.0.1 - - [15/Feb/2017:00:15:19 +0900] "GET /index.html HTTP/1.1" 408 221 127.0.0.1 - - [15/Feb/2017:00:17:21 +0900] "GET /index.html HTTP/1.1" 400 226 127.0.0.1 - - [15/Feb/2017:20:03:30 +0900] "GET /index.html HTTP/1.1" 408 221
###該当のソースコード
TcpClient.java
import java.io.*; import java.net.*; public class TcpClient { public static void main(String[] args) throws Exception { try (Socket socket = new Socket("localhost", 80); FileInputStream fis = new FileInputStream("client_send.txt"); FileOutputStream fos = new FileOutputStream("client_recv.txt")) { int ch; OutputStream output = socket.getOutputStream(); while ((ch = fis.read()) != -1) { output.write(ch); } InputStream input = socket.getInputStream(); while ((ch = input.read()) != -1) { fos.write(ch); } } catch (Exception ex) { ex.printStackTrace(); } } }
client_send.txt
GET /index.html HTTP/1.1 Accept: text/html, application/xhtml+xml, */* Accept-Language: ja-JP User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko Accept-Encoding: gzip, deflate Host: localhost:80 DNT: 1 Connection: Keep-Alive
###試したこと
まずはApacheサーバーをhttpd.exeを実行して立ち上げて、
コマンドプロントより【java TcpClient】で実行するという手順でリクエストを送っております。
ただし上述したように何度試してもタイムアウトしてしまいます。
ちなみにブラウザよりhttp://localhost/index.htmlを叩いて実行すると、ちゃんとindex.htmlが返ってくるので通信が成功しているようです。
どこに原因があるのかが分からず途方に暮れております。
何卒ご教示頂ける方がおりましたら幸いです。
長文、駄文失礼致しました。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/02/15 12:16
2017/02/15 12:22
退会済みユーザー
2017/02/15 13:15
2017/02/15 13:36