Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

server and client

Status
Not open for further replies.

satellite03

IS-IT--Management
Dec 26, 2003
248
IN
server can have a port because it listens to a port. but how a client can have a port ?

here is
Code:
//----
Socket client = server.accept ();
System.out.println("Using port:"+ client.getLocalPort());
//---
//---

is not it wrong ?
 
The server socket listens on a port of your choosing. The clients must use this port number to connect to the server socket.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Client connects by ...
Code:
Socket clientSock = new Socket("host",[i][b]port-number[/b][/i]);

where port-number is the port number the server socket is using to listen.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Ok, u mean to say client just use the port of the server we define at the time of creating the ServerSocket.

but then what are these two ports ?


int port = 8080;
ServerSocket server = new ServerSocket (port);

Socket client = server.accept ();
System.out.println(" port:"+ client.getLocalPort()); // what is this port ?


System.out.println(" port:"+server.getLocalPort()); // what is this port ?



are these two ports are same ? do they belong to the same machine ?
 
Mmmm. When the ServerSocket accepts a connection on the 'well-known' port upon which it is listening, the Socket instance it returns uses a new port to handle to conversation from the original client socket and itself (I think - I'm sure someone will correct me if I'm wrong).

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
so u mean these two are different port .

i am writing in simple words . can you verify and confirm me please ?

so, that means if i have 2 computer e.g comp-1 ( server ) and a comp-2(client), there would be basically 2 different port . one port will tell me where from and from which portthe client request is coming . this is basically client.getLocalPort() and belongs to client only (server is not involved at all ) and other port(ServerSocket) where the request is entering into (that is server.getLocalHost() )

this is very much confusing to me . can you plz help me ?


am i right ?


 
You're beginning to confuse me too. Why do you want to know this? It's not knowledge you need to know do most TCP/IP conversations.

1) Decide on a port the server will listen on. Say 9000 for example.
2) Create a ServerSocket on the server bound to this port and set it listening.
3) Create a Socket on the client machine using the hostname of the server and port 9000.
4) The ServerSocket will acknowledge the connection by returning a new Socket instance from the listen method which you use to carry out the conversation with the client.

Note that the conversation using the new socket in step 4 should occur on a new Thread. The thread on which the ServerSocket accepted the connection should then go straight back to listening again.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
... said you were getting me confused.

In step 4, I meant the accept method. This method listens for connections and returns sockets to handle requests from clients.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
thats a very good link u have posted (javaworld ) . i will read the tutorial soon . i think i will get some information from it.

thanks for the tips .

thank you very much
good bye
 
The server:
Has a server socket that listens for incomming clients, when a client connects, a client socket is spawned to communicate on the client. All a server socket can do is allow two client socket to connect.

The client:
Has a client socket that connects to the server.

All of this is happening on the same PORT, that is a port is more the connection between them, but each side needs a socket to talk to eachother.

Note that the conversation using the new socket in step 4 should occur on a new Thread. The thread on which the ServerSocket accepted the connection should then go straight back to listening again.
Unless your purposfully trying to allow only one client to connect at a time.

[plug=shameless]
[/plug]
 
jstreich said:
Unless your purposfully trying to allow only one client to connect at a time.

Indeed!

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top