简体   繁体   English

如何实现 2 个不同的服务器,它们由具有相同 object 的客户端调用

[英]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:您可以使用一个套接字客户端通过如下过程连接到多个服务器:

  1. Connect to server1 with ip address : port <> (open) .使用ip 地址连接到 server1:端口<> (打开)
  2. Request data to server1 >> (write) .向 server1 请求数据 >> (write)
  3. Wait response from server1 << (read) .等待来自 server1 << (read)的响应。
  4. disconnect from server1 <> (close) .从 server1 <> 断开(关闭)

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.

相关问题 如何实现参数相同但签名不同的方法 - how to implement the method which have parameter same but different signature 在未实现Observable的对象上调用Method时如何获得通知? - How to get Notified when Method is Called on Object that does not Implement Observable? 如何为不同类型的CRUD操作实现Hibernate Interceptor,并获取已修改的Java对象的类名? - How to implement Hibernate Interceptor for different type of CRUD actions and get the class name of the java object which has been modified? Java:如何从一个端口上的客户端套接字连接到不同的服务器? - Java: how to connect to different servers from a client socket on one port? 从地图对象获取密钥和值,并将它们发送到其他服务器 - Get the key and the value from a Map Object and send them to different Servers 如何使用节俭实现客户端/服务器对象与另一个相同类型的对象之间的通信 - How to implement communication between a client/server object with another of the same type using thrift 如何注入实现同一接口的两个不同类的两个实例? - How to inject two instances of two different classes which implement the same interface? 服务器如何获取客户端的IP和端口号TCP? - How do servers get client's IP and port number TCP? Java Tyrus WebSocket客户端@OnMessage方法在“不同”对象上被调用 - Java Tyrus WebSocket Client @OnMessage method gets called on 'different' object 将请求对象传递给在不同服务器上运行的同一应用程序的不同servlet /动作 - Passing request object to different servlet/action of same application running at different servers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM