import java.io.*;
import java.net.*;
public class SocketClient{
public static void main(String[] args){
Socket cs;
String data="";
String recvdata="";
try{
cs=new Socket("192.168.1.100",3000);
BufferedReader is=new BufferedReader(new InputStreamReader(System.in));
BufferedReader cis=new BufferedReader(new InputStreamReader(cs.getInputStream()));
PrintWriter cos=new PrintWriter(cs.getOutputStream());
System.out.println("请输入发送的字符串,输入OK求出面积并退出系统");
do{
data=is.readLine();
cos.println(data);
cos.flush();
System.out.println("您输入的数据是"+data);
recvdata=cis.readLine();
System.out.println(recvdata);
}while(!recvdata.equals("OK"));
is.close();
cis.close();
cos.close();
cs.close();
}catch(Exception e){System.out.println(e.toString());}
}
}
import java.io.*;
import java.net.*;
import java.util.*;
public class SocketServer{
public static void main(String[] args){
ServerSocket ss=null;
StringBuffer data=new StringBuffer();
String recv="";
float area[]=new float[4];
int i=1;float coA;double M=0;
try{
ss=new ServerSocket(3000);
}catch(Exception e){
System.out.println("不能监听到请求:"+e.toString());
}try{
Socket s=ss.accept();
System.out.println("连接成功,正在接收数据...");
BufferedReader sis=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter sos=new PrintWriter(s.getOutputStream());
recv=sis.readLine();
while(!recv.equals("OK")){
data.append("在时间"+Calendar.getInstance().getTime().toString());
data.append("接收到数据:"+recv);
area[i]=Float.parseFloat(recv);
i++;
System.out.println(data.toString());
sos.println("请输入三角形的第"+i+"边:");
sos.flush();
recv=sis.readLine();
data.setLength(0);
}
System.out.println("三角形的三边分别为:"+area[1]+", "+area[2]+", "+area[3]);
if(recv.equals("OK"))
{
coA=(area[2]*area[2]+area[3]*area[3]-area[1]*area[1])/(2*area[2]*area[3]);
M=area[2]*Math.sqrt(1.0-coA*coA)*area[3]/2.0;
}
System.out.print("求得三角形的面积为:");
System.out.println(M);
sos.print("求得三角形的面积为:");
sos.println(M);
sos.println("OK");
sos.close();
sis.close();
ss.close();
s.close();
}catch(Exception e){
System.out.println("Exception:"+e.toString());
}
}
}
这两个分别是Client和Server的程序,我运行的时候本来就要求的是输入三条条的,请问一下怎样可以删除
请输入三角形的第4边的字呢
----------------解决方案--------------------------------------------------------