Skip to main content

JXTA socket client-server example

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
coro90
Offline
Joined: 2011-11-05
Points: 0

Hi!

In the tutorial client-server socket in the following key code:

while (true) {

try {

System.out.println("Waiting for connections");

Socket socket = serverSocket.accept();

if (socket != null) {

System.out.println("New connection appeared");

Thread thread = new Thread(new ConnectionToClient(socket), "Connection Handler Thread");

thread.start();

}

}catch (Exception e) {

e.printStackTrace();

My question is if there are 2 clients that connect to the server, do they have different sockets? If yes is it possible to have different pipes between a client and the 2 servers?

Waiting for your anwser.

Andrei

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
wangzhanyu
Offline
Joined: 2011-08-09
Points: 0

you want to run 2 clients which connect to the same server? In my understanding, you do need to establish two different sockets.

if you read the examples carefully, you will find these code bellow on a client:

// Creating the JXTA socket
JxtaSocket MySocket = new JxtaSocket(NetPeerGroup, null,
RendezVous_Lidong_The_JXTA_Socket_Server.GetPipeAdvertisement(),
30000, true);

it is shown that a client uses the server's pipeAdvertisement to create a connection to server. When you have another client wants to establish one communication to that server, it should use the same advertisement, because if the later client uses a new pipeAdvertisement that server doesn't have, it will fail to establish such a socket.

According the description above, here comes problem: Only the advertisement which both server and client know can be used in creating a socket. So, if you want establish second socket to server, you shoud make both the server and the later client have a same pipeAdvertisment.