本帖最后由 梁航斌 于 2013-4-7 21:28 编辑
这是一个客户端发送小写到服务端,然后服务端转大写再返回服务端,但在结束时客户端发over的时候,客户端没事,服务端出了问题,这个什么原因?怎么解决?- import java.net.*;
- import java.io.*;
- //客户端
- class Client
- {
- public static void main(String []args) throws Exception{
- Socket s = new Socket(InetAddress.getByName("192.168.1.4"),10000);
- //控制台输入
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- //获取输入输出流
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
- BufferedReader sbr = new BufferedReader(new InputStreamReader(s.getInputStream()));
- //发送和接收
- String sline = null;
- String line = null;
- while((line = br.readLine())!=null){
- bw.write(line);
- bw.newLine();
- bw.flush();
- sline = sbr.readLine();
- System.out.println(sline);
- if(line.equals("over"))
- break;
- }
- }
- }
复制代码- import java.net.*;
- import java.io.*;
- //服务端
- class Server
- {
- public static void main(String []args) throws Exception{
-
- ServerSocket ss = new ServerSocket(10000);
- Socket s = ss.accept();
- //连接反馈
- String ip = s.getInetAddress().getHostAddress();
- System.out.println(ip+"....连接成功");
- //获取客户端的输入输出流
- BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
- //接收和发送
- String line = null;
- while((line = br.readLine())!=null){
- System.out.println(line);
- bw.write("server: "+line.toUpperCase());
- bw.newLine();
- bw.flush();
- }
- s.close();
- ss.close();
- }
- }
复制代码 |