简体   繁体   中英

java client-server app - unable to create multiple clients

I am creating a multi threaded app, where server should handle multiple client requests simultaneously. Further, a client should communicate with other clients. The server will hold the port address of various clients and so each client must contact the server in order to know the port address of its peers.

My understanding about the programs:

client.java ( has a socket and a server socket)

socket - used for talking to the server in order to register its serversocket no, which will help other peers to identify. (Note: unlike server socket, where we might have to mentiond the port at the time of creation, socket # is uniquely assigned by the OS and so we don't need to worry about this.)

serversocket - used to talk to other peers and this number should be known by other peers.


server.java

serversocket - used for communicating with various clients


Now, I have two programs server.java and client.java. Of course, I will run client.java more than once in order to have multiple clients. Here are my questions:

will my client.java have two ports? one socket to talk to the server and one server socket to listen to other clients? if so, should I pass the port no as a parameter to client.java so that it can be used as a unique server socket # for each client instance?

Please help me out!

yes you can pass them as runtime args or even better let the server decide it for you that way you wont have to worry about assigning unique port numbers.

Also i believe you will have to pass the client port and ip for clients to talk to each other, unless all your clients will be on the same machine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM