import java.io.*; import java.net.*; import java.util.Scanner; public class Client { public static Socket client = null; public static String choice; public static DataOutputStream dos; public static DataInputStream dis = null; public static String str2 = null; public static void main(String[] args) throws Exception { new Thread(new Server()).start(); // 启动服务器 client = new Socket("127.0.0.1", 8878); dis = new DataInputStream(client.getInputStream()); dos = new DataOutputStream(client.getOutputStream()); System.out.println("1.登录"); System.out.println("2.退出"); System.out.println("请输入对应数字进行操作"); Scanner sca = new Scanner(System.in); choice = sca.next(); if (choice.equals("1")) { dos.writeUTF("LANDING"); dos.flush(); str2 = dis.readUTF(); // 这里能读取了 System.out.println("测试是否运行 读取数据: " + str2); System.out.println("客户端退出"); } else if (choice.equals("2")) { System.exit(0); } } } class Server implements Runnable { ServerSocket server = null; Socket socket = null; public DataInputStream dis; public DataOutputStream dos; public void run() { try { server = new ServerSocket(8878); System.out.println("服务器已启动"); socket = server.accept(); System.out.println("客户端已连接:" + socket.getInetAddress()); dis = new DataInputStream(socket.getInputStream()); dos = new DataOutputStream(socket.getOutputStream()); // 1.登录用户请求; if (dis.readUTF().equals("LANDING")) { try { dos.writeUTF("gggggggggg"); dos.flush(); } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } } }
换成
str2 = dis.readLine();试试
import java.io.*; import java.net.*; import java.util.Scanner; public class Client { public static Socket client = null; public static String choice; public static DataOutputStream dos; public static DataInputStream dis = null; public static String str2 = null; public static void main(String[] args) throws Exception { new Thread(new Server()).start(); // 启动服务器 client = new Socket("127.0.0.1", 8878); dis = new DataInputStream(client.getInputStream()); dos = new DataOutputStream(client.getOutputStream()); System.out.println("1.登录"); System.out.println("2.退出"); System.out.println("请输入对应数字进行操作"); Scanner sca = new Scanner(System.in); choice = sca.next(); if (choice.equals("1")) { dos.writeUTF("LANDING"); dos.flush(); str2 = dis.readUTF(); // 这里能读取了 System.out.println("测试是否运行 读取数据: " + str2); System.out.println("客户端退出"); } else if (choice.equals("2")) { System.exit(0); } } } class Server implements Runnable { ServerSocket server = null; Socket socket = null; public DataInputStream dis; public DataOutputStream dos; public void run() { try { server = new ServerSocket(8878); System.out.println("服务器已启动"); socket = server.accept(); System.out.println("客户端已连接:" + socket.getInetAddress()); dis = new DataInputStream(socket.getInputStream()); dos = new DataOutputStream(socket.getOutputStream()); // 1.登录用户请求; if (dis.readUTF().equals("LANDING")) { try { dos.writeUTF("gggggggggg"); dos.flush(); } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } } }