• Post Reply Bookmark Topic Watch Topic
  • New Topic

Q reg. ServerSocket.accept() method

 
nitin_k
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<pre>
When ServerSocket.accept() method is called,
it waits for client requests from remote machines.
After receiving a request, it returns a new Socket object.
i had a doubt whether this new socket object is bound with
same port as the ServerSocket or some different unique port?
cos when i invoked getPort method on both ServerSocket
object and new Socket object, it returned same port no.
which i guess conflicts with the theory.
any help is appreciated.
<pre>
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nitin_k:

<pre>
When ServerSocket.accept() method is called,
it waits for client requests from remote machines.
After receiving a request, it returns a new Socket object.
i had a doubt whether this new socket object is bound with
same port as the ServerSocket or some different unique port?
cos when i invoked getPort method on both ServerSocket
object and new Socket object, it returned same port no.
which i guess conflicts with the theory.
any help is appreciated.
<pre>

Just as example, I tried this with the HTTP server I'm currently working on and these are the results I obtained:
Immediately after calling the ServerSocket accept() method I called ss.getLocalPort(), s.getLocalPort(), and s.getPort() where s is the client socket object.
The ServerSocket local port was 80, the socket local port was also 80, but the client port was 33728. Subsequent connections returned various other client port values that were high port numbers 33729 etc.
That's what I expected to see. Maybe the point of confusion is what Java means by localPort() versus port()?
 
nitin_k
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nitin_k:

<pre>
When ServerSocket.accept() method is called,
it waits for client requests from remote machines.
After receiving a request, it returns a new Socket object.
i had a doubt whether this new socket object is bound with
same port as the ServerSocket or some different unique port?
cos when i invoked getPort method on both ServerSocket
object and new Socket object, it returned same port no.
which i guess conflicts with the theory.
any help is appreciated.
<pre>

 
nitin_k
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry Marvin , the previous one was wrong reply.
getLocalPort() invoked on ss and the Socket obj. returned
from ss.accept() returned same port.
so does that mean one port can be associated with multiple sockets?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!