- 服务端:
- ServerSocket ss=new ServerSocket(40006);
- while(true)
- {
- Socket s=ss.accept();
- OutputStream os=s.getOutputStream();
- os.write("北京".getBytes());
- os.flush();//这里改成os.close( ),客户端就能读到,这是为什么啊?
- }
- 客户端:
- try {
- Socket s=new Socket("192.168.0.110",40006);
- InputStream ins=s.getInputStream();
- BufferedReader br=new BufferedReader(new InputStreamReader(ins));
- String line;
- while((line=br.readLine())!=null)
- {
- System.out.println(line);
- }
复制代码 |