connection.addActionListener(this); thread = new Thread(this); setBounds(100,100,360,310); setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
public void run() {
while(true) {
try{
double area=in.readDouble();
showResult.append(\三角形的面积:\\n\
showResult.setCaretPosition((showResult.getText()).length()); }
catch(IOException e) {
showResult.setText(\与服务器已断开\ computer.setEnabled(false); break; } } }
public void actionPerformed(ActionEvent e) {
if(e.getSource()==connection) {
try {
if(socket.isConnected()) { } else {
InetAddress address=InetAddress.getByName(\ InetSocketAddress socketAddress=new InetSocketAddress(address,4331);
socket.connect(socketAddress);
in =new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream()); computer.setEnabled(true); thread.start(); } }
catch (IOException ee){} }
if(e.getSource()==computer) {
try {
double a=Double.parseDouble(inputA.getText()), b=Double.parseDouble(inputB.getText()), c=Double.parseDouble(inputC.getText()); if(a+b>c&&a+c>b&&b+c>a) {
out.writeDouble(a); out.writeDouble(b); out.writeDouble(c); } else {
inputA.setText(\你输入的3个数不构成三角形\ } }
catch(Exception ee) {
inputA.setText(\请输入数字字符\ } } }
public static void main(String args[]) {
Client win=new Client(); } }
(2)服务器端程序
import java.io.*; import java.net.*; import java.util.*; public class Server {
public static void main(String args[]) {
ServerSocket server=null; ServerThread thread; Socket you=null; while(true) {
try{
server=new ServerSocket(4331); }
catch(IOException e1)
{
System.out.println(\正在监听\ } try{
you=server.accept();
System.out.println(\客户的地址:\ }
catch (IOException e) {
System.out.println(\正在等待客户\ }
if(you!=null) {
new ServerThread(you).start(); } else {
continue; } } } }
class ServerThread extends Thread {
Socket socket;
DataOutputStream out=null; DataInputStream in=null; String s=null;
ServerThread(Socket t) {
socket=t; try {
in=new DataInputStream(socket.getInputStream()); out=new DataOutputStream(socket.getOutputStream()); }
catch (IOException e) {} }
public void run() {
while(true) {
double a=0,b=0,c=0,area=0; try{
a=in.readDouble(); b=in.readDouble();
c=in.readDouble(); double p=(a+b+c)/2.0;
area=Math.sqrt(p*(p-a)*(p-b)*(p-c)); out.writeDouble(area); }
catch (IOException e) {
System.out.println(\客户离开\ break; } } } }
运行结果: 服务器:
客户端:
实验十四 网络编程(2)
1.使用DatagramSocket编写一个时间服务器程序,它能够向客户程序发送以下格式的时间信息。
时间格式为:Sat Jan 15 10:45:20 CST 2005
算法提示:
打开一个数据报DatagramSocket; 开始循环:
创建客户程序请求数据报的缓冲区并等待请求;
当接收到请求时获取发送者(即客户程序)的IP地址和端口;
创建包含当前日期和时间信息,可向给定IP地址和端口发送的应答数据包; 发送上述应答数据包; 结束循环;
关闭UDPDaytimeServer并退出; (1)服务器端程序
import java.net.*; import java.io.*; import java.util.*;
public class UDPDaytimeServer{
public static void main(String[] args){ DatagramSocket timeSocket; Date currDate; byte[] inBuffer; byte[] outBuffer; DatagramPacket request; DatagramPacket reply; InetAddress clientAddress; int clientPort; if(args.length<1){
System.out.println(\ System.out.println(\ System.exit(-1); } try{
timeSocket=new DatagramSocket(Integer.parseInt(args[0])); System.out.println(\时间服务器已经启动,正在侦听断口\ try{
while(true){
inBuffer=new byte[1];
request=new DatagramPacket(inBuffer,inBuffer.length); // 等待客户程序的请求
timeSocket.receive(request); // 获得客户程序的地址和端口
clientAddress = request.getAddress(); clientPort = request.getPort();
System.out.println(\接受来自\ \的时间服务请求.\ // 创建并发送应答数据 currDate = new Date();