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 2 of 2
  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) ?

    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
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,307
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Ask a mod to move you over to the right forum.

    It is your responsibility to die() if necessary….. - PHP Manual


  •  

    Posting Permissions

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