服务端:
public class Test1 { public static void main(String[] args) { ServerRead serverRead = new ServerRead(); serverRead.start(); while (true){ ServerSocket serverSocket = null; Socket socket = null; DataInputStream in = null; DataOutputStream out = null; try { serverSocket = new ServerSocket(6666); socket = serverSocket.accept(); //阻塞 in = new DataInputStream(socket.getInputStream()); System.out.println(in.readUTF()); out = new DataOutputStream(socket.getOutputStream()); //阻塞 Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); out.writeUTF("server:"+s); } catch (IOException e) { e.printStackTrace(); }finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } try { in.close(); } catch (IOException e) { e.printStackTrace(); } try { socket.close(); } catch (IOException e) { e.printStackTrace(); } try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
public class Test{ public static void main(String args[]){ while (true){ Socket socket = null; DataInputStream in = null; DataOutputStream out = null; try { socket = new Socket("127.0.0.1",6666); out = new DataOutputStream(socket.getOutputStream()); Scanner scanner = new Scanner(System.in); //阻塞 String s = scanner.nextLine(); out.writeUTF("client:"+s); //阻塞 in = new DataInputStream(socket.getInputStream()); System.out.println(in.readUTF()); } catch (IOException e) { e.printStackTrace(); }finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } try { out.close(); } catch (IOException e) { e.printStackTrace(); } try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
问题:当任意一端发送两个消息以上,第一个问题是第二条(包括第二条)以后的消息不会立即发送到另外一端;第二个问题是从第三条开始的消息消失了。由于每个程序都是单线程的,而有的代码会造成线程阻塞,所以导致后面的代码执行不了。
解决方法:使用多线程
客户端
public class Test{ public static void main(String args[]){ while (true){ Socket socket = null; DataInputStream in = null; DataOutputStream out = null; try { socket = new Socket("127.0.0.1",6666); out = new DataOutputStream(socket.getOutputStream()); Scanner scanner = new Scanner(System.in); //阻塞 in = new DataInputStream(socket.getInputStream()); ClientRead clientRead = new ClientRead(in); clientRead.start(); // System.out.println(in.readUTF()); //阻塞 while (scanner.hasNext()){ String s = scanner.nextLine(); out.writeUTF("client:"+s); } } catch (IOException e) { e.printStackTrace(); }finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } try { out.close(); } catch (IOException e) { e.printStackTrace(); } try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } } class ClientRead extends Thread{ DataInputStream in; public ClientRead(DataInputStream in){ this.in = in; } public void run(){ while (true){ try { System.out.printf("%30s\n",in.readUTF()); } catch (IOException e) { e.printStackTrace(); } } } }
服务器端
public class Test1 { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; DataInputStream in = null; DataOutputStream out = null; try { serverSocket = new ServerSocket(6666); socket = serverSocket.accept(); //阻塞 in = new DataInputStream(socket.getInputStream()); ServerRead serverRead = new ServerRead(in); serverRead.start(); // System.out.println(in.readUTF()); out = new DataOutputStream(socket.getOutputStream()); //阻塞 Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ String s = scanner.nextLine(); out.writeUTF("server:"+s); } } catch (IOException e) { e.printStackTrace(); }finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } try { in.close(); } catch (IOException e) { e.printStackTrace(); } try { socket.close(); } catch (IOException e) { e.printStackTrace(); } try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } class ServerRead extends Thread{ DataInputStream in = null; public ServerRead(DataInputStream in){ this.in = in; } public void run(){ while (true){ try { System.out.printf("%30s\n",in.readUTF()); } catch (IOException e) { e.printStackTrace(); } } } }