58,454
社区成员
发帖
与我相关
我的任务
分享
import java.io.*;import java.net.*;
public class Client {
public static void main(String args[]){
Socket mySocket;
PrintWriter pw;
BufferedReader br;
BufferedReader localReader;
InputStream in=null;
OutputStream out=null;
String s=null;
try{
mySocket=new Socket("127.0.0.1",4555);
br = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
pw = new PrintWriter(mySocket.getOutputStream(), true);
localReader =new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please type in radius: ");
while((s=localReader.readLine()) != null){
System.out.println("The radius you input is " + s);
pw.println(s);
System.out.println(br.readLine());
}
}
catch(IOException e){System.out.println("Cannot connect to Server!");}
}}
import java.io.*;
import java.net.*;
public class Server {
public static void main(String args[]){
ServerSocket server=null;
Socket you=null;String s2=null;int r;
DataInputStream in=null;
DataOutputStream out=null;
try{
server=new ServerSocket(4555);
}
catch(IOException e1)
{System.out.println("error"+e1);}
while (true) {
try{
you=server.accept();
System.out.println("New connection arrived: " + you.getPort());
BufferedReader br = new BufferedReader(new InputStreamReader(you.getInputStream()));
PrintWriter pw = new PrintWriter(you.getOutputStream(), true);
String msg = null;
while ((msg = br.readLine()) != null) {
System.out.println("msg = " + msg);
double d = Double.parseDouble(msg);
double area = 3.14 * d * d;
System.out.println("area is : " + area);
pw.println(area);
}
}catch(IOException e){
System.out.println("error"+e);}
}
}
}