問題描述
使用java應用程序在linux中打開端口 (Open port in linux with java application)
I've a java application that creates a socketserver in a port. I test my application in a windows machine, and runs correctly, but when I test it in a linux machine, the port is not listening.
Is there any way to open a port specifically in a linux machine?
I run 'netstat' command, and the port I use in my application doesn't appear. It doesn't throw any exception. I'm trying to connect from another machine to the application, and the connection is refused.
Sincerely, I don't know why it doesn't run...
help please.
thanks, david
參考解法
方法 1:
What is the port number you're trying to open ?
If it's below 1024, then only the root user can open it or grant access to it.
方法 2:
Are you sure your code is actually being executed? Your comment about the code being in a .sar
file implies that you aren't executing it directly, but are deploying it to jboss. Maybe it is not deployed correctly? Have you tried putting some logging statements (or even System.out.println
statements) before and after the ServerSocket is created?
方法 3:
There should be no difference on Windows and Linux. Can you post an exception-stacktrace, that you most likely get?
The possibilities that are likely are, that your chosen port is already occupied (on Linux usually some services are running) or that you try to bind a port below 1024, that is only allowed for root.
方法 4:
It should work the same. Does it throw an exception on linux? Maybe the target port is occupied already.
Edit: Maybe your code successfully binds to the port but the Linux firewall blocks the incoming connections?
Edit 2: Maybe your Linux JBoss configuration differs and your code which contains the initialization for the ServerSocket is not automatically executed.
方法 5:
use netstat -napt to check (I don't know which arguments you used).
(by David、Johan Buret、Jason Day、Mnementh、akarnokd、Aif)