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());
//獲取數(shù)據(jù)輸出流連接到socket上晋涣,向服務(wù)器端發(fā)送信息
DataInputStream dis=newDataInputStream(socket.getInputStream());
//獲取數(shù)據(jù)輸入流連接到socket上仪媒,并讀取服務(wù)器端的響應(yīng)信息
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{//發(fā)送信息的線程
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寫(xiě)數(shù)據(jù)
}
}catch(IOException e) {
e.printStackTrace();
}
}
publicString area(String r0) {
doubler =0;
r = Double.parseDouble(r0);
if(r <0)
return"輸入數(shù)據(jù)不能小于0";
doubleS = r*r*Math.PI;
returnDouble.toString(S);
}
}