[英]How to listen to port 80 by using Java Socket
我有一個服務器,其端口 80 被 HTTP 事務占用。 我想查看該端口中的流量,並嘗試使用套接字程序來偵聽該端口。
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);
}
但是,當我運行該 Java 應用程序時,它顯示 BindException: Address already in use。
那么我應該如何處理我的代碼並讓它偵聽端口 80,或者還有其他方法可以在 Java 中偵聽該端口嗎?
如果我理解正確,您正在嘗試嗅探傳遞到服務器的數據包。 如果是這樣的話, 這篇文章中有一些答案。
你在什么服務器上運行它?
這一切都取決於您正在使用的服務器類型。 例如,Tomcat 在 Server.xml 文件中具有它運行的端口類型。
在 Windows 中,您可以由管理員運行您的程序。 在 Linux 中使用 root 用戶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.