教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 高等教育 >

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

来源:网络收集 时间:2026-08-31
导读: InetAddress localHost = InetAddress.getLocalHost(); String ipAdd = localHost.getHostAddress(); textField1.setText(ipAdd); }catch(Exception ex){ textField1.setText(\ } } public static void main(String

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(\异常:\ } } }

…… 此处隐藏:373字,全部文档内容请下载后查看。喜欢就下载吧 ……
Java程序设计实用教程习题答案(7).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/613993.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)