本帖最后由 西门吹风 于 2014-7-6 12:51 编辑
下面是毕老师源码,为什么把读取改成注释中的循环读取就会挂
- class TcpClient2
- {
- public static void main(String[] args)throws Exception
- {
- Socket s = new Socket("192.168.1.101",10004);
-
- OutputStream out = s.getOutputStream();
- out.write("服务端,你好".getBytes());
-
- InputStream in = s.getInputStream();
- byte[] buf = new byte[1024];
- int len = in.read(buf);
- System.out.println(new String(buf,0,len));
- //这里改成循环读取就会挂
- /*
- int len=0;
- while((len=in.read(buf))!=-1)
- {
- System.out.println(new String(buf,0,len));
- }
- */
- s.close();
- }
- }
- class TcpServer2
- {
- public static void main(String[] args) throws Exception
- {
- ServerSocket ss = new ServerSocket(10004);
- Socket s = ss.accept();
- String ip = s.getInetAddress().getHostAddress();
- System.out.println(ip+"....connected");
- InputStream in = s.getInputStream();
- byte[] buf = new byte[1024];
-
- int len = in.read(buf);
- System.out.println(new String(buf,0,len));
- //这里改成循环读取就会挂
- /*
- int len=0;
- while((len=in.read(buf))!=-1)
- {
- System.out.println(new String(buf,0,len));
- }
- */
- OutputStream out = s.getOutputStream();
- //Thread.sleep(10000);
- out.write("哥们收到,你也好".getBytes());
- s.close();
- ss.close();
- }
- }
复制代码
|
|