求助高手,一道难题,谢谢,急,后天考试
编写一个客户/服务器程序qmj,服务器端的功能是计算圆的面积。客户端将圆的半径发送给服务器端,服务器端计算得出的圆面积将发送给客户端,并在客户端显示。
我写的程序,好像不能把数据,送到服务器,哪里错了,求教了,谢谢
客户端部分
import java.io.*;import java.net.*;
public class client {
public static void main(String args[]){
Socket mySocket;BufferedReader br;
DataInputStream in=null;
DataOutputStream out=null;
String s=null;
try{mySocket=new Socket("127.0.0.1",4555);
in=new DataInputStream(mySocket.getInputStream());
out=new DataOutputStream(mySocket.getOutputStream());
br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入半径:");
String s1=null;
s1=in.readUTF();
System.out.println("面积="+s1);
while(true){
s=br.readLine();
System.out.println("你输入的半径为:");
out.writeUTF(s);}
}
catch(IOException e){System.out.println("无法连接");}
}}
服务器端部分
import java.io.*;
import java.net.*;
public class sever {
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);}
try{ you=server.accept();
in=new DataInputStream(you.getInputStream());
out=new DataOutputStream(you.getOutputStream());
while(true){
s2=in.readUTF();
if(s2.equals("/r"))//如果等于回车/r,请问我这样判断字符串输入结束对吗。
break;
r=Integer.parseInt(s2);
String s3=String.valueOf(3.14*r*r);
out.writeUTF(s3);
System.out.println("面积="+s3);
}}catch(IOException e){
System.out.println("error"+e);}
}
}
[解决办法]
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);}}}}