which port to use for applet servlet communication - java

I am trying to communicate between an applet and a servlet. I first tried Http connection. But I am not getting the desired result. Hence decided to switch to socket communication. Hence I wanted to know which port to use for the same. I remember reading somewhere that port 80 is ideal. Is it port 80 or port 8080? I need a port that is not blocked by firewall(default).

By default, an Applet can only connect to the same host as the applet was served from. You can't set up TCP connections arbitrary hosts. (see e.g. here for info on how to sign an applet, signed applets does not have this restriction)
Using port 80 likely will not work either, as your web server probably works on port 80. Port 80 is really the only port that you usually can count not being blocked, if your servlet container is running on port 8080 , port 80 might be free for you to use though.
imo, try rather to communicate with http so you can talk to a servlet - and work out whatever desired results you had trouble with.

Related

Java - Is there a way a client and a server can connect via IPv4 without port forwarding?

So I'm trying to connect two clients in a Java application, but in a way that one client acts as a server and other client acts as a ... client. I managed to connect them locally which works perfect, but I've been researching whether I can connect a client to a server that are not on a same network (via IPv4 or IPv6). I have read that I should do port forwarding on my router server-side. I know how to port forward, but shouldn't it be possible to do without port forwarding? If I understand correctly, only server-side should be port forwarded and the server can respond to the client without the need for the client to port forward their router? So if I'm correct, another solution would be a 'global' third party server(that is port forwarded) that would connect two clients by receiving and passing information from one client to another?
I'm just learning here, so I'm sorry if this has already been answered here but I haven't found answers to all of this in one place and I'm trying to come to a conclusion.
Yes, you can access a computer from outside the network and connect to a server
You must download the (ngrok) tool on the device that contains the server and run the tool
The client will contact the server without the need to forward the ports
ngrok
Explain the use of the tool on the site with a download link
shouldn't it be possible to do without port forwarding
Yes, you can make a connection between two machines without port-forwarding.
Example: Web servers
Take for example, web servers. By default a web server sits there listening on port 80, with 80 being the port assigned by convention for HTTP.
The web client (browser or such) sends a request by trying to connect on port 80. If there are no obstacles in the way, then the connection proceeds.
Restricted port access
However, there may be an obstacle.
One common obstacle: Unix-oriented operating systems (BSD, macOS, Solaris, Linux, AIX, etc.) by convention restrict access to ports numbered under 1,024 for security reasons. The operating system blocks any incoming connections on port 80. With that security blockage in place, the web request never reaches the server.
Port-forwarding with a packet-filter tool
One way to get past this restriction is to have the web server listen on an unrestricted port, a port numbered above 1,024, up to the 64K limit, such as 8080. Then configure the packet filter tool on the server machine’s OS to do port-forwarding. The incoming request for port 80 is altered to go to port 8080 instead.
A connection is then established between the web server and the web client.
The client thinks it is talking to the server on port 80.
The server thinks the client asked for port 8080.
With the packet filter tool in the middle altering packets on-the-fly, both server and client is none the wiser about packets being altered.
You may want to configure your firewall to allow HTTP connections from outside the machine only on 80, including blocking any external requests for 8080. In this case, only packets altered from 80 to 8080 will reach your web server. Common practice is to close as many ports as possible on a server.
FYI: For encrypted HTTP (HTTPS), the conventional port is 443 rather than 80.
Not a programming issue
Notice that there is no programming issue here. As the programmer, your client software should attempt to connect on the port number as documented for the server in which you are interested. On the server-side machine, or server-side router, port-forwarding will be configured as needed. Your client programming does not care about, or even know about, any port-forwarding that may or may not be in place. Port-forwarding is a network-admin issue, and should be transparent to the programmer.
See sister sites for networking issues
As a network-admin issue, look to the sister sites such as Server Fault and Network Engineering rather than Stack Overflow.

Jetty list on port 80 for application which would usually be on another port

I think my issue may be not knowing exactly what to look for (or the terminology), so hopefully this will serve to also help future people with similar questions.
I have a webapp running on Jetty, deployed using a .war in the webapps dir, lets say it is:
mydomain.com/foo
So the .war file is named "foo.war".
I also have some server which listens on another port, say port 9000. I would like to make this accessible through port 80, but Jetty is using that port. It is a HTTP server, but the port it listens on is 9000 (And I cannot change this).
Is it possible to have mydomain.com/baz relay data to and from localhost:9000 and then back to the client on port 80?
Of course, this needs to be done through port 80 as to the outside world port 80 is the only one available, and jetty is already listening on port 80.
I suppose this would look like:
Client -> mydomain.com:80/baz -> mydomain.com:9000 -> mydomain.com:80/baz -> Client
Almost like an "iframe", only of course an iFrame would require the client to request mydomain.com:9000 which isn't open to the outside world.
You might find it straightforward to set up an Apache httpd which uses http proxying to provide a single set of URLs on port 80 to "clients", but which actually makes http requests to back end servers on non-standard ports behind the scenes.
Nginx can probably do this too.
Start here - http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

Incoming connection on port 80 to webserver application

I made a Java web application running on tomcat, what I'm trying to do now is letting people outside my net acceding my application.
I've read lot of guides and I understood I need to forward incoming traffic on port 80 to the raspberry hosting it (listening on port 8080).
What I did was going to the router panel and make a port forwarding with the following parameters:
Are all the parameters right?
What should i put in the remote IP address?
What I meant to do is redirecting request on port 80 to local port 8080 on which the raspberry is listening (if tomcat is listening to port 8080 if i write something like ip/webappname will I access my application?)
By the way, with the following settings, if I write my "outside" ip as url, I get prompted with a user\password form. I tried to make some tcpdump to try to understand if it's raspberry or router related, but I got tons of packets and couldn't find a way out, what could it be? Thanks.

How to serve HTTP content from a port in Java

When I run ActiveMQ by executing the batch file in its bin/ directory, I am able to go to its admin/management console by opening a web browser and going to http://localhost:8161/admin/.
This has me curious.
This is my local sandbox and I do not have any web server (httpd or otherweise) installed. So how is it that ActiveMQ is able to "register" a port on my machine, and listen to it exclusively?
If I try to go to http://localhost:8162/admin/ (notice the different port #), I get an unable to connect error.
Somewhere, somehow, AMQ is saying "map this URI (localhost:8161) to some root directory on this machine". As a programmer, I'm interested in how something like this works.
A Java process is able to use any port (>= 1024 on linux) as a web server or for any other purpose. You don't need a separate web server to do this
I suggest you read up on sockets: here. All a web server is, is a socket listener that handles the HTTP protocol. HTTP protocol is here.
Web servers often handle a lot of other things, but that is the basics. If you want a small program that also runs a web server I suggest not re-inventing the wheel. Try incorporating jetty into your server.
ActiveMQ starts an embedded Jetty server, which listens for HTTP connections on that port. You don't need any other server running. It's all done from Java. If you dig down deep enough, you'll find some variety of ServerSocket at the bottom of it all. You can learn all about sockets and listening on ports in the Java Tutorial.
At its simplest level, ActiveMQ is creating a ServerSocket instance within itself and listening for connections using this server socket. A socket is always bound to a port.
One: this port is greater than (or equal to) 1024, so it means a "non root" user can listen on it.
Second: you can bind to ports from dedicated addresses only. This means ActiveMQ may have only opened that port on 127.0.0.1 (localhost). Try and see if you can open that URL from your external interface's IP address: chances are you cannot.
If you are under a Unix system, you can check what program listens on which port by using netstat -ltpn.
The basic system call for binding to a port is listen(2).

getting the port number of a website

How can i obtain the port number of a website using a program. Is there any method / way that i can use to know the port number of a website ?
Or, if i know that my port number 52970 is connected with 212.58.241.131 this ip , can i know the port number with which the port number of my PC is connected ?
I believe you need to review the concept of port numbers.
By default, HTTP uses port 80. So an individual website you visit won't need access to any other port.
TCP and UDP port numbers
I am not sure what you are looking for but from a PHP script you can get the port the client uses to connect to your web server with $_SERVER["REMOTE_PORT"]
The port number for http is port 80 and 443 for ssl.
If you are on windows start up cmd and type netstat -a -b to see what program connects where.
Please elaborate or post an example of what you want to achieve as it's not quite clear to me.
€dit: in php you can find the remote or server port with
$_SERVER['REMOTE_PORT']
or
$_SERVER['SERVER_PORT']
If I am right, you are looking for the remote port.
http://php.net/manual/en/reserved.variables.server.php
for telnet look here :
http://www.geckotribe.com/php-telnet/
I am not sure if this helps you at all but the gold standard for scanning ports has to be nmap.
http://nmap.org/
You can scan open ports for a specific IP address.
echo $_SERVER['SERVER_PORT']."<br/>";
echo $_SERVER['REMOTE_PORT']."<br/>";
'SERVER_PORT'
The port on the server machine being used by the web server for
communication. For default setups, this will be '80'; using SSL, for
instance, will change this to whatever your defined secure HTTP port
is.
'REMOTE_PORT'
The port being used on the user's machine to communicate with the web
server.

Categories