SocketException
有没有报SocketException connection reset错误的同学?
有没有报SocketException connection reset错误的同学?
2017-10-04
服务器端: package zx_Socket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; /** * * @author yingmanji * @修改日期: * @描述:模拟慕课视频中的服务端 * */ public class TcpServer3 { public static void main(String[] args) { try { ServerSocket ss=new ServerSocket(8888); System.out.println("服务器已开启,等待连接"); Socket s=ss.accept(); InputStream is=s.getInputStream(); InputStreamReader isr=new InputStreamReader(is); BufferedReader br=new BufferedReader(isr); String info=null; while((info=br.readLine())!=null) { System.out.println("我是服务端,客户端说:"+info); } s.shutdownInput(); OutputStream out=s.getOutputStream(); PrintWriter pw=new PrintWriter(out); pw.write("欢迎您"); pw.flush(); s.shutdownOutput(); //关闭资源 pw.close(); out.close(); br.close(); isr.close(); is.close(); s.close(); ss.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 客户端: package zx_Socket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; public class TcpClient3 { public static void main(String[] args) { try { Socket s=new Socket("localhost",8888); OutputStream out=s.getOutputStream(); PrintWriter pw=new PrintWriter(out); pw.println("hello,我是admin"); pw.flush(); s.shutdownOutput(); InputStream is=s.getInputStream(); InputStreamReader isr=new InputStreamReader(is); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String info=null; while((info=br.readLine())!=null) { System.out.println(info); } s.shutdownInput(); br.close(); isr.close(); is.close(); pw.close(); out.close(); s.close(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
举报