客户端
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client1
{ public static void main(String args[])
{ 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();
setLayout(new FlowLayout()) ;
Box box=Box.createVerticalBox();
connection=new Button("connect the server");
send=new Button ("send");
send.setEnabled(false) ;
inputText=new TextField(20);
showResult=new TextField(20);
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(30,50,300,400);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==connection)
{ try
{if(socket.isConnected())
{}
else
{ InetAddress address=InetAddress.getByName("172.16.8.20");
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 (IOException ee){}
}
if(e.getSource()==send)
{ String s=inputText.getText();
if(s!=null)
{ try { out.writeUTF(s);
}
catch(IOException e1){}
}
}
}
public void run()
{ String s=null;
while(true)
{ try{ s=in.readUTF();
showResult.setText(s);
}
catch(IOException e)
{ showResult.setText("与服务器已断开");
break;
}
}
}
}
服务器端
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{ public static void main(String args[])
{ ServerSocket server=null;
Server_thread thread;
Socket you=null;
while (true)
{ try{ server=new ServerSocket(4331);
}
catch(IOException el)
{ System.out.println("正在监听");
}
try{ System.out.println("等待客戸呼叫");
you=server.accept();
System.out.println("客戸的地址:"+you.getInetAddress());
}
catch(IOException e)
{ System.out.println("正在等待客戸");
}
if(you!=null)
{ new Server_thread(you).start();
}
}
}
}
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)
{}
}
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();
try{ a[i]=Double.valueOf(temp).doubleValue();i++;}
catch(NumberFormatException e)
{ out.writeUTF("请输入数字字符");
quesion=true;
}
}
if (quesion==false)
{ double p=3.14*(a[0])*(a[0]);
out.writeUTF(" "+(p));
}
}
catch (IOException e)
{ System.out.println("客户离开");
return;
}
}
}
}