有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我是否必须写入socket才能让服务器向我发送响应,以便从中读取某些服务的标题?

public class PortScan 
{

public String ip;

public PortScan(String ip)
{
    this.ip = ip;
}
public void setIP( String ip )
{
    this.ip = ip;   
}

public String getIP()
{
    return this.ip;
}
public void scan()
{
    int i = 1;
    String ip = this.getIP();
    while(i < 50000)
    {
    try
    {
        Socket sock = new Socket(ip, i);
        InputStream in = sock.getInputStream();
        OutputStream out = sock.getOutputStream();
        BufferedInputStream bins = new BufferedInputStream(in);
        BufferedReader bin = new BufferedReader(new InputStreamReader(bins));
        String response = bin.readLine();
        System.out.println(response);
        System.out.println(ip + ":" + i);
        sock.close();
    }
    catch( UnknownHostException e )
    {

        //System.out.println("No host found.");
    }
    catch(IOException e)
    {

        //System.out.println("Problem connection to host.");
    }
    i++;
    }
} 
public static void main(String[] args) 
{
    PortScan aPortScan = new PortScan("127.1.1.1");

    aPortScan.scan();
}
}

控制台输出:

SSH-2.0-OpenSSH_6.0p1 Debian-4
127.1.1.1:22<
null
127.1.1.1:80

共 (1) 个答案

  1. # 1 楼答案

    是的,有些服务需要交互才能输出横幅。对于HTTP服务器,您必须发送GET / HTTP/1.1,然后解析Server:行的响应