Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Server to Server connection (with clients included) ?

    I need help to make a Server to server connection

    I already have a server connected to clients, clients send msgs and it echoes back to all of them and now i want when a client sends a msg it echoes on his server and the other server too .. so when any of the clients on any of the servers sends a msg it is broadcasted all over the servers to all clients

    This is my SERVER code

    Code:
    import java.io.*;
    import java.net.*;
    
    public class MultiThreadChatServer {
    
    	// Declaration section: This part to declare the server socket, client
    	// socket, input stream
    	// and output stream
    
    	static Socket clientSocket = null;
    	static ServerSocket serverSocket = null;
    
    	// server can hold up to 10 clients
    
    	static clientThread t[] = new clientThread[10];
    
    	public static void main(String args[]) {
    
    		int port_number = 6000;
    
    		if (args.length < 1) {
    			System.out.println("Server Started \n" + "Now using port number="
    					+ port_number);
    		} else {
    			port_number = Integer.valueOf(args[0]).intValue();
    		}
    
    		// Initialization section: Where I try to open a server socket on the
    		// given port
    
    		try {
    			serverSocket = new ServerSocket(port_number);
    		} catch (IOException e) {
    			System.out.println(e);
    		}
    
    		// Create a socket object from the ServerSocket to listen and accept
    		// connections
    		// Open input and output streams for this socket will be created in
    		// client's thread since every client is served by the server in
    		// an individual thread
    
    		while (true) {
    			try {
    				clientSocket = serverSocket.accept();
    				for (int i = 0; i <= 9; i++) {
    					if (t[i] == null) {
    						(t[i] = new clientThread(clientSocket, t)).start();
    						break;
    					}
    				}
    			} catch (IOException e) {
    				System.out.println(e);
    			}
    		}
    	}
    }
    
    // This client thread opens the input and the output streams for a particular
    // client,
    // ask the client's name, informs all the clients currently connected to the
    // server about the fact that a new client has joined the chat room,
    // and as long as it receive data, echos that data back to all other clients.
    // When the client leaves the chat room this thread informs also all the
    // clients about that and terminates.
    
    class clientThread extends Thread {
    
    	DataInputStream is = null;
    	PrintStream os = null;
    	Socket clientSocket = null;
    	clientThread t[];
    
    	public clientThread(Socket clientSocket, clientThread[] t) {
    		this.clientSocket = clientSocket;
    		this.t = t;
    	}
    
    	public void run() {
    		String line;
    		String name;
    		try {
    			is = new DataInputStream(clientSocket.getInputStream());
    			os = new PrintStream(clientSocket.getOutputStream());
    			os.println("Enter your name.");
    			name = is.readLine();
    			os.println("Hello " + name
    					+ " you can now start chatting with all the connected chat-mates");
    			for (int i = 0; i <= 9; i++)
    				if (t[i] != null && t[i] != this)
    					t[i].os.println(".." + name + " has entered the chat room ..");
    			while (true) {
    				line = is.readLine();
    				if (line.startsWith("/quit"))
    					break;
    				for (int i = 0; i <= 9; i++)
    					if (t[i] != null)
    						t[i].os.println("<" + name + "> " + line);
    			}
    			for (int i = 0; i <= 9; i++)
    				if (t[i] != null && t[i] != this)
    					t[i].os.println("" + name + " has left the chat room ..");
    
    			os.println("Bye " + name + " ..");
    
    			// Clean up:
    			// Set to null the current thread variable such that other client
    			// could
    			// be accepted by the server
    
    			for (int i = 0; i <= 9; i++)
    				if (t[i] == this)
    					t[i] = null;
    
    			// close the output stream
    			// close the input stream
    			// close the socket
    
    			is.close();
    			os.close();
    			clientSocket.close();
    		} catch (IOException e) {
    		}
    		;
    	}
    }

  • #2
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    I think the perfect solution to this would be a Queue messaging system like JMS:
    Java Message Service. An implementation of that would be something called JORAM which would allow the following:

    Code:
    Define a topic queue for each server
    Make all other servers subscribe to each other servers topic queue
    Put any message which comes into the server on the servers topic queue
    
    When a message is put onto the servers queue - that message is sent automagically to allow subscribed participants i.e all other servers! Boom - problem solved!
    Link to JORAM and info on JMS:

    http://joram.ow2.org/

    http://en.wikipedia.org/wiki/Java_Message_Service

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well this is a university project .. so I just need a very simple connection in TCP between the two server, nothing more

  • #4
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    The JORAM solution would fit perfectly with that - would be the correct way to design this program....

    If you just wanted another TCP connection - just have another client and server connection between the servers + exchange info using that .... If its 'nothing more' I suggest you do it because you are ignoring the advice I have given you.

  • #5
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    um sry don't take it offensively that i didn't take ur advice i would have, but its required that we do a TCP direct connection

    the problem is when i try to do the same connection i did with the client-to-server i get errors and um so confused ?

    can i find any help here with that code "like an addition to my existing code" that would help me establish a TCP connection from that server to another similar one ?

  • #6
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Well its simple.

    You start another ServerSocket on a different port for the server to server connection.

    Then have the other server connect on that port as a client and vice versa!

  • #7
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    is it possible if u show me a "sample or a template code", I have been trying to do it myself but i end either with an endless loop of the two servers messaging each other or just the connection without any message reception :S

  • #8
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    If you were to post the entire project code from eclipse in a zipped project I would consider it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •