import java.io.*; import java.net.Socket; import java.util.Scanner; public class ServerThread extends Thread{ private Socket socket = null; public ServerThread(Socket socket) { this.socket = socket; } @Override public void run() { System.out.println("服务端--------接"); 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("服务端------>>客户端--服务器欢迎你"); pw.flush(); } catch (Exception e) { // TODO: handle exception } finally { //关闭资源 try { if (null != pw) pw.close(); if (null !=os ) os.close(); if (null!= br ) br.close(); if ( null!= isr) isr.close(); if (null != is) is.close(); if (null != socket) socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
socketserver服务端多线程服务
最新推荐文章于 2024-09-03 15:21:06 发布