简体   繁体   中英

serversocket server + client issues

ok is hard for me to describe my problem now but then i will try my best to in order for me to get some assist.

technically i have a server.java and client.java as a super class. and my layout structure for my server and client connection goes like this

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

my main problem is the this CLIENT/SERVER part is 1 driver class that calls 2 different classes which is CLIENT and SERVER together... and this creates a problem when my CLIENT sends something that needs to be received by MAIN SERVER side needs to go through CLIENT/SERVER part. if is that condition happens..

the CLIENT of course need to interact with CLIENT/SERVER (SERVER) part because is a SERVER that accepts the CLIENT data. but now i wanted the (SERVER) part in the CLIENT/SERVER to transfer the data to (CLIENT) in the CLIENT/SERVER part so that it can be send to the MAIN SERVER

how is it possible for me to write something that allows the CLIENT/SERVER to interact with each other so it can transfer the data between them vise versa? how ever this is my code for calling the CLIENT and SERVER together

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());
    }
}

}

the above is only the driver class that calls the 2 object class which is the SERVER and CLIENT side.

i will attach my slaveserver and slaveclient code here but i am not sure how to do it like you said

 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();
        }
    }
}
}

So you are trying to write a proxy?

You you need to give the server half of the proxy a reference to the client half, so that it can forward the data.

Then create a method in the client half to accept messages from the server half.

So each message you read in at the server half, you pass to the client half. The client half can then pass it to the real server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM