> How do I simulate the ports that the socket uses? Suppose that I want
> that the server that runs on my PC will read from port X.
>
> Can the client that runs on my PC write to this port and the server
> will succeed to read it?
Yes. Connect to that port and write to the resulting
(Socket)OutputStream.
> I assume that if the above is true I can also run several clients on
> my PC, and they will write to that socket.
Yes, but each client will have its own socket. At the server, there
will be one socket per client. All of the sockets will share the same
port number at the server end of the connection.
> I also have a really beginner question. When the client writes to the
> server socket in another computer - does he need to specify the socket
> in his computer through which the data goes out? There is a client
> Socket constructor with 4 parmeters srv-ip, srv-socket, clt-ip, clt-
> socket, but the examples I saw - in Sun tutorial for example - use the
> two parameters Socket constructor.
Don't confuse sockets and ports. A socket is one half of a connection
(i.e. a connection endpoint), while a port is more like an address
used to discern between different services.
The normal situation is that the server specifies only the port number
to listen on, and the client specifies the address and port number of
the server.
The client's outgoing port is irrelevant in virtually all cases, and
when the client doesn't specify one the system will choose an
available "ephemeral" port for it.
All of your clients can connect to the same server port, regardless of
whether they come from the same client host or not (unless you have
more than 64k of them, in which case they will need to come from
multiple hosts).
/gordon
--
DavidNorep - 07 Jun 2007 14:34 GMT
Many thanks.
Now one more question: I have two clients who communicate via sockets.
Should each one of them create a Socket (and ServerSocket is not
necessary at all)?
Gordon Beaton - 07 Jun 2007 15:08 GMT
> Now one more question: I have two clients who communicate via
> sockets. Should each one of them create a Socket (and ServerSocket
> is not necessary at all)?
The ServerSocket is necessary in order to establish the connection,
without it there's nothing to connect *to*.
One of the "clients" will have to be a server if they are to
communicate with each other.
/gordon
--