import java.io.*;
import java.net.*;
class Client
{
public static void main(String []args)throws Exception{
//建立连接
Socket s = new Socket("172.40.84.99", 10006);
//定义键盘录入对象
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//定义网络流读对象
BufferedReader brIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
//定义网络流写对象
BufferedWriter bwOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String str = null;
while((str = br.readLine()) != null){
bwOut.write(str);
bwOut.newLine(); //加换行(在服务端用于结束标记)
bwOut.flush(); //刷新缓存区数据
//读取从服务器端发送过来的数据
System.out.println(brIn.readLine());
//结束标记, 当输入为over时结束
if(str.equals("over")){
break;
}
}
br.close();
s.close();
}
}
class Server
{
public static void main(String args[])throws Exception{
ServerSocket ss = new ServerSocket(10006);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress() + " : 连接已建立");
BufferedReader brIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bwOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String str = null;
while((str = brIn.readLine()) != null){
//System.out.println(str);
//将字符串进行反转
StringBuilder sb = new StringBuilder(str);
str = sb.reverse();
bwOut.write(str.toUpperCase());
bwOut.newLine();
bwOut.flush();
}
s.close();
ss.close();
}
} |