简体   繁体   English

如何使用Java Socket监听80端口

[英]How to listen to port 80 by using Java Socket

I have a server whose port 80 is for occupied by HTTP transactions.我有一个服务器,其端口 80 被 HTTP 事务占用。 I wanted to see the traffic in that port and I tried to use a socket program to listen to that port.我想查看该端口中的流量,并尝试使用套接字程序来侦听该端口。

public Server(int serverPort) throws IOException {  
    super(serverPort);  
    try {  
        while (true) {  
            Socket socket = accept();  
            new ServerThread(socket);  
        }  
    } catch (IOException e) {  
        e.printStackTrace();  
    } finally {  
        close();
    }  
}  

// inner-class ServerThread  
class ServerThread extends Thread {  
    private Socket socket;  
    private BufferedReader in;  
    private PrintWriter out;  

    // Ready to conversation  
    public ServerThread(Socket s) throws IOException {  
        this.socket = s;  
        in = new BufferedReader(new InputStreamReader(socket  
                .getInputStream(), "UTF-8"));  
        out = new PrintWriter(socket.getOutputStream(), true);  
        start();  
    }  

    // Execute conversation  
    public void run() {  
        try {  

            // Communicate with client until "bye " received.  
            while (true) {  
                String line = in.readLine();  
                if (line == null || "".equals(line.trim())) {
                    break;  
                }  
                System.out.println("Received   message: " + line);  
                out.println(line);  
                out.flush();  
            }  

            out.close();  
            in.close();  
            socket.close();  

        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  

}  

public static void main(String[] args) throws IOException {  
    new Server(80);  
} 

However, when I run that java application, it showed a BindException: Address already in use.但是,当我运行该 Java 应用程序时,它显示 BindException: Address already in use。

So what should I do to my code and make it listen to port 80, or are there any other ways to listen to that port in Java?那么我应该如何处理我的代码并让它侦听端口 80,或者还有其他方法可以在 Java 中侦听该端口吗?

If I understand you correctly you are trying to sniff the packets that are being passed to your server.如果我理解正确,您正在尝试嗅探传递到服务器的数据包。 If that is the case there are some answers in this post.如果是这样的话, 这篇文章中有一些答案。

What Server are you running it on?你在什么服务器上运行它?

it all depends on the type of server you're working on.这一切都取决于您正在使用的服务器类型。 Tomcat for example has the type of port it's running off of in the Server.xml file.例如,Tomcat 在 Server.xml 文件中具有它运行的端口类型。

In Windows you can run your program by administrator.在 Windows 中,您可以由管理员运行您的程序。 In Linux using root user.在 Linux 中使用 root 用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM