如何正确使用输入/输出流?服务器/客户端/线程

llycmphe  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(222)

我正在尝试编写一个程序,其中客户端使用输出流向服务器发送字符串。服务器将看到它是什么字符串,它将它与条件字符串进行比较,然后将它发送回客户端,并在客户端显示一条消息。我想了解它是如何工作的,以及到目前为止我的代码有什么问题。一旦我得到它,我将用javafx实现它。
客户

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class MyClient {

    static Socket socket;
    static int clientNo;
    static DataOutputStream toServer;
    static DataInputStream fromServer;

    public static void main(String[] args) {
        try{
            socket = new Socket("localhost", 8888);
            System.out.println("Client connected to the server");

            fromServer = new DataInputStream(socket.getInputStream());
            toServer = new DataOutputStream(socket.getOutputStream());

            toServer.writeBytes("listAll");
            toServer.flush();
            System.out.println("Sending string to the ServerSocket");

            } catch (IOException ex) {
            System.err.println(ex.getMessage());
        }  
    }

}

服务器

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class MyServer {

    static final int PORT = 8888;

    static ServerSocket serverSocket;
    static Socket socket;

    public static void main(String[] args) {
            try {
                serverSocket = new ServerSocket(PORT);
                System.out.println("Server started at " + new Date());
                System.out.println("Server on PORT: " + PORT + " is open");
                System.out.println("Server is ready for multiple clients!");
                System.out.println("Waiting for request...");
                while(true){
                    socket = MyServer.serverSocket.accept();
                    new Thread(new HandleClient(MyClient.socket)).start();
                }

            } catch (IOException ex) {
            System.err.println("Server Error: " + ex.getMessage());

            }
    }
}

线

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class HandleClient implements Runnable {

    private Socket socket;
    public static int clientNo = 1;
    public static int condition;

    static DataInputStream input ;
    static DataOutputStream output;
    static String message;

    public HandleClient(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        switch (condition) {
            case 1:
                break;
            case 2:
                System.out.println("list all");
                break;
            case 3:
                break;
            default:
                System.out.println("Client #" + clientNo + " connected!");
                clientNo++;
                try {

                    input = new DataInputStream(socket.getInputStream());
                    output = new DataOutputStream(socket.getOutputStream());

                        message = input.readUTF();
                        System.out.println(message);

                        if("listAll".equals(message)){
                            HandleClient.condition = 2;
                            new Thread(new HandleClient(socket)).start();
                        } 
                } catch (IOException ex) {
                    System.err.println("One of the clients disconnected");
            }
                break;
        }
    }
}

我真的很感激你的帮助!我知道我可能丢失了一些代码来将响应发送回客户端?请告诉我我要去的方向是否好。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题