Domanda

I'm doing a chat desktop app with sockets and my intention is to create a peer-to-peer communication between clients. My question is this:

If I have all the IP addresses, can I connect simultaneously clients between them?

In my project every Client has both server sockets and sockets (client).

For example client_1 listens in the port 1001 for connections but at the same time can connect to other clients who have different IP-s and listen to different ports.

Now what I want to know, if someone with experience can tell it, is it possible to connect to some client and at the same time receive messages from others if I'm connected to one client but others are connected to me?

If Client_A listens to port 1001 but wants to chat with Client_B, than it connects himself to Client_B by specifying his IP and port; now, can at the same time Client_A recieve messages form other Clients who connects to his port?Is it possibile?

If someone can help me I can publish also my code; but I think is a logic problem I have to solve here.

Thank you very much.

È stato utile?

Soluzione

It sounds like you're struggling a little bit with socket architecture. Each socket pair in your architecture will be determined by a unique pair of IP addresses. Wikipedia says:

Communicating local and remote sockets are called socket pairs. Each socket pair is described by a unique 4-tuple consisting of source and destination IP addresses and port numbers, i.e. of local and remote socket addresses.3[4] As seen in the discussion above, in the TCP case, each unique socket pair 4-tuple is assigned a socket number

Additionally, according to this stackoverflow answer:

Simply, if I connect to the same web server twice from my client, the two connections will have different source ports from my perspective and destination ports from the web server's. So there is no ambiguity, even though both connections have the same source and destination IP addresses.

Your peer-to-peer chat client network can definitely work, and clients can indeed syndicate messages that they've received to other clients. Most likely you will want to tag each message with a GUID so that clients do not process the same message multiple times.

Perhaps a more useful architecture would be a pub/sub architecture. Realtime networks such as PubNub allow you to publish messages to global channels, and receive those messages on clients subscribed to that channel. In your case, using their Java SDK, publishing a message is as simple as:

Callback callback = new Callback() {
  public void successCallback(String channel, Object response) {
    System.out.println(response.toString());
  }
  public void errorCallback(String channel, PubnubError error) {
    System.out.println(error.toString());
  }
};
pubnub.publish("demo", "Hello World !!" , callback);

I hope this helps. Cheers!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top