JAVA实验-综合应用实验(2)

2025-10-15

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();


JAVA实验-综合应用实验(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:前期物业服务合同 贵阳住房和城乡建设局

相关阅读
本类排行
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 7

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219