package socket;import java.io.*;import java.net.Socket;public class ServerThread extends Thread {private Socket socket = null;public ServerThread(Socket socket) {this.socket = socket;}@Overridepublic void run() {InputStream is = null;InputStreamReader isr = null;BufferedReader br = null;OutputStream os = null;PrintWriter pw = null;try {is = socket.getInputStream();isr = new InputStreamReader(is);br = new BufferedReader(isr);String info = null;while ((info=br.readLine())!= null) {System.out.println("我是服务器,客户端说:" + info);}socket.shutdownInput();os = socket.getOutputStream();pw = new PrintWriter(os);pw.write("服务器欢迎你,这是你的幸运数字: " + (int) (Math.random() * 10000));pw.flush();} catch (Exception e) {e.printStackTrace();} finally {//关闭资源try {if (pw != null)pw.close();if (os != null)os.close();if (br != null)br.close();if (isr != null)isr.close();if (is != null)is.close();if (socket != null)socket.close();} catch (IOException e) {e.printStackTrace();}}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。