简体   繁体   English

Java Server和Client集成在一个程序中

[英]Java Server and Client in one program

i want to synchronize 2 Collections with each other. 我想彼此同步2个收藏夹。 If something changes at the Server side, the connected Clients get updated. 如果服务器端发生了某些变化,则连接的客户端将得到更新。 I have a quite basic question. 我有一个很基本的问题。 Do I now need to copy my java project and program the server in one and the client in the other one? 现在是否需要复制我的Java项目,并在其中一个服务器中编程服务器,而在另一个项目中对客户端编程? But that sounds like quite a lot of unnecessary work. 但这听起来是很多不必要的工作。 Can't I implement it all in one project an then start server and client in one main? 我不能在一个项目中实现所有功能,然后在一个主项目中启动服务器和客户端吗? Do I need threads? 我需要螺纹吗? I'm kind of stuck what the best way is here. 我对这里最好的方法感到困惑。 Thanks in advance. 提前致谢。

Because codereview doesn't allow my code cause it's not yet working, i post it now here in the hope, that you can help me. 因为codereview不允许我的代码导致它无法正常工作,所以我现在将其张贴在这里,希望对您有所帮助。

public class Server implements Runnable{
private String hostName = "127.0.0.1";
private int portNumber;
private ServerSocket serverSocket;
private Socket clientSocket;

public Server(int port){
    this.portNumber = port;
}

public void run(){
    String line = "";
    PrintWriter out = null;
    BufferedReader in = null;
    BufferedReader stdIn = null;

    try{
        this.serverSocket = new ServerSocket(this.portNumber); 
    }catch (IOException e) {
        System.out.println("Could not listen on port");
    }

    try{
        clientSocket = serverSocket.accept();
    } catch (IOException e) {
        System.out.println("Accept failed");
    }

    try{
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        out = new PrintWriter(clientSocket.getOutputStream(), true);
    }catch (IOException e) {
        System.out.println("Read failed");
    }

    while(true){

        try{
            line = in.readLine();

        }catch (IOException e) {
            System.out.println("Read failed");
        }

        System.out.println("line: "+line);
    }


}

protected void finalize(){
//Objects created in run method are finalized when
//program terminates and thread exits
    try{
        serverSocket.close();
    }catch (IOException e) {
        System.out.println("Could not close socket");
    }
 }

} }

public class Client implements Runnable{
private String hostName = "127.0.0.1";
private int portNumber = 6602;
private Socket clientSocket = null;

public Client(Socket client){
    this.clientSocket = client;
}

public Client(int portNumber, String hostName){
    this.portNumber = portNumber;
    this.hostName = hostName;
}

public void run(){
    String line;
    PrintWriter out = null;
    BufferedReader in = null;
    BufferedReader stdIn = null;

    try{
        if(clientSocket == null)
            this.clientSocket = new Socket(this.hostName, this.portNumber);
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        stdIn = new BufferedReader(new InputStreamReader(System.in));
        out.println("Test string from client");
    }catch (IOException e){
        System.out.println("in or out failed");
    }   

}

} }

public class DebugServerClient {

public static void testServerClient(){
    int port = 6602;
    Server srv = new Server(port);
    Client clt = new Client(port, "127.0.0.1");

    Thread s = new Thread(srv);
    s.start();

    Thread c = new Thread(clt);
    c.start();


}

} }

I changed it now to this and it seems to work. 我现在将其更改为此,它似乎可以工作。 Is this a good way? 这是个好方法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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