本帖最后由 Без_тебя 于 2014-6-24 18:11 编辑
原理:用io流从键盘中获取用户输入的英文句子,然后通过Socket输出流从客户端传给服务端的输入流,服务端通过接收到客户端发来的Socket对象,获取其中的Socket的输入流进行解析,然后转换成大写字母后,再传入Socket对象的输出流返回给客户端,这样客户端就能一直变写边接收转换成大写的数据了
输入over结束
package Demo12UpToLower;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("127.0.0.1",10000);
//接收键盘的信息
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
//发送个服务端的信息
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
//接收服务端发来的信息
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line = null;
while(!(line=read.readLine()).equals("over")){
out.newLine(); out.write(line);
out.flush();
String up = in.readLine();
System.out.println("服务器返回:"+up);
}
s.close();
}
}
package Demo12UpToLower;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server{
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10000);
//获取客户端发来的Socket对象
Socket s = ss.accept();
System.out.println("Ip:"+s.getInetAddress()+"connect");
//接收客服端发来的数据
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//Socket对象的输出流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line = null;
while(!(line=br.readLine()).equals("over")){
String up = line.toUpperCase();
System.out.println(up);
bw.write(up);
bw.newLine();
bw.flush();
}
s.close();
ss.close();
}
}
|
|