你一言
2018-04-26 00:12
出现一个问题,服务器在循环里阻塞读着信息,就没出来获取数据,我两个循环都加了break才出来的 为什么老师实现了???
package CStest;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.Socket;import java.net.UnknownHostException;import java.util.Scanner;public class ClientTest { public static void main(String[] args) { try { //创建客户端与服务器连接,并向服务器提示已连接 Socket socket = new Socket("127.0.0.1", 8888); PrintWriter pw=new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); pw.write("客户端"+socket.hashCode()+"已连接"); pw.flush(); //获取服务器端响应信息 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String info = null; new ClientThread(socket).start(); new ClientThreadR(socket).start(); } catch (Exception e) { e.printStackTrace(); } }}
package CStest;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;public class ClientThread extends Thread { Socket socket = null; public ClientThread(Socket socket) { this.socket = socket; } public void run() { BufferedReader br = null; String info = null; try { //获取服务器信息 br = new BufferedReader(new InputStreamReader(socket.getInputStream())); while (true) { if ((info = br.readLine()) != null) { System.out.println(info); } } } catch (Exception e) { e.printStackTrace(); } }}
package CStest;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.Socket;import java.util.Scanner;public class ClientThreadR extends Thread { Socket socket = null; Scanner inp=new Scanner(System.in); public ClientThreadR(Socket socket) { this.socket = socket; } public void run() { // 给服务器发送信息 PrintWriter pw = null; try { pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));// pw.write("客户端"+socket.hashCode()+"已建立连接:\n");// pw.flush(); while(true) { System.out.println("客户端"+socket.hashCode()+":"); pw.write(inp.nextLine()+"\n"); pw.flush(); } } catch (Exception e) { e.printStackTrace(); } }}
package CStest;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;import java.util.ArrayList;import java.util.List;public class ServerTest { public static void main(String[] args) { try { ServerSocket server = new ServerSocket(8888); System.out.println("服务器启动\n"); List<Socket> list = new ArrayList<Socket>(); while (true) { Socket socket = server.accept(); list.add(socket); //启动线程 new ServerThreadR(socket,list).start(); System.out.println("已连接Client" + socket.hashCode()+":"); } } catch (Exception e) { e.printStackTrace(); } }}
package CStest;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.Socket;import java.util.ArrayList;import java.util.List;public class ServerThreadR extends Thread { Socket socket = null; List<Socket> list = null; public ServerThreadR(Socket socket, List<Socket> list) { this.socket = socket; this.list = list; } PrintWriter pw = null; public void run() { BufferedReader br = null; String info = null; try { // 客户端登录,服务器向客户端发送 pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); pw.write("客户端"+socket.hashCode()+"登录\n"); pw.flush(); while (true) { // 读客户端信息,监听 br = new BufferedReader(new InputStreamReader(socket.getInputStream())); if ((info = br.readLine()) != null) { ServerReader(info); System.out.println("Client" + socket.hashCode() + ": " + info); } } } catch (Exception e) { e.printStackTrace(); } } public void ServerReader(String str) { for (int i = 0; i < this.list.size(); i++) { PrintWriter pw = null; try { pw = new PrintWriter(this.list.get(i).getOutputStream()); pw.write(str + "\n"); pw.flush(); } catch (IOException e) { e.printStackTrace(); } } }}
//粘贴的有点乱,一共五个类,客户端和一个监听一个输入两个线程,服务端和一个监听,你可以试试,不会来交流下,或者粘贴不对的,可以留下邮箱
贴代码!
Java Socket应用---通信是这样练成的
125013 学习 · 590 问题
相似问题