本帖最后由 klas2008 于 2012-4-5 04:51 编辑
在毕老day23中TcpDemo2.java里,如果在原代码上加入31行和50行这两行while{}循环,会出现如下错误
客户端正常,服务端运行完毕后会出现Socket已经关闭。
主要想请问下大家程序是怎么运行的过程最后报出这样的错误呢?- import java.io.*;
- import java.net.*;
- class TcpClient2
- {
- public static void main(String[] args)throws Exception
- {
- Socket s = new Socket("192.168.1.254",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));
- s.close();
- }
- }
- class TcpServer2
- {
- public static void main(String[] args) throws Exception
- {
- ServerSocket ss = new ServerSocket(10004);
- while(true){
- 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));
-
-
- OutputStream out = s.getOutputStream();
-
-
- Thread.sleep(10000);
- out.write("哥们收到,你也好".getBytes());
- }
- s.close();
- ss.close();
- }
- }
复制代码 |
|