77范文网 - 专业文章范例文档资料分享平台

Java程序设计实用教程习题答案(5)

来源:网络收集 时间:2018-12-21 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

URL tirc=new URL(\ BufferedReader in=

new BufferedReader(new InputStreamReader(tirc.openStream()));

String s;

while((s=in.readLine())!=null) System.out.println(s); in.close();

}catch(MalformedURLException e){ System.out.println(e); }

catch(IOException e){ System.out.println(e); }

} }

2.编写Applet,访问并显示或播放指定URL地址处的图像和声音资源。 答案略

3.编写程序,程序的界面如图11-8所示,单击获取按钮,在下面的文本框中将显示本机的IP地址。

图11-8

参考代码如下: import java.awt.*;

import java.awt.event.*;

import java.net.*;

public class getIpAddress extends Frame implements ActionListener{ Button button1 = new Button(); Label label1 = new Label();

TextField textField1 = new TextField(); public getIpAddress(){

this.setResizable(true);

this.setSize(new Dimension(281, 168)); this.setTitle(\得到本机IP地址\button1.setLabel(\获取\

button1.addActionListener(this);

label1.setFont(new java.awt.Font(\ label1.setText(\本机的IP地址是:\ textField1.setColumns(15); textField1.setEditable(false);

textField1.setFont(new java.awt.Font(\ this.setLayout(new FlowLayout()); add(button1); add(label1); add(textField1); }

public void actionPerformed(ActionEvent e){ try{

InetAddress localHost = InetAddress.getLocalHost(); String ipAdd = localHost.getHostAddress(); textField1.setText(ipAdd); }catch(Exception ex){

textField1.setText(\ } }

public static void main(String[] args) { getIpAddress frm=new getIpAddress(); frm.setVisible(true); }

}

4.采用套接字的连接方式编写一个程序,允许客户向服务器提出一个文件的名字。如果文件存在就把文件的内容发送回客户,否则指出文件不存在。

假设文本文件score.txt的文件内容如下:

VB Java C++ 90 87 70 91 84 88

92 81 90

服务器端的运行结果如图11-9所示。

图11-9

客户端的运行结果如图11-10所示。

图11-10

程序参考代码如下:

? 服务器端程序

import java.net.*; import java.io.*;

public class SocketServer {

public static final int port=8000;

public static void main(String[] args) { String str;

try{

ServerSocket server=new ServerSocket(port); System.out.println(\ Socket socket=server.accept();

System.out.println(\ InputStream fln=socket.getInputStream();

OutputStream fOut=socket.getOutputStream();

InputStreamReader isr=new InputStreamReader(fln); BufferedReader in=new BufferedReader(isr); PrintStream out=new PrintStream(fOut); File sourceFile;

BufferedReader source;

System.out.println(\等待客户端的消息...\ str=in.readLine();

System.out.println(\客户端:\ sourceFile=new File(str);

System.out.println(\等待客户发送:\ try{

source=new BufferedReader(new FileReader(sourceFile)); while((str=source.readLine())!=null) {out.println(str);}

out.println(\

}catch(FileNotFoundException e) {

System.out.println(\文件不存在:\ out.println(\ }

socket.close(); server.close();

}catch(Exception e){

System.out.println(\异常:\ } } } ?

客户端程序

import java.net.*; import java.io.*;

public class ServerClient {

public static void main(String[] args) { String str; try{

InetAddress addr=InetAddress.getByName(\ Socket socket=new Socket(addr,8000); System.out.println(\

InputStream fIn=socket.getInputStream();

OutputStream fOut=socket.getOutputStream(); InputStreamReader isr=new InputStreamReader(fIn); BufferedReader in=new BufferedReader(isr); PrintStream out=new PrintStream(fOut);

InputStreamReader userisr=new InputStreamReader(System.in); BufferedReader userin=new BufferedReader(userisr); System.out.print(\发送字符串:\ str=userin.readLine();

out.println(str);

System.out.println(\等待获取服务器字符串\ if(str.equalsIgnoreCase(\ {

System.out.println(str);

throw new FileNotFoundException(\文件不存在异常!\ }

while(true){ str=in.readLine(); if(str.equals(\ System.out.println(str); }

socket.close(); }

catch(Exception e){

System.out.println(\异常:\ } } }

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Java程序设计实用教程习题答案(5)在线全文阅读。

Java程序设计实用教程习题答案(5).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/381601.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: