views:

256

answers:

3

Hi, I am beginner of learning Java Networking.I want to connect 3 or more computers. One for server and others(eg A and B )for clients .But I want to connect

  1. A to Server and vise versa
  2. B to Server and vise versa
  3. A to B and vise versa

First I want to send message from either A or B to server and server sends stored data to sender (eg A) and sender (A) will connect to B. Then A and B will confirm message and A and B will send data one to another. But this occasion can occurs from A and B concurrently.

But I just learned simple code for connection of one server and one client using Server socket and socket. Will all 3 computers require to act for both server and client? Is there other ways to connect between clients. I don't know how to consider to solve data conflict between computers. I also want to satisfy if new clients are added. If anyone know to solve above problem including data conflicts, pls help me with simple sample code for both server and clients.

Thanks !!

A: 

A process that is expecting to receive incoming calls must be listening as a server; a process that makes outgoing calls is a client for the purposes.

You can get around the problem of client machines (A, B) being behind a firewall that would prevent them making direct connections by having each of them poll the server at intervals to ask whether there is any data for them; so that when A has data for B, it makes a request of the server that next time B connects to ask to let it know.

A then waits with the connection open until B polls, & is told there is data, the server then tells A that B is ready, and can act as a proxy until one or the other breaks the connection.

Steve Gilham
If he sets up two way sockets can't the server write whatever comes in on one socket to go out on the other, eliminating the need for polling?
Adam
If both A and B are running continually, then yes. If they are only intermittently in operation, then having some sort of keep-alive and handshake protocol makes state management more transparent.
Steve Gilham
A: 

You can use Sockets. I know you already do this, but I think you have to do it on this way.

Server

ServerSocket server = new ServerSocket(2009); // 2009 is the networkport
while (acceptingClients)
{
    Socket socket = server.accept();
    ClientHandler handler = new ClientHandler(socket); // ClientHandler, you have to make by yourself
    Thread thread = new Thread(handler, "Handler");
    /* Use a thread, so you can connect more clients at the same
     * time. Of course ClientHandler must implement java.lang.Runnable
     */
    thread.start();
}

Client

Socket socket = new Socket(host, 2009); // host is a String with ipadress from the server. Use the same port

This code must be try-catched. For the communication you can use many type of writers and readers. In the readers and writers you have to add a stream param by constructing. These can by getted by calling

socket.getOutputStream();
socket.getInputStream();

Do not close the writers because otherwise the connection will be closed to. flush() can by a solution.

Hope this helps

Martijn Courteaux
+1  A: 

It sounds like you are looking for some type of group communication. This can be accomplished using IP multicast, and a Java library which comes in very handy then is JGroups (adds reliability and group membership). If you don't mind abstracting IP away, you might want to look into Java Messaging Service, which is a standard interface to many messaging implementations which give you reliability and transactions for publish-subscribe and queue-type communications.

disown