Starting a ServerSocket from an Applet

I would like to support applet to applet communication
without having to start a server socket by hand (from the command line).
I really would like to start the server socket from an applet, and if at all possible I would even like the server socket's life to be independent from the applet's.
Is this in any way possible.
I was thinking of writing a server-side application to support the server socket, and let the applet create the application through a call to the server (Our Oracle WebServer supports server-side applications rather than server-side applets).
However, the application would never return!
I could keep the applet from hanging up by using a separate thread, though I'm afraid the application will stop as soon as the applet dies.
I want this socket server to serve the communication between many applets/people. I would like to end the server only when all users have stopped listening.
The only other solution I can think of is to create a separate server for each person, but that would really be overkill.
Who is Participating?


what you want, is very similar to a chat server. There is an article on chat servers currently on webmonkey:

If you use a cgi then every applet will execute the cgi once. Cgi are good for request-response type of applications. It seems you want the applets to exchange data or to get updates as long as they are connected. You also you want serveral applets to connect to the server at the same time. In that case it is better to write a server which will be listenning on a given port for incomming connections from applets. The proper way to start a server in that case is to always run it (eg start it at boot time).

 hope this helps,


There is a couple of solutions I can think of:

1.- URLConnection allows you to post/send to a CGI. The CGI could be a program (or shell script) that launces your server.

2.- You could use servlets if your using JDK1.1+

Which soloution sould work better for you?
stouffsAuthor Commented:
I would like to use your first solution, I can't use JDK1.1 yet.
The CGI program is a java application in my case.

You will probably wnat to look at:

which has somewhat detailed information on how to send a post to a CGI from Java.

As long as the program you call deos not end, it will remain active.

As to the invocation itself. Its realy implementation specific. My advise would be to call a program that forks and spawns your server, and then ends, in order to release the connection resoources needed to call the CGI. The advantage of this is taht you can return a value indicating success/failure via stdout (standard CGI output).
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.