Передача данных от клиентов к серверу server2 throw server1. Может ли кто-нибудь помочь мне, как это сделать?
У меня есть несколько клиентов и два сервера. Клиенты должны быть подключены к серверу 1, а не к серверу 2. Но я хочу, чтобы передавать данные от клиента к серверу server2 бросить сервер1. Есть ли такая возможность?
Что я уже пробовал:
Клиент
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); } }
Сервер1
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); } }
Сервер2
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); } }