hello,

take the simpliest example from a java book.

this is one:

public class BasicTCPServer extends AbstractServer implements Runnable{
        /**
         * The port to listen on.
         */
        private int port;
        /**
         * The listening socket.
         */
        private ServerSocket serverSocket;
        /**
         * True as long as we are running.
         */
        private volatile boolean running;
        
        /**
         * Creates a new server with the given port and connection factory.
         * @param aPort the port to listen to.
         * @param conFactory
         */
        public BasicTCPServer(int aPort, IConnectionFactory conFactory){
                super(conFactory);
                port = aPort;
        }
        

        @Override public void startServer() throws ServerException{
                try{
                        serverSocket = new ServerSocket(port);
                }catch(IOException e){
                        throw new ServerException("Couldn't bind port: "+port+" 
: "+e.getMessage());
                }
                new Thread(this).start();
        }
        
        @Override public void run(){
                setRunning(true);
                while(isRunning()){
                        try{
                                Socket s = serverSocket.accept();
                                IConnection newConnection =
getConnectionFactory().createConnection(new SocketContext(s));
                                notifyConnectionCreated(newConnection);
                                newConnection.open();
                        }catch(IOException e){
                                e.printStackTrace();
                        }
                }
        }

        /**
         * Stops the server.
         */
        @Override public void stopServer() {
                setRunning(false);
                
        }


regards
Leon


P.S. you can checkout the code under
svn://svn.anotheria.net/opensource/ano-net/trunk with a ready to use
tcp client/server, udp client/server and so on.



On Mon, Sep 28, 2009 at 10:10 AM, Sergio Bello <ser...@televes.com> wrote:
> Hi all,
> I'm trying to figure out how to use tomcat as a TCP server. The basic idea
> is to receive tcp connections, through a given port, process them and return
> a response. Has anyone done it? I've googling but I've not found much
> information.
> Which do you think is the best/simplest way to do it?
> Thanks a lot
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to