套接字编程将数据从服务器传输到服

时间:2016-12-17 16:14:46

标签: java sockets

我有一个client_1,centerlized_server和server_1 客户端向centerlized_server发送请求 和centerlized_server将请求传输到server_1

问题是我如何将请求从centerlized_serve传输到server_1 ??

感谢您的帮助

... Client1代码

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;



public class Client1 {

private Socket server;
private ObjectOutputStream out;
private ObjectInputStream in;

public Client1() {

    try 
    {
        server = new Socket("localhost", 5050);
        out = new ObjectOutputStream(server.getOutputStream());
        in = new ObjectInputStream(server.getInputStream());

        while (true) 
        {
            Scanner s = new Scanner(System.in);
            System.out.println("press 2 to date or 1 for time:");
            Message msg = new Message();
            msg.Type = s.nextInt();
            out.writeObject(msg);
            msg = (Message) in.readObject();
            System.out.println(msg.message);
        }
    } 
    catch (Exception ex) 
    {
        ex.printStackTrace();
    }
}

public static void main(String[] args) {
    new Client1();
}
 }

CernterlizedServer代码

 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.net.Socket;
 import java.util.Date;


 public class CentralizedServer extends Thread {

private Socket client;
private ObjectOutputStream out;
private ObjectInputStream in;

public CentralizedServer(Socket current_socket) 
{
    try
    {
        client = current_socket;
        out = new ObjectOutputStream(client.getOutputStream());
        in = new ObjectInputStream(client.getInputStream());
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }
}

@Override
public void run() 
{
    try 
    {
        while (true) 
        {
            Message msg = (Message) in.readObject();
            if (msg.Type == 1)
            {
                OnTimeRequst();
            } 
            else if (msg.Type == 2) 
            {
                OnDateRequst();
            }
        }
    } 
    catch (Exception ex) 
    {
        try
        {
            out.close();
            in.close();
            client.close();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
}

private void OnTimeRequst() throws IOException 
{
    Date d = new Date();
    Message msg = new Message();
    msg.message = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
    out.writeObject(msg);
}

private void OnDateRequst() throws IOException 
{
    Date d = new Date();
    Message msg = new Message();
    msg.message = d.toString() + "";
    out.writeObject(msg);
}
       }

服务器代码

 import java.net.ServerSocket;
 import java.util.ArrayList;


 public class Server {

ServerSocket server;
ArrayList<CentralizedServer> list = new ArrayList<>();

public Server() 
{
    try 
    {
        server = new ServerSocket(5050);
        while (true) 
        {
            CentralizedServerthr = new CentralizedServer(server.accept());
            list.add(thr);
            thr.start();
        }
    } 
    catch (Exception ex)
    {
        ex.printStackTrace();
    }

}

public static void main(String... args) {
    new Server();
}
    }

1 个答案:

答案 0 :(得分:1)

您可以设计以下两种方式之一。

选项1:重定向。集中式服务器将响应发送回客户端以重定向&#34;他到server1。 (ala http 30x回复)。然后客户端只与server1建立单独的连接并重新发送请求。

选项2:代理。在收到来自客户端的请求后,集中式服务器与server1建立连接并代表客户端发送请求。当集中式服务器收到来自server1的响应时,它只是将响应转发给客户端。

重定向方法更简单,但如果&#34; server1&#34;旨在保护其免受直接客户端访问或仅可从其他服务器访问。

代理方法有点困难,但允许不同的设计。