0

Anyone please help me understand this code. this is taken from the IPCamera from android which i take from the googlecode. the code that i was trying to figure out was:

public NanoHTTPD( int port, File wwwroot ) throws IOException
    {
        myTcpPort = port;
        this.myRootDir = wwwroot;
        myServerSocket = new ServerSocket( myTcpPort );
        myThread = new Thread( new Runnable()
            {
                public void run()
                {
                    try
                    {
                        while( true )
                            new HTTPSession( myServerSocket.accept());
                    }
                    catch ( IOException ioe )
                    {}
                }
            });
        myThread.setDaemon( true );
        myThread.start();
    }

private class HTTPSession implements Runnable
    {
        public HTTPSession( Socket s )
        {
            mySocket = s;
            Thread t = new Thread( this );
            t.setDaemon( true );
            t.start();
        }

        public void run()
        {
            try
            {
                InputStream is = mySocket.getInputStream();
                if ( is == null) return;

Things that i want to know. please tell me if what i understand is wrong:

1- myServerSocket.accept() what this code will return? boolean value as if true or false?

2- InputStream is = mySocket.getInputStream(); Input stream is to get the byte stream. but what the program read on mySocket. From what i understand, it reads on the port number. What it has to do with mySocket with to get the byte?

I am really sorry if my understanding is completely wrong as socket is not my knowledge. please help me to understand more on this.

Stephan
  • 41,764
  • 65
  • 238
  • 329
sara brown
  • 1,057
  • 8
  • 29
  • 46

1 Answers1

2

Here's ServerSocket's accept() method

Waits for an incoming request and blocks until the connection is opened.

and

Returns

the connection representing socket.

And here's getInputStream() which

Returns an input stream to read data from this socket.

You might want to read up on sockets and streams.

Here's Java's tutorial All About Sockets.

Community
  • 1
  • 1
keyser
  • 18,829
  • 16
  • 59
  • 101
  • my question on getInputStream is what the program read on mySocket? next question is accept method. it returns connection representing socket. meaning does it return true if it receive the connection? I mean, what is exactly this program return? – sara brown Oct 31 '12 at 00:55
  • @sarabrown What it read was an input stream and the tutorial should clear a lot of things up. The accept method returns a Socket object not a boolean. It `Listens for a connection to be made to this socket and accepts it`. And yes, it uses the port you specified (meaning accept listens on that port). – keyser Oct 31 '12 at 07:33