[英]How to implement 2 different servers, which get called by client with the same object
Hy, i´m doing some exercises, heres the background to know.嗨,我正在做一些练习,这是要知道的背景。 imagine you are a travel agency想象你是一家旅行社
-you have to search for the cheapest flight for your customers - 您必须为您的客户寻找最便宜的航班
-they tell you how many miles they gone flight - 他们告诉你他们飞行了多少英里
-which area they wanna sit, default - business - firstclass. - 他们想坐在哪个区域,默认 - 商务 - 头等舱。
travel agency is the client旅行社是客户
and there are 2 airlines where we can choose.我们可以选择 2 家航空公司。
this 2 airlines airline1: dreamLine (Server1), cloudLine(Server2),这 2 家航空公司航空公司 1:dreamLine (Server1)、cloudLine(Server2)、
this servers get a request via commandline/client and calculate their value and return it to the client.该服务器通过命令行/客户端获取请求并计算其值并将其返回给客户端。 just for example server1 should return 200€ and server 2 should return 600 €, calculation isnt the problem more how 2 servers can work sequential or simultaniously举例来说 server1 应该返回 200 欧元,服务器 2 应该返回 600 欧元,计算不是问题更多的是 2 台服务器如何顺序或同时工作
my problem here is i dont know where i should start by separating the servers.我的问题是我不知道应该从哪里开始分离服务器。 i have implemented already both servers but i get some errors, because i need to make a queue or implement a further Handler which tells which server should start or which server should end first or do anything.我已经实现了两台服务器,但是我遇到了一些错误,因为我需要创建一个队列或实现一个进一步的处理程序,它告诉哪个服务器应该启动或哪个服务器应该首先结束或做任何事情。 i tried it already with just one server, this works, but 2 idk i think they issue is this that there are 2 sockets opened and this isnt allowed but i didnt found any info for a client which sends a request to 2 different servers, just the other way, many clients to one server, but this isnt what im searchin for我已经用一台服务器试过了,这行得通,但是我认为他们的问题是有 2 个 sockets 打开,这是不允许的,但我没有找到任何关于向 2 个不同服务器发送请求的客户端的信息,只是另一种方式,一个服务器的许多客户端,但这不是我搜索的
would be great if u can tell me what i should search for, when i wanna work with n servers and just 1 client如果你能告诉我我应该搜索什么,那会很棒,当我想使用 n 个服务器和 1 个客户端时
got already various errors once -Address already in use: JVM_Bind more -server timed out and so on曾经出现过各种错误-地址已在使用中:JVM_Bind更多-服务器超时等等
here my test classes client:这是我的测试类客户端:
public class Client {公共 class 客户端 {
public static void main(String[] args) throws UnknownHostException, IOException {
int number, temp, temp1, more;
Scanner sc = new Scanner(System.in);
Socket s = new Socket("localhost", 1342);
Scanner sc1 = new Scanner(s.getInputStream());
System.out.println("enter any number");
number = sc.nextInt();
PrintStream p = new PrintStream(s.getOutputStream());
p.println(number);
temp = sc1.nextInt();
System.out.println(temp);
sc.close();
s.close();
sc1.close();
p.close();
Scanner sc2 = new Scanner(System.in);
Socket s2 = new Socket("localhost", 1343);
Scanner sc3 = new Scanner(s.getInputStream());
System.out.println("enter any number");
number = sc.nextInt();
PrintStream p1 = new PrintStream(s2.getOutputStream());
p1.println(number);
more = sc3.nextInt();
System.out.println(temp);
System.out.println(temp+ " " +more);
}
} }
server 1:服务器 1:
public class Server {公共 class 服务器 {
public static void main(String[] args) throws IOException {
int number;
int temp;
ServerSocket s1 = new ServerSocket(1342);
Socket ss = s1.accept();
Scanner sc = new Scanner(ss.getInputStream());
number = sc.nextInt();
temp = number*2;
PrintStream p = new PrintStream(ss.getOutputStream());
p.println(temp);
}
} }
server2:服务器2:
public class Server2 {公共 class Server2 {
public static void main(String[] args) throws IOException {
int number;
int temp;
ServerSocket s1 = new ServerSocket(1343);
Socket ss = s1.accept();
Scanner sc = new Scanner(ss.getInputStream());
number = sc.nextInt();
temp = number*10;
PrintStream p = new PrintStream(ss.getOutputStream());
p.println(temp);
}
} }
cant find the error what i miss找不到我想念的错误
You can use one socket client to connect to multiple servers with a procedure like this:您可以使用一个套接字客户端通过如下过程连接到多个服务器:
Repeat the steps above for the server2.对 server2 重复上述步骤。 Remember: You must close the connection to server1, before using the same socket client for server2.请记住:您必须先关闭与 server1 的连接,然后才能对 server2 使用相同的套接字客户端。
free for all client server example with 2 servers and only 1 client: before u run the client, u have to run the servers first than u can start with the client对所有客户端服务器示例免费,有 2 个服务器,只有 1 个客户端:在您运行客户端之前,您必须先运行服务器,然后才能从客户端启动
public class Client {公共 class 客户端 {
public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
int number, temp, temp1, more;
Scanner sc = new Scanner(System.in);
Socket s = new Socket("localhost", 1342);
Scanner sc1 = new Scanner(s.getInputStream());
System.out.println("enter any number");
number = sc.nextInt();
PrintStream p = new PrintStream(s.getOutputStream());
p.println(number);
temp = sc1.nextInt();
System.out.println(temp);
s.close();
System.out.println("Server 1 closed!");
Scanner scc = new Scanner(System.in);
s = new Socket("localhost", 2555);
Scanner sc2 = new Scanner(s.getInputStream());
System.out.println("enter any number");
number = scc.nextInt();
p = new PrintStream(s.getOutputStream());
p.println(number);
more = sc2.nextInt();
System.out.println(more);
System.out.println(temp+ " " +more);
}
} }
public class Server {公共 class 服务器 {
public static void main(String[] args) throws IOException {
int number;
int temp;
ServerSocket s1 = new ServerSocket(1342);
Socket ss = s1.accept();
Scanner sc = new Scanner(ss.getInputStream());
number = sc.nextInt();
temp = number*2;
PrintStream p = new PrintStream(ss.getOutputStream());
p.println(temp);
}
} }
public class Server2 {公共 class Server2 {
public static void main(String[] args) throws IOException {
int number;
int temp;
ServerSocket s1 = new ServerSocket(2555);
Socket ss = s1.accept();
Scanner sc = new Scanner(ss.getInputStream());
number = sc.nextInt();
temp = number*10;
PrintStream p = new PrintStream(ss.getOutputStream());
p.println(temp);
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.