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)在线全文阅读。
相关推荐: