2

I was planning to create a chat messaging application wherein two or more clients can communicate but I am a little confused.

  1. Does java can have a client to client communication using sockets?
  2. Does the socket communication always needs a server?
  3. Is it possible that one client will stand a server of the communication?
  4. Do you have any tutorials for a client to client communication?
  5. If the communication needs a server, how a can a client A see Client B's messages?
virus_upgrade
  • 51
  • 1
  • 4

3 Answers3

3

Client to client communication does not makes any sense because once a system start receiving message it is termed as server, so in communication there should be a server and client to communicate else the situation will be like two people talking and none of them listening.

Client A can act as a server and client both and so the client B, in doing so both can communicate in two way ie send and receive information.

Ankit Zalani
  • 3,068
  • 5
  • 27
  • 47
1

Yes, java can work with sockets. For example, an "official" tutorial from Oracle: http://docs.oracle.com/javase/tutorial/networking/sockets/

But working with sockets directly requires a lot of code for encoding/decoding message from/to a binary form, separating data stream to logical "packets", handling threads and message queues, etc. Fortunately, there are network libraries which make this process much more easier. I would recommend Netty: http://netty.io/

About client/server relationships. If we are talking about TCP/IP, then yes. One side (server) always listens for connection, and the other side (client) opens a connection to the server.

If you are using UDP, however, from network point of view, all participants are equal. They just send and receive UDP packets.

Back to your chat application: the most simple solution - all clients connect to the dedicated server. Every chat message contains client id. When the server receives the message, it sends it to the client with the specified id. Thus, every client can transfer message to every other client. The server works as a "dispatcher".

Deinlandel
  • 1,023
  • 8
  • 25
  • can explain the UDP concept more? I want to create a chat application that do not need a server. do you have any tutorials / documentations on this? – virus_upgrade Mar 06 '14 at 07:59
  • Here's the UDP tutorial: http://docs.oracle.com/javase/tutorial/networking/datagrams/ But the first question you should answer (to yourself) when talking about "serverless" environment, is "how will clients know about each other"? How they will discover that there are another client that is willing to join a chat? – Deinlandel Mar 07 '14 at 08:41
  • I am not sure if there will be a database that will connect the users to each other? – virus_upgrade Mar 10 '14 at 05:42
  • Sorry, I don't understand your question. – Deinlandel Mar 11 '14 at 06:10
  • any way thanks! I have the little idea now. – virus_upgrade Mar 11 '14 at 08:39
0

If you need simple approach you can try https://httprelay.io service. What you need is just http client and no external libraries.

Jonas
  • 4,683
  • 4
  • 45
  • 81