permaculture playing cards*
The moose likes Java in General and the fly likes java application to listen on port 80. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java application to listen on port 80." Watch "java application to listen on port 80." New topic
Author

java application to listen on port 80.

kamal krishna bhatt
Ranch Hand

Joined: Apr 26, 2011
Posts: 41

hi,
i need to listen requests coming on port 80 in order to authenticate these request from free radius server but i want to know is it necessary to create a new serversocket object on port 80 as whenever i do so it says "java.net.BindException: Address already in use" , how can i listen for the traffic with out creating a serversocket object.
Mohan Patidar
Greenhorn

Joined: Aug 28, 2011
Posts: 6
Hi I think You need To write Following Things

Made a Server Socket Object at Server site and Wait till Clinet made a request

ServerSocket ss=new ServerSocket(8000);

Socket s=ss.accept(); // wait till client made request.....


And Write Client Side

Socket s=new Socket("IpAddressOfServer",8000);
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41583
    
  54
As long as there's some other server process already listening on that port, you can't create a ServerSocket (and yes, you need one).

What process is that? Your code will have to run inside that process somehow, so that it gets a chance to handle a piece of its URL space.


Ping & DNS - my free Android networking tools app
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

One and only one can listen to a port at each time. If your normal web server is listening to calls on port 80, then you can't do it also.

If you want to listen to the port, then you need to start listening to it and if the web server then should get the request if any logic is validated, then you need to forward it to the web server that is then listening to another port.

An example is when you put Apache HTTPD in the front, listening to port 80 and delivering static data, and forward JSP calls to a Tomcat that listens to port 8080.
kamal krishna bhatt
Ranch Hand

Joined: Apr 26, 2011
Posts: 41

i think port forwarding would be a nice way to do it i will create a socket on empty port and forward all the requests coming on port 80 to that port.
kamal krishna bhatt
Ranch Hand

Joined: Apr 26, 2011
Posts: 41

hi ,
how can i do port forwarding in java ,which library to use to do that.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41583
    
  54
You still haven't answered the question what process it is that is running on that server already. Why can't your code not run as part of that?

(And no, there is no single library that can do "port forwarding" in Java - and port forwarding is actually something other than what is talked about here).
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Port forwarding will not fix the problem.

The problem is that another program is already listening to port 80. If it's a Windows machine, you maybe have Microsoft IIS or other webserver software running on the machine. You need to stop that program before your own program can listen to port 80.

Note that on most Unix-like operating systems, including Linux and Mac OS X, you can only listen to ports under 1024 when you have root rights. A normal program which doesn't run as root will not be allowed to listen on ports below 1024.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
kamal krishna bhatt
Ranch Hand

Joined: Apr 26, 2011
Posts: 41

i got following output after executing command "netstat -anpl | grep 127.0.0.1:80"

tcp 0 0 127.0.0.1:80 0.0.0.0:* LISTEN 1208/aolserver4-nsd
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

So, you have a program called "aolserver4-nsd" that's listening to port 80. Now you have to find out how to switch off that program.
kamal krishna bhatt
Ranch Hand

Joined: Apr 26, 2011
Posts: 41

actually i don't want to switch off any thing running on port 80 but want to listen incoming requests on the port 80 , so to do it with java i was needed to create a new ServerSocket object which was not allowed because a process is already running on the port. i wanted to do it by forwarding port 80's traffic to another port where i can make a new ServerSocket object on that new free port.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41583
    
  54
forwarding traffic to some other port means the aolserver thingie won't get any requests - that's not what you want, either, right? So you need to figure out how to run your stuff as part of the aolserver.
kamal krishna bhatt
Ranch Hand

Joined: Apr 26, 2011
Posts: 41

yes that's right , to resolve it we can do the following forwarding -

port:80 -> port:xxxx (process request here) -> port:80.

i just want to keep my application separated from any thing running on the port 80 because in case of many servers i won't be able to know what is running on port 80.
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

kamal krishna bhatt wrote:yes that's right , to resolve it we can do the following forwarding -

port:80 -> port:xxxx (process request here) -> port:80.

i just want to keep my application separated from any thing running on the port 80 because in case of many servers i won't be able to know what is running on port 80.


Then you need to change the program that is listening to port 80 to forward to your port and then receive the traffic on port xxxx and forward it to the original port 80 that will forward it to port xxxx... (I can do this all night... ;) ).

What you are trying to do is a network sniffer. There are two ways to do that.
1. Create a proxy and have your program put in front of the actual application that was listening to port 80. As suggested.
2. Hack yourself into the IP-stack and pull the data from there.

And also, tell whomever you want to sniff the traffic from that you are trying to find out what that person is doing on port 80...

Edit: What do you mean with "I don't know what is listening on port 80"? If you do not know, then you are by 99.9% probability not allowed to do it in the first place... ;)
kamal krishna bhatt
Ranch Hand

Joined: Apr 26, 2011
Posts: 41

Not exactly that, i wanted to forward incoming authentication request on port 80 to radius client which will get them authenticated from radius server, if authenticated then proceed to original port.
and some times it is possible that its apache or aolserver there on port 80 so i didn't wanted to bother about that .
any body will not be needed to do forwarding all night it will be done with a simple thread using jsch library.
 
Don't get me started about those stupid light bulbs.
 
subject: java application to listen on port 80.