Can a Java applet function as a server? - java

Is it possible to write a Java applet that can be a server on the client machine within the client's local network?
To be more specific, what I am looking to do is tunnel non-web traffic over the web. The sender would send to the applet, which would then forward the received data back to the server.
Is this sort of thing possible? What are the restrictions that might get in the way?
Note: I know that the applet can connect back to the server, that isn't an issue. The issue is whether or not an applet can listen for a connection / data on a local, client-side port.

An unsigned applet can only connect the host they come from.
A signed applet can do any connection you want and can listen on tcp-ip ports.
Source : http://docs.oracle.com/javase/tutorial/deployment/applet/security.html

Server does not connect to anywhere. Server opens server socket and is listening.
In past as far as I remember the server socket was restricted in MSIE and was permitted in Netscape (do you remember such browser?) :)
I personally have not been writing applets for the last 10 years, so I have no idea what happens now with currently existing browsers, but it is very easy to check. Just write the shortest applet you can and put code new ServerSocket(1234).accept(); into its init() or start() method. If no exception was thrown you can write applet that functions as a server. Otherwise you cannot.
Just try it with all available browsers. 20 minutes work and you are done. Good luck. I'd will be glad to know about the results.

Generally, it cannot.
One reason why is that applets tend to be ran within security constrained environments, which means that they are denied the ability to open server sockets.
There are ways around such a restriction, basically you can specify a special security policy for the applet, or run it in a special unconstrained container; but why bother when you can just port the contents of your application into a standard servlet, or even a stand-alone server?

Related

Redirecting an ip in Java

For the past few weeks, I have been scouring the internet, the minds of computer programmers, and just a few random people over the situation I am looking to overcome. Basically, what I am trying to do it write a AntiJoinBot "plugin" (if you will) for the popular game Minecraft. This would be like all others in respect that it blocks IPs based on if they are using a proxy or not, but this AntiJoinBot is running on a different VPS than the actual server.
This is the best graph I can make of the situation (it's not that good):
(non-minecraft server) Connection -> Proxy check -> Redirect to the
server -> Minecraft
The only problem is, I need to be able to redirect the IP and close the connection so that the player's real IP is the one that would connect to the server. If the connection is not able to be closed, it would cause real problems due to some of the plugins we are running.
If you have a solution or a better way to do this, please help me.
Redirection of connections along the lines that you want requires support from the (application) protocol. TCP/IP does not support it. AFAIK, SOCKS does not support it either. Unless the Minecraft application protocol (and by implication, Minecraft clients and servers) include support for redirection, you are out of luck.
(FWIW - that's how HTTP redirection works. HTTP has a "protocol element" that allows the server to tell the client to redirect, and where to redirect to. The client then resends the original request to a new address.)
But that doesn't mean that you can't deal with the pests. It just means that the redirection approach is not viable. Try a custom proxy or an IP filter / redirector instead.
You are trying to save the server's resources on the cost of increase Traffic.
I am not sure with the answer but may be by looking into the concept of LBS(Load Balancing Server) you may find the answer.
LBS is purely defined and controlled by us so you can manage the resources of two servers using one load balancing server.

how do i send data from a webpage to port in web server?

which is the best method to send a numerical value data from a javscript webpage to a web server port.. i have a server socket program running on the server and listening to port 5000.
i need to send data from a webpage to the port? which is the best method?
the scene is this.
i have a C socket program running at a web server hosted in my laptop using XAMPP.
it is listeing to a port 5000 and also i have my ip address. in the web page,
i am running a javascript and when the user presses any of the arrow keys i need a value to be sent to this port.. like up -1 , down -2 etc ...
There isn't currently any way to do this in a portable fashion. Some browsers support the WebSocket standard, which gives you some limited access to sockets, but this isn't currently available in any common browsers other than Chrome and Safari. The Flash player supports a separate XMLSocket protocol, which you can also use if the Flash plugin is available. However, neither of these protocols supports raw access to sockets -- both require some initial negotiation and packet framing.
Because your web server is running on a different port than your socket program, the Javascript in the browser is restricted from making requests directly to the socket program because of the Same Origin Policy. However, there are various ways to get around this restriction. Alex Sexton has a lengthy overview of all the different ways of doing this. However, all of those ways require that your socket program be running the http protocol also.
Socket.IO seems like a perfect fit for what you are trying to do, but you'll probably need to ditch your C program.
The only way to open a network socket connection from a web page is to use a Java or Flash app to make the binary socket connection. You could then use java script to pass the data to the app which would connect and forward it to the server.
Alternatively you could use some server side code and AJAX to proxy the connection and forward the data.
The second option doesn’t rely on any browser plug-in being installed and I think would be easier to implement. It would also be able to forward to a port that is not on the web server itself as it wouldn’t be subject to browser security restrictions.
I guess making one ajax request to required port on a server... can solve the request.

Whats my hostname on my local MySQL server?

I just set up a MySQL server on my PC for testing Java with JDBC.
At the moment "localhost" works perfectly as hostname for my applications, when running them on the same system.
However what would be the hostname for my MySQL server for applications that are running on different computers? Something like "my_ip:port" would work? I was thinking of writing an applet, which I could upload on a web server and try to connect to my database here. Is it possible to achieve that?
Something like "my_ip:port" would work?
If the MySQL instance has bound to your public interface, and if your firewall allows it, yes. If you connect to the 'net via a router that does NAT (for instance, a combined DSL modem and wireless router allowing you to connect multiple computers), you'll have to set up forwarding rules in the router to tell it which of the local machines to forward requests to.
You don't have to use an IP address. Your machine will also probably have a host name of some kind (either one you've assigned or, if you connect through an ISP, more likely one they've assigned). That would work too.
I was thinking of writing an applet, which I could upload on a web server and try to connect to my database here. Is it possible to achieve that?
With a signed Java applet, yes; otherwise, no. That's because the security sandbox that Java applets run in doesn't let them access servers other than the one they were loaded from (the web server).
A much better approach is to have your client-side code (Java applet, or just DHTML+Ajax stuff) talk to server-side code on the web server, which in turn talks to your DB. That way, the DB is never directly exposed to the outside world, and you don't have to do things like signed applets.
You can always use the ip address of the server running mysql as the hostname or its fully qualified domain name.
That should work, but you also should consider port-forwarding through your firewall.
Go here to get your IP: http://www.whatsmyip.org/
The port is the port mysql is setup on.

Best way to program a server status feature

Some background information.
- Running a java server on localhost
- Running a webserver on localhost
I would like a webpage to have a 'server status' feature which lets me know whether the server is running or not. My question, what is the best way to do this?
When I launch the java server, I write a flag in the database to signify that it is running.
Javascript/PHP sockets to try and bind on the same port. (Not sure if possible yet)
Shell script to locate the program in the task list.
Thanks!
When I launch the java server, I write
a flag in the database to signify that
it is running.
would not be of much help if the server should segfault.
Maybe have a look at http://mmonit.com/monit/
what is pretty much what you are looking for
I suspect the simplest method is simply for your web service (backend) to try and connect to the port that your server is running on, and provide an automatically refreshing page that reports this status. If your server goes down then you'll get an faster notification than if you're polling (say) the process table.
Of course the fact that you can connect to the port doesn't really give you an indication of whether it's working other than it's opened a port (e.g. it may have no resources etc. to service requests) but it's a start.

Is it possible to access the different ports on a web server?

I created a game and I want to put it on online. I want to buy a website (I'll probably use goddaddy to buy a domain name and use them as the web host) to use as the server to handle game play. Because I would need a separate server for each game, I would need each game's server to exists on different ports. So this leads to my question, is is possible to access these ports on my future web server? (I wrote the program in Java, so I would assume that I would access the ports from the server side by choosing a port for a ServerSocket, and from the client side by using the IP address from the website and the chosen port for a Socket)
(note: also, I am aware that it may be easier to simply use one port and run the servers on different threads instead, but I am just curious to have my question answered)
thanks a lot,
Ian
Technically it is possible to use different ports, but I don't think that a webhoster like goddaddy will let you run a java process that binds to a special port.
If you mean that you are going to create your own TCP server you obviously can create as many instances of your server and configure them to listen to different ports. But it is year 2011 now. This solution was OK in early 90s.
I'd suggest you to use Restful API that works over HTTP. In this case you can forward calls to server side of each application using URL, e.g.
http://www.lan.com/foo/login?user=u123&password=123456 - log in into application foo
http://www.lan.com/bar/login?user=u123&password=123456 - log in into application bar
In this case you need only one server (the web server) that is listening to socket (port 80).
Your server side implementation could be done using various web techonlogis (php, java, asp.net etc) on your choice.
Yes, that should work. The security manager permits connections to a different port on the same IP address that the applet was loaded from.
You can run a Java server on whatever port you want. Each server will accept incoming requests on one port.
The correct way is simply run on one port and each connection will instantiate a new servlet instance (which happens to run in its own thread) that can then service that request. You usually don't need to run separate ports or worry about concurrency, especially if all the stuff that's shared between connections (e.g. multiple players in one game) is handled through database read/writes.
Your host (GoDaddy) will have to allow you use of those ports, but if they are providing proper hosting (not virtual hosting) and given you your own IP there's no reason why you shouldn't be able to.
Your solution may work theoritically, and I like AlexR's solution. But providers like godaddy doesnt let you run a java server, on ANY port. You will need to find out somebody who does. What I found is the cost goes up from $5/mo to about $20/mo, but you get a much better (read faster) machine. Good wishes, - MS.

Categories