Java Socket Programming

D

DaveL

Hi, I'm a 2nd year undergrad with a personal project on my agenda.
The project I've been working on is a ChatApplication under the
Client/Server Architecture, which is still in its early stages. I'm
new to Socket Programming and have been putting in efforts to dig up
some starter tutorials. Just when I think i'm getting the hang of the
concepts, I've hit a snag.
From my understanding, only ONE Socket can be bound to ANY given port.
Through some toying with the java.net package and its api, I've written
a very simple server.

----------------------------------------------------------------------------------
import java.net.*;

public class SimpleServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;

try {
serverSocket = new ServerSocket(8000);
} catch (IOException e) {
System.err.println("Could not listen on port: 8000.");
System.exit(-1);
}

while (listening) {
System.out.println(serverSocket.accept());
}

serverSocket.close();
}
}
----------------------------------------------------------------------------------

What I've discovered from this SimpleServer.java is that, when the
"accept( )" method from ServerSocket returns, the "new Socket" returned
appears to be bound to port 8000.
But isn't that port already in use, since that's the port SimpleServer
is listening on?
Furthermore, if multiple connections is attempted, it appears that
every "new Socket" returned by "accept( )" will be bound to port 8000?
Wouldn't that mean there exist multiple client sockets all connected to
the server on ONE port?

When a few friends and I try to telnet on the port that SimpleServer is
listening on, here's the output on command prompt:

-----------------------------------------------------------------------------------
Socket[addr=65.93.27.235,port=61159,localport=8000]
Socket[addr=127.0.0.1,port=1597,localport=8000]
Socket[addr=70.28.249.97,port=2074,localport=8000]
-----------------------------------------------------------------------------------
From the output above, it appears all client socket connections are
connected on my localport=8000. Is this the correct behavious that I
should be expecting? I would like to think that the "new Sockets"
returned by "accept( )" would be bound on a different available port
than the one that SimpleServer is listening on, but apparently that
isn't the case. This result is mind-boggling for me, and I am unsure
of how to interpret it. I hope that I've stated my concern well
enough, so I may receive some guidance to this problem I'm facing.
Thank you all.

Regards,
DaveL
 
R

Ravi

Yes Beaton,
I understood your explanation. I think his question is in such a way
that if the serversocket listens in single port 8000.
But how all sockets connected to the same listened port handles the
information passing into that port.
In programmatically , we are spawning the each socket connection into
different threads, Correct?
There should be virtual connection table in low level of network which
correctly delivers the information to corresponding socket to our code
..
Correct me if am wrong at any place

Thaks,
Ravi.
 
G

Gordon Beaton

Socket[addr=65.93.27.235,port=61159,localport=8000]
Socket[addr=127.0.0.1,port=1597,localport=8000]
Socket[addr=70.28.249.97,port=2074,localport=8000]
From the output above, it appears all client socket connections are
connected on my localport=8000. Is this the correct behavious that I
should be expecting?

I would like to think that the "new Sockets" returned by "accept( )"
would be bound on a different available port than the one that
SimpleServer is listening on, but apparently that isn't the case.

First, consider this:

Socket s = new Socket(rhost, 8000); // connect to port 8000
int rport = s.getPort(); // check remote port

if (rport != 8000) {
System.out.println("Suspicious");
}

Are you suggesting that the result of connecting to a given port
number *shouldn't* result in a connection to that same port number?

What happens is that the Socket created in the call to accept() is a
clone of the ServerSocket, and it inherits several attributes from the
ServerSocket, including the port number.

The reason this works is that each connection is uniquely identified
by 4 attributes: the local address and local port number, and the
remote address and remote port number.

The remote addr and port for the ServerSocket are wildcards, since it
isn't connected. The resulting connected Socket has these bound to the
addr and port of the client's connecting Socket, so the ServerSocket
and all Sockets created from it are unique and distinct, regardless of
the local address and port.

Two Sockets cannot be identical in all 4 of the attributes, so for
example it isn't possible to create two ServerSockets listening on the
same port, unless you also bind them to different local addresses
(which requires you to have multiple local addresses to bind to).

/gordon
 
G

Gordon Beaton

I understood your explanation. I think his question is in such a way
that if the serversocket listens in single port 8000.
But how all sockets connected to the same listened port handles the
information passing into that port.

The sockets are different in more aspects than just the port number.
Traffic arriving to the interface is dispatched by the network stack
based on the *combination* of local address, local port, remote
address and remote port. The port number alone does *not* identify the
socket or the connection.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top