serversocket服务器+客户端问题

时间:2012-02-27 21:33:16

标签: java serversocket

我现在很难描述我的问题但是我会尽力为我提供帮助。

从技术上讲,我有一个server.java和client.java作为超类。我的服务器和客户端连接的布局结构就像这样

MAIN SERVER --- CLIENT/SERVER ----- CLIENT

我的主要问题是这个CLIENT / SERVER部分是1个驱动程序类,它调用了两个不同的类,它们一起是CLIENT和SERVER ......当我的CLIENT发送需要MAIN SERVER端接收的内容时,这会产生问题需要通过CLIENT / SERVER部分。如果那种情况发生..

CLIENT当然需要与CLIENT / SERVER(SERVER)部分进行交互,因为它是一个接受CLIENT数据的SERVER。但现在我希望CLIENT / SERVER中的(SERVER)部分将数据传输到CLIENT / SERVER部分中的(CLIENT),以便可以将其发送到MAIN SERVER

我怎么可能写一些允许CLIENT / SERVER相互交互的东西,所以它可以在它们之间传输数据,反之亦然?这是我的一起调用CLIENT和SERVER的代码

public class Slave {
public static void main(String args []) throws IOException{
    try{
    // set Config file settings to slave mode   
    Config cfg = new Config("Slave");
    String MasterServerIP = cfg.getProperty("ServerIP");
    String MasterServerPort = cfg.getProperty("ServerPort");
    String SlaveServerPort = cfg.getProperty("ListeningPort");

    System.out.println("Slave Client connecting to Master Server");

    // start connect to master server by calling the SlaveClient class
    new SlaveClient(MasterServerIP,Integer.parseInt(MasterServerPort)).start();

    int numClient = 0;
    ServerSocket listener = new ServerSocket(Integer.parseInt(SlaveServerPort));
    System.out.println("Server starts running");

    try{
        while(true){
            // start listening to the server port by calling SlaveServer class
            new SlaveServer(listener.accept(), numClient++, Integer.parseInt(SlaveServerPort)).start();
        }
    } finally {
        listener.close();
    }

    } catch (FileNotFoundException file) {
        System.out.println("File Not Found Error: "+file.getMessage());
    }
}

}

以上只是调用2对象类的驱动程序类,即SERVER和CLIENT端。

我会在这里附上我的slaveserver和slaveclient代码,但我不知道怎么做就像你说的那样

 public class SlaveServer extends Server {
private JFrame frame = new JFrame();
private JTextArea msgArea = new JTextArea();
private JTextArea connectionArea = new JTextArea();

// SlaveServer Constructor
public SlaveServer(Socket socket, int numClient, int port) {
    super(socket, numClient, port);
}
   public void writeToMsg(String msg){
    msgArea.append(msg+"\n");
}

public void writeToConnection(String msg){
    connectionArea.append(msg+"\n");
}


 public void run(){
    try{
        startGUI();
        // initial BufferedReader and PrintWriter object by binding it with Socket
        BufferedReader in = new BufferedReader(new InputStreamReader(getSocket().getInputStream()));
        PrintWriter out = new PrintWriter(getSocket().getOutputStream(), true);

        // welcome message send from server to client
        out.println("Welcome to the Slave Server port:"+getPort()+" client #"+getNumClient());

        while(true){
            String readmsg = in.readLine();
            writeToMsg(readmsg);
        }

    } catch (IOException e){
        writeToMsg("Error in closing Socket");
    }
    writeToConnection("Connection from client #"+getNumClient()+" is closed");
}
 }






 public class SlaveClient extends Client{
private BufferedReader in;
private PrintWriter out;
private JFrame frame = new JFrame();
private JTextArea msgArea = new JTextArea();
private JTextArea connectionArea = new JTextArea();

// SlaveClient Constructor
public SlaveClient(String ip, int port) {
    super(ip, port);
}
 public void run(){
    startGUI();
    Socket sock = null;
    try {
        sock = new Socket(getIp(), getPort());
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        out = new PrintWriter(sock.getOutputStream(), true);
    } catch (IOException e) {
        e.printStackTrace();
    }

    out.println("TEST");

    // while loop for reading message from server
    while(true){
        try {
            getMsg(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

所以你正在尝试编写代理?

您需要为服务器的一半提供对客户端的引用,以便它可以转发数据。

然后在客户端创建一个方法,以接受来自服务器一半的消息。

因此,您在服务器一半读取的每条消息都会传递给客户端一半。然后客户端可以将其传递给真实服务器。