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

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

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

  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.net.Socket;
  5. public class MyClient {
  6. static Socket socket;
  7. static int clientNo;
  8. static DataOutputStream toServer;
  9. static DataInputStream fromServer;
  10. public static void main(String[] args) {
  11. try{
  12. socket = new Socket("localhost", 8888);
  13. System.out.println("Client connected to the server");
  14. fromServer = new DataInputStream(socket.getInputStream());
  15. toServer = new DataOutputStream(socket.getOutputStream());
  16. toServer.writeBytes("listAll");
  17. toServer.flush();
  18. System.out.println("Sending string to the ServerSocket");
  19. } catch (IOException ex) {
  20. System.err.println(ex.getMessage());
  21. }
  22. }
  23. }

服务器

  1. import java.io.IOException;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4. import java.util.Date;
  5. public class MyServer {
  6. static final int PORT = 8888;
  7. static ServerSocket serverSocket;
  8. static Socket socket;
  9. public static void main(String[] args) {
  10. try {
  11. serverSocket = new ServerSocket(PORT);
  12. System.out.println("Server started at " + new Date());
  13. System.out.println("Server on PORT: " + PORT + " is open");
  14. System.out.println("Server is ready for multiple clients!");
  15. System.out.println("Waiting for request...");
  16. while(true){
  17. socket = MyServer.serverSocket.accept();
  18. new Thread(new HandleClient(MyClient.socket)).start();
  19. }
  20. } catch (IOException ex) {
  21. System.err.println("Server Error: " + ex.getMessage());
  22. }
  23. }
  24. }

线

  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.net.Socket;
  5. public class HandleClient implements Runnable {
  6. private Socket socket;
  7. public static int clientNo = 1;
  8. public static int condition;
  9. static DataInputStream input ;
  10. static DataOutputStream output;
  11. static String message;
  12. public HandleClient(Socket socket) {
  13. this.socket = socket;
  14. }
  15. @Override
  16. public void run() {
  17. switch (condition) {
  18. case 1:
  19. break;
  20. case 2:
  21. System.out.println("list all");
  22. break;
  23. case 3:
  24. break;
  25. default:
  26. System.out.println("Client #" + clientNo + " connected!");
  27. clientNo++;
  28. try {
  29. input = new DataInputStream(socket.getInputStream());
  30. output = new DataOutputStream(socket.getOutputStream());
  31. message = input.readUTF();
  32. System.out.println(message);
  33. if("listAll".equals(message)){
  34. HandleClient.condition = 2;
  35. new Thread(new HandleClient(socket)).start();
  36. }
  37. } catch (IOException ex) {
  38. System.err.println("One of the clients disconnected");
  39. }
  40. break;
  41. }
  42. }
  43. }

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

暂无答案!

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

相关问题