Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Should I use servlets for my chat?

I am trying to write a chat program for my web page. I am not sure on a few things. I have the program running perfectly on my home computer. There is a server object that handles incomming connections by using ServerSockets and then calling its accept() method. When a connection comes in it creates a seperate thread to handle each individual connection. On the client side I just create a socket connection to the server that is already running. I use my computer name and the port as arguments. The client sends a message to be posted to the server. The server maintains a list of all the open connections and handles dead connections as well. The server receives a message to display and sends it to all the clients.

Here are the questions. I think I need to have the server side running on my web server (lunarpages hosting using resin 2.1.* servlet runner). I think I need to use servlets to do this. Or can you run a java program on the server side without servlets? I know that servlets are mostly used to create dynamic HTTP content like PHP but I am getting the impression that you can run pretty much any other java code through them. Is this correct? The problem is that with my current model the server program needs to continuously monitor a port for incoming connections. This means first of all that I need to find a free port on the server and monopolize it. It also means that the client needs a way of finding out which port to connect to. I can figure the port stuff out myself but what I am woried about is; do host providers allow users to keep a java program continuously running? Keep in mind that the program will be blocking on the ServerSocket.accept() fuction and for the most part will be using 0 cpu cycles. Also, as it is a small chat there will only be a few other threads running (one for each chatter). The program will also use up one of the servers ports while it is executing and waiting for connections on that port(I know I know there are like 65000 of them but I don't know). I don't think that my host will allow this, or will they?

Comments

  • GeoHoffmanGeoHoffman Member Posts: 176
    by the way, I now see that treshr posts in here. Treshr sent me a tutorial and got most of the chat program I am working on running.

    Treshr: I kept a lot of the server side stuff the same but I changed most of how the client side works so it fits the way I want it. Thanks for the awesome start. The only thing the tutorial is slightly unclear on is how to get the chat to work in the context of the web. Is it possible to have the server running here at home (I think the answer is no) and allow people who go to my web site use it? If not can I run the server side on the server without modification? Or do I need to run it as a servlet? Thanks again.
  • GeoHoffmanGeoHoffman Member Posts: 176
    : I am trying to write a chat program for my web page. I am not sure on a few things. I have the program running perfectly on my home computer. There is a server object that handles incomming connections by using ServerSockets and then calling its accept() method. When a connection comes in it creates a seperate thread to handle each individual connection. On the client side I just create a socket connection to the server that is already running. I use my computer name and the port as arguments. The client sends a message to be posted to the server. The server maintains a list of all the open connections and handles dead connections as well. The server receives a message to display and sends it to all the clients.
    :
    : Here are the questions. I think I need to have the server side running on my web server (lunarpages hosting using resin 2.1.* servlet runner). I think I need to use servlets to do this. Or can you run a java program on the server side without servlets? I know that servlets are mostly used to create dynamic HTTP content like PHP but I am getting the impression that you can run pretty much any other java code through them. Is this correct? The problem is that with my current model the server program needs to continuously monitor a port for incoming connections. This means first of all that I need to find a free port on the server and monopolize it. It also means that the client needs a way of finding out which port to connect to. I can figure the port stuff out myself but what I am woried about is; do host providers allow users to keep a java program continuously running? Keep in mind that the program will be blocking on the ServerSocket.accept() fuction and for the most part will be using 0 cpu cycles. Also, as it is a small chat there will only be a few other threads running (one for each chatter). The program will also use up one of the servers ports while it is executing and waiting for connections on that port(I know I know there are like 65000 of them but I don't know). I don't think that my host will allow this, or will they?
    :


    OK. It has been a long few hours of frustration, but I finally figured out how to run servlets, where to put them, and how to configure my web.xml file (cauchos help files are a bit cryptic in my opinion). I still need answers for the other questions:

    1) Is my host likely to allow me to accept incoming connections using the ServerSockets accept() method which will require the use of a port on the server? I have written in the help forum for my web host and have recieved no replies yet.

    2) Are servlets the answer for writing a web based chat or should I do it some other way?

    3) Is it possible to run a normal Java program (one that runs locally without a server and is not derived from HttpServlet) from a web server without modification? I think I read somewhere that any java program that runs on a web server needs to be subclassed from HttpServlet or GenericServlet or something.
  • GeoHoffmanGeoHoffman Member Posts: 176
    Nevermind. I posted on my hosts message board and they replied saying that they do not allow clients to run any programs that scan a port and wait for incoming connections.
  • treshrtreshr Member Posts: 326
    I don't think that host providers allow you to have a program that listens to connections. I've seen a few and the ones I saw were pretty expensive or just really slow.

    You can, if you have a static IP, host it yourself.

    For the other question about the servlets. The program you've written (the serverside one) can be used as a servlet. You'll have to change a few things so it runs like a servlet, but it's possible.

    --=][tReShR][=--

Sign In or Register to comment.