服务器代码
public class Server {
public static void main(String[] args) throws IOException{
ServerSocket ss = new ServerSocket(14521);
Socket s = ss.accept();
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
String temp = null;
while((temp = br.readLine())!=null)
{
pw.println(temp);
}
pw.close();
br.close();
ss.close();
s.close();
}
}
上面的代码是没有问题的,可是把打印流换成高效字符输出流就不能正常运行了 为什么
高效字符输出流代码:
public class Server {
public static void main(String[] args) throws IOException{
ServerSocket ss = new ServerSocket(14521);
Socket s = ss.accept();
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String temp = null;
while((temp = br.readLine())!=null)
{
bw.write(temp);
bw.flush();
bw.newLine();
}
bw.close();
br.close();
ss.close();
s.close();
}
}
客户端代码
public class client {
public static void main(String[] args) throws IOException{
Socket s = new Socket("192.168.0.110",14521);
BufferedWriter os =new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
Scanner sc = new Scanner(System.in);
String temp = null;
while(!((temp= sc.nextLine()).equals("end")))
{
os.write(temp);
os.newLine();
os.flush();
System.out.println(br.readLine());
}
sc.close();
os.close();
}
}
|
|