Java TCP server program running on Apache Tomcat

10,343

We did this successfully for a chat program a couple of years ago but it was a nightmare to maintain. There are several other more standardised ways of doing this. From the top of my head here is what we did.

  1. Created a servlet
  2. In the servlet init function created a thread that services the socket.

You can see a example of this here Connecting to socket on Tomcat?. Also read this discussion How to listen a socket in Tomcat(servlet container)?

Some alternatives would be to google the following:

  1. JCA (Java Connector Architecture) see http://code.google.com/p/jca-sockets/ for a example

  2. The JBOSS Netty Project

I recently create a tcp/ip server in Apache Camel and it was very easy.

Share:
10,343
Venkatesh Dayawar
Author by

Venkatesh Dayawar

Java/Web Developer

Updated on June 05, 2022

Comments

  • Venkatesh Dayawar
    Venkatesh Dayawar almost 2 years

    Here i have some problem with TCP server program, i have created a jar which is having the TCP server code and this need to be run continuously and i need to deploy this jar in apache server... this jar will works fine when we execute the jar from command prompt ans will not accept the incoming connections when its running apache tomcat,in apache i have already some jars running so they are not at all dependent on this jar.

    while (true) {
        Socket client = serverSocket.accept();
        System.out.println("S: Receiving...");
    
    ----------
    
  • Venkatesh Dayawar
    Venkatesh Dayawar about 12 years
    it needs to be a stand alone program,not any servlet or something else
  • Venkatesh Dayawar
    Venkatesh Dayawar about 12 years
    How to know that TCPServer Program is Running on Apache,i doubt its not running
  • Venkatesh Dayawar
    Venkatesh Dayawar about 12 years
    Hello Namphibian will you please suggest me to do TCPServer Program which is running in Apache tomcat server
  • Namphibian
    Namphibian about 12 years
    @Venkatesh You best best bet would be to look at Apache MINA or JBoss netty projects. These are frameworks for developing TCP/IP server in a short amount of time. They can be be used as stand alone programs. I have worked with both and they are both very excellent and easy to use.
  • Venkatesh Dayawar
    Venkatesh Dayawar about 12 years
    @ Namphibian Thanks for your valuable suggestions and i will look forward to work with Apache MINA,thnx again
  • Venkatesh Dayawar
    Venkatesh Dayawar about 12 years
    i have used this mina but when i try that in apache tomcat its saying classnotfound and i have kept all jar files
  • Venkatesh Dayawar
    Venkatesh Dayawar about 12 years
    noclassdeffound exception org/apache/mina/core/service/IoAcceptor
  • Namphibian
    Namphibian about 12 years
    Venkatesh are you trying to run a TCP/IP standalone program in Apache TOmcat?
  • Venkatesh Dayawar
    Venkatesh Dayawar about 12 years
    yes,Actually we have already a project which built in spring and now we are adding one extra part to it at the bottom for some other functionalities,so for this we need TCP/Ip Server Program needs to be there but i am not able to run that on apache so i have created this jar as Tcp client but i need this jar should be Tcp Server which will continuously listening for connections,i will be very thankful to you if you provide me any sort of suggestions.thnx
  • Namphibian
    Namphibian about 12 years
    @Ventakesh. Go have a look at Apache Camel. It provides you with a MINA component that will allow you to create a TCP IP server with in Tomcat. Do you need to use Tomcat?
  • Venkatesh Dayawar
    Venkatesh Dayawar about 12 years
    @ Namphibian yes i need to use Tomacat it self cause the application already running in it and thnx for ur suggestions