packagework11;
importjava.io.*;
importjava.net.*;
public classclient {
staticSocketsocket;
public static voidmain(String[] args)throwsUnknownHostException,IOException{
socket=newSocket("127.0.0.1",20000);
DataOutputStream dos=newDataOutputStream(socket.getOutputStream());
//获取数据输出流连接到socket上,向服务器端发送信息
DataInputStream dis=newDataInputStream(socket.getInputStream());
//获取数据输入流连接到socket上,并读取服务器端的响应信息
newreciveMessage(dis).start();
newsendMessage(dos).start();
}
}
classreciveMessageextendsThread{//接受信息的线程
privateDataInputStreamdis;
publicreciveMessage(DataInputStream dis){
this.dis=dis;
}
public voidrun(){
while(true){
try{
System.out.println(dis.readUTF());
}catch(IOException e){
e.printStackTrace();
}
}
}
}
classsendMessageextendsThread{//发送信息的线程
privateDataOutputStreamdos;
publicsendMessage(DataOutputStream dos){
this.dos=dos;
}
public voidrun(){
String info;
InputStreamReader is=newInputStreamReader(System.in);
BufferedReader bf=newBufferedReader(is);
while(true){
try{
info=bf.readLine();
dos.writeUTF(info);
}catch(IOException e){
e.printStackTrace();
}
}
}
}
packagework11;
importjava.io.*;
importjava.net.*;
public classserver {
staticServerSocketss;
public static voidmain(String[] args) {
try{
ss=newServerSocket(20000);
while(true) {
Socket s =ss.accept();
newrelayMessage(s).start();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
classrelayMessageextendsThread {
DataInputStreamdis;
DataOutputStreamdos;
Stringstr;
publicrelayMessage(Socket s)throwsIOException {
// this.s=s;
dis=newDataInputStream(s.getInputStream());
dos=newDataOutputStream(s.getOutputStream());
}
public voidrun() {
try{
while(true) {
str=dis.readUTF();
String area0 = area(str);
dos.writeUTF(area0);// 向s.dos写数据
}
}catch(IOException e) {
e.printStackTrace();
}
}
publicString area(String r0) {
doubler =0;
r = Double.parseDouble(r0);
if(r <0)
return"输入数据不能小于0";
doubleS = r*r*Math.PI;
returnDouble.toString(S);
}
}