package itat.s11_6;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;
public class Server {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ServerSocket server=null;
Server_thread thread;
Socket you=null;
while(true){
try {
server=new ServerSocket(4331);
System.out.println("等待客户呼叫");
you=server.accept();
System.out.println("客户的地址:"+you.getInetAddress());
if(you!=null){
new Server_thread(you).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("正在监听");
}
}
}
}
class Server_thread extends Thread{
Socket socket;
DataOutputStream out=null;
DataInputStream in=null;
String s=null;
boolean quesion=false;
Server_thread(Socket t){
socket=t;
try {
out=new DataOutputStream(socket.getOutputStream());
in=new DataInputStream(socket.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
while(true){
double a[]=new double[3];
int i=0;
try {
s=in.readUTF();
quesion=false;
StringTokenizer fenxi=new StringTokenizer(s," ,");
while(fenxi.hasMoreTokens()){
String temp=fenxi.nextToken();
a[i]=Double.valueOf(temp).doubleValue();
i++;
}
if(quesion==false){
double p=(a[0]+a[1]+a[2])/2.0;
out.writeUTF(""+Math.sqrt(p*(p-a[0])*(p-a[1])*(p-a[2])));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package itat.s11_6;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.Box;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ComputerClient();
}
}
class ComputerClient extends Frame implements Runnable,ActionListener{
Button connection,send;
TextField inputText,showResult;
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread thread;
ComputerClient(){
socket =new Socket();
this.setLayout(new FlowLayout());
Box box=Box.createVerticalBox();
connection=new Button("连接服务器");
send=new Button("发送");
send.setEnabled(false);
inputText=new TextField(12);
showResult=new TextField(12);
box.add(connection);
box.add(new Label("输入三角形的长度,用逗号或空格分隔"));
box.add(inputText);
box.add(send);
box.add(new Label("收到的结果"));
box.add(showResult);
connection.addActionListener(this);
send.addActionListener(this);
thread=new Thread(this);
add(box);
setBounds(10,30,300,400);
setVisible(true);
validate();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void run() {
// TODO Auto-generated method stub
String s=null;
while(true){
try {
s=in.readUTF();
showResult.setText(s);
} catch (IOException e) {
// TODO Auto-generated catch block
showResult.setText("与服务器已断开");
break;
}
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==connection){
if(socket.isConnected()){}else{
try {
InetAddress address=InetAddress.getByName("127.0.0.1");
InetSocketAddress socketAddress=new InetSocketAddress(address,4331);
socket.connect(socketAddress);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
send.setEnabled(true);
thread.start();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
if(e.getSource()==send){
String s=inputText.getText();
if(s!=null){
try {
out.writeUTF(s);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}