import java.io.*;
import java.net.*;
/*
练习:服务器接收客户端的字符,并转换为大写返回给客户端。
*/
public class DemoTransform
{
}
class Client
{
public static void main(String[] args)throws Exception
{
Socket s = new Socket(InetAddress.getLocalHost(),8080);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
bw.write(str);
bw.newLine();
bw.flush();
BufferedReader br2 = new BufferedReader(new InputStreamReader(s.getInputStream()));
String read = br2.readLine();
System.out.println(read);
s.close();
}
}
class Server
{
public static void main(String[] args)throws Exception
{
ServerSocket ss = new ServerSocket(8080);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = br.readLine();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( s.getOutputStream()));
str=str.toUpperCase();
bw.write(str);
// bw.newLine();
bw.flush();
s.close();
ss.close();
}
}
readLine()是阻塞式方法
客户端的bw要写回车符或者换行符我可以理解,服务器的bw不是也要写回车符和换行符吗?
为什么我没写,客户端的readLine()也能返回?
------解决思路----------------------
我的理解是.readLine() 怎么判断是一行..通过回车和换行符来判断..而实际上newLine()方法把分隔符当做String写了出去,
所以客户端等着收就是..收到了就是带着分隔符的,所以回来的时候就不用再写了.