Click here to Skip to main content
15,887,966 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have multiple clients and two server. Clients should be connected with server1 than server one connected with server2. But I want to pass data from clients to server2 throw server1. Is there any possibility?

What I have tried:

Client
import java.net.*;
import java.io.*;

public class Client
{
    // initialize socket and input output streams
    private Socket socket            = null;
    private DataInputStream  input   = null;
    private DataOutputStream out     = null;
 
    // constructor to put ip address and port
    @SuppressWarnings("deprecation")
	public Client(String address, int port)throws IOException 
    {
        // establish a connection
        try
        {
            socket = new Socket(address, port);
            System.out.println("Connected");
 
            // takes input from terminal
            input  = new DataInputStream(System.in);
 
            // sends output to the socket
            out    = new DataOutputStream(socket.getOutputStream());
        }
        catch(UnknownHostException u)
        {
            System.out.println(u);
        }
        catch(IOException i)
        {
            System.out.println(i);
        }
 
        // string to read message from input
        String line = "";
 
        // keep reading until "Over" is input
        while (!line.equals("Over"))
        {
            try
            {
                line = input.readLine();
                out.writeUTF(line);
            }
            catch(IOException i)
            {
                System.out.println(i);
            }
        }
 
        // close the connection
        try
        {
            input.close();
            out.close();
            socket.close();
        }
        catch(IOException i)
        {
            System.out.println(i);
        }
    }
 
    public static void main(String args[]) throws IOException
    {
        @SuppressWarnings("unused")
		Client client = new Client("127.0.0.1", 5000);
    }
}

Server1
import java.net.*;
import java.io.*;

public class ServerThread
{
    //initialize socket and input stream,
	private Socket    socket   = null;
	private Socket    socket1   = null;
    private ServerSocket    server   = null;
    private DataInputStream in       =  null;
    private DataInputStream inn      =  null;
    private DataOutputStream out      =  null;
 
    // constructor with port
    public ServerThread(String address,int port)throws IOException 
    {
        // starts server and waits for a connection
        try
        {
            server = new ServerSocket(5000);
            System.out.println("Server started");
 
            System.out.println("Waiting for a client ...");
 
            socket = server.accept();
            System.out.println("Client accepted");
 
            // takes input from the client socket
            in = new DataInputStream(
                new BufferedInputStream(socket.getInputStream()));
            String line = "";
            while (!line.equals("Over"))
            {
                try
                {
                    line = in.readUTF();
                    System.out.println(line);
 
                }
                catch(IOException i)
                {
                    System.out.println(i);
                }
            }
            socket1 = new Socket(address, 5001);
            
            System.out.println("Connected");
            inn  = new DataInputStream(
                    new BufferedInputStream(socket.getInputStream()));;
            out    = new DataOutputStream(socket1.getOutputStream());
            
 
            // reads message from client until "Over" is sent
           
            System.out.println("Closing connection");
 
            // close connection
            socket.close();
            socket1.close();
            in.close();
            inn.close();
            out.close();
        }
        catch(IOException i)
        {
            System.out.println(i);
        }
    }
 
    public static void main(String args[]) throws IOException
    {
        @SuppressWarnings("unused")
		ServerThread server = new ServerThread("127.0.0.1",5000);
       
    }

}

Server2
import java.net.*;
import java.io.*;

public class SereverMain
{
    //initialize socket and input stream
    private Socket          socket1   = null;
    private ServerSocket    server2   = null;
    private DataInputStream in1      =  null;
 
    // constructor with port
    public SereverMain(int port)throws IOException 
    {
        // starts server and waits for a connection
        try
        {
            server2 = new ServerSocket(port);
            System.out.println("Server started");
 
            System.out.println("Waiting for a client ...");
 
            socket1 = server2.accept();
            System.out.println("Client accepted");
 
            // takes input from the client socket
            in1 = new DataInputStream(
                new BufferedInputStream(socket1.getInputStream()));
 
            String line = "";
 
            // reads message from client until "Over" is sent
            while (!line.equals("Over"))
            {
                try
                {
                    line = in1.readUTF();
                    System.out.println(line);
 
                }
                catch(IOException i)
                {
                    System.out.println(i);
                }
            }
            System.out.println("Closing connection");
 
            // close connection
            socket1.close();
            in1.close();
        }
        catch(IOException i)
        {
            System.out.println(i);
        }
    }
 
    public static void main(String args[]) throws IOException
    {
        @SuppressWarnings("unused")
		SereverMain server = new SereverMain(5001);
    }
}
Posted
Updated 1-Aug-18 0:32am

1 solution

You have to send the packets to Server2 just after receiving them from the client in Server1.

Untested Server1 snippet:
Java
// Open connection to Server2
socket1 = new Socket(address, 5001);
out = new DataOutputStream(socket1.getOutputStream());

// reads message from client until "Over" is sent
while (!line.equals("Over"))
{
    try
    {
        line = in1.readUTF();
        System.out.println(line);

        // Forward message to Server2
        out.writeUTF(line);
     
    }
    catch(IOException i)
    {
        System.out.println(i);
    }
}
// Close everything here
Your code is opening the connection to Server2 when the client has finished communication by sending "Over".

Note also that your current code of both servers will terminate when a client finished the communication. A normal server would not do that but enter listening state again to accept further connections from clients.
 
Share this answer
 
Comments
FerdouZ 1-Aug-18 12:17pm    
Thank you Mr. Jochen. Now I can understand what was my fault.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900