私はファウリングコードを書いて、127.0.0.1:9090 に HTTP プロキシを設定し、google.com にアクセスしようとしましたが、次の出力が出力され、何も起こりませんでした。出力:
Waiting for clients on port 9090
Got connection from /127.0.0.1:11827
Active Connections = 1
Waiting for clients on port 9090
connect started
connect finished
***Got connection from /74.125.232.131:80
writed
GET http://google.com/ HTTP/1.1
Host: google.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: ****
Connection: keep-alive
null
read line startes
コード:
import java.net.*;
import java.io.*;
import java.util.*;
public class SocketGoogler
{
public static void main(String[] args)
{
int port = 9090;
try {
ServerSocket server = new ServerSocket(port);
//System.out.println(google());
while(true) {
System.out.println("Waiting for clients on port " + port);
Socket client = server.accept();
ConnectionHandler handler = new ConnectionHandler(client);
handler.start();
}
} catch(Exception ex) {
System.out.println("Connection error: "+ex);
}
}
}
class ConnectionHandler extends Thread {
private Socket client;
BufferedReader reader;
PrintWriter writer;
static int count;
public ConnectionHandler(Socket client) {
this.client = client;
System.out.println("Got connection from "+client.getInetAddress()
+":"+client.getPort());
count++;
System.out.println("Active Connections = " + count);
}
public void run() {
String message;
String totalMessage;
try {
reader = new BufferedReader(new
InputStreamReader(client.getInputStream()));
writer = new PrintWriter(client.getOutputStream());
writer.flush();
message = reader.readLine();
totalMessage=message+"\n";
while (message != null) {
message = reader.readLine();
totalMessage+=message+"\n";
}
client.close();
//System.out.println(totalMessage);
google(totalMessage);
count--;
System.out.println("Active Connections = " + count);
} catch (Exception ex) {
count--;
System.out.println("Active Connections = " + count);
}
}
public String google(String w) throws IOException
{
String message , totalMessage;
int port = 80;
//ServerSocket server = new ServerSocket(port,10,InetAddress.getByName("google.com"));
//Socket googler=server.accept();
Socket googler=new Socket();
InetSocketAddress endpoint=new InetSocketAddress(InetAddress.getByName("google.com").getHostAddress(), port);
System.out.println("connect started");
googler.connect(endpoint);
System.out.println("connect finished");
System.out.println("***Got connection from "+googler.getInetAddress()+":"+googler.getPort());
BufferedReader reader = new BufferedReader(new
InputStreamReader(googler.getInputStream()));
PrintWriter writer=new PrintWriter(googler.getOutputStream());
//String w="GET http://google.com/ HTTP/1.1\nHost: google.com\nUser-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\nAccept-Language: en-US,en;q=0.5\nAccept-Encoding: gzip, deflate\nConnection: keep-alive"+null;
writer.print(w);
System.out.println("writed\n"+w);
System.out.println("read line startes");
message = reader.readLine();
System.out.println("read line finished");
totalMessage=message+"\n";
while (message != null) {
message = reader.readLine();
System.out.println("*");
totalMessage+=message+"\n";
}
googler.close();
System.out.println("close");
return totalMessage;
}
}
なぜこの問題が発生するのですか?
すでに google.com にも接続してリクエストを送信しましたが、このホストからは何も応答しません。