




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、.java项目实训一、将服务器上的资源读到本机1、从键盘输入网址,将该网址的内容读入到本机并输出import .*;import java.io.*;import java.util.*;public class lx1 public static void main(string args) scanner scanner; url url; system.out.println(输入url资源,例如:); scanner = new scanner(system.in); string source = scanner.nextline
2、(); try url = new url(source); inputstream in = url.openstream(); byte b = new byte1024; int n=-1; while(n=in.read(b)!=-1) string str = new string(b,0,n); system.out.print(str); catch(exception exp) system.out.println(exp); 2、从键盘输入网址,将该网址的内容读入到本机并输出到一个文件urltext中。import .*;import java.io.*;im
3、port java.io.*;import java.util.*;public class lx2 public static void main(string args) scanner scanner; url url; system.out.println(输入url资源,例如:); scanner = new scanner(system.in); string source = scanner.nextline(); try url = new url(source);精品. inputstream in = url.openstream();
4、 byte b = new byte1024; int n=-1; fileoutputstream out=new fileoutputstream(urltext.txt); while(n=in.read(b)!=-1) string str = new string(b,0,n); out.write(b,0,n); system.out.print(str); catch(exception exp) system.out.println(exp); 3、用线程技术实现上述程序import .*;import java.io.*;import java.util.*;
5、public class example13_1 public static void main(string args) scanner scanner; url url; thread readurl; look look = new look(); system.out.println(输入url资源,例如:); scanner = new scanner(system.in); string source = scanner.nextline(); try url = new url(source); look.seturl(url); readu
6、rl = new thread(look); catch(exception exp) system.out.println(exp); readurl = new thread(look); readurl.start(); import .*;import java.io.*;精品.public class look implements runnable url url; public void seturl(url url) this.url=url; public void run() try inputstream in = url.openstream(); by
7、te b = new byte1024; int n=-1; while(n=in.read(b)!=-1) string str = new string(b,0,n); system.out.print(str); catch(ioexception exp) 二、读取主机地址和本机地址1、调试下列读取主机名称及ip地址的程序import .*; public class example13_2 public static void main(string args) try inetaddress address_1=inetaddress.getbyname(www.s
8、); system.out.println(address_1.tostring(); inetaddress address_2=inetaddress.getbyname(); system.out.println(address_2.tostring(); catch(unknownhostexception e) system.out.println(无法找到 ); 2、从键盘输入任意主机名,读取主机地址、本机地址import .*;import java.io.*;import java.io
9、.*;import java.util.*;public class ex2 public static void main(string args) scanner scanner; url url;精品. system.out.println(输入url资源,例如:); scanner = new scanner(system.in); string source = scanner.nextline(); try inetaddress address_1=inetaddress.getbyname(source); system.out.println(add
10、ress_1.tostring(); inetaddress address_2=inetaddress.getlocalhost(); system.out.println(address_2.tostring(); catch(unknownhostexception e) system.out.println(无法找到 ); 三、套接字应用1、编程实现客户端与服务端之间的应答通讯服务端代码:import java.io.*;import .*;public class server public static void main(string
11、 args) string answer =南非,进入世界杯了,哈哈.问题真逗!; serversocket serverforclient=null; socket socketonserver=null; dataoutputstream out=null; datainputstream in=null; try serverforclient = new serversocket(2010); catch(ioexception e1) system.out.println(e1); try system.out.println(等待客户呼叫); socketonserver = se
12、rverforclient.accept(); /堵塞状态,除非有客户呼叫 out=new dataoutputstream(socketonserver.getoutputstream(); in=new datainputstream(socketonserver.getinputstream(); for(int i=0;ianswer.length;i+) string s=in.readutf(); / in读取信息,堵塞状态 system.out.println(服务器收到客户的提问:+s); out.writeutf(answeri); thread.sleep(500); ca
13、tch(exception e) 精品. system.out.println(客户已断开+e); 服务端代码import java.io.*;import .*;public class server public static void main(string args) string answer =南非,进入世界杯了,哈哈.问题真逗!; serversocket serverforclient=null; socket socketonserver=null; dataoutputstream out=null; datainputstream in=null; try
14、 serverforclient = new serversocket(2010); catch(ioexception e1) system.out.println(e1); try system.out.println(等待客户呼叫); socketonserver = serverforclient.accept(); /堵塞状态,除非有客户呼叫 out=new dataoutputstream(socketonserver.getoutputstream(); in=new datainputstream(socketonserver.getinputstream(); for(int
15、 i=0;ianswer.length;i+) string s=in.readutf(); / in读取信息,堵塞状态 system.out.println(服务器收到客户的提问:+s); out.writeutf(answeri); thread.sleep(500); catch(exception e) system.out.println(客户已断开+e); 2、通过键盘输入问答方式,实现客户与服务器之间的对话,任何一端回答“bye”时,结束。服务器端程序代码:import java.io.*;import .*;import java.util.*;public c
16、lass server1 精品. public static void main(string args) string answer; serversocket serverforclient=null; socket socketonserver=null; dataoutputstream out=null; datainputstream in=null; scanner scanner= new scanner(system.in); try serverforclient = new serversocket(2010); catch(ioexception e1) system.
17、out.println(e1); try system.out.println(等待客户呼叫); socketonserver = serverforclient.accept(); /堵塞状态,除非有客户呼叫 out=new dataoutputstream(socketonserver.getoutputstream(); in=new datainputstream(socketonserver.getinputstream(); while(true) string s=in.readutf(); / in读取信息,堵塞状态 system.out.println(服务器收到客户的发言:
18、+s); system.out.print(服务器发言:); answer = scanner.nextline(); if (answer.equals(bye) break; out.writeutf(answer); catch(exception e) system.out.println(客户已断开+e); 客户端代码:import .*;import java.io.*;import java.util.*;public class client1 public static void main(string args) scanner scanner= new s
19、canner(system.in); string mess; string s; socket mysocket; datainputstream in=null; dataoutputstream out=null;精品. try mysocket=new socket(,2010); in=new datainputstream(mysocket.getinputstream(); out=new dataoutputstream(mysocket.getoutputstream(); while (true) system.out.print(客户发言:); mess
20、 = scanner.nextline(); if (mess.equals(bye) break; out.writeutf(mess); s=in.readutf(); /in读取信息,堵塞状态 system.out.println(客户收到服务器的信息:+s); catch(exception e) system.out.println(服务器已断开+e); 3、服务器端代码不变,客房端按以下届面设计客户端的程序代码如下:public class client2 public static void main(string args) cc win=new cc(); win.setbo
21、unds(100,100,460,360); win.settitle(聊天程序客户端) 精品.import java.awt.event.*;import java.awt.*;import javax.swing.*;import .*;import java.io.*;import java.util.*;public class cc extends jframe jtextfield inputtext; jtextarea textshow; jbutton button; policelisten listener; public cc() init(); set
22、visible(true); setdefaultcloseoperation(jframe.exit_on_close); void init() socket mysocket; datainputstream in=null; dataoutputstream out=null; try mysocket=new socket(,2010); in=new datainputstream(mysocket.getinputstream(); out=new dataoutputstream(mysocket.getoutputstream(); setlayout(ne
23、w flowlayout(); textshow = new jtextarea(9,30); inputtext = new jtextfield(10); button = new jbutton(发送); listener = new policelisten(); listener.setjtextfield(inputtext); listener.setjtextarea(textshow); listener.setin(in); listener.setout(out); inputtext.addactionlistener(listener); button.addactionlistener(listener); add(new jscrollpane(textshow); add(inputtext); add(button); catch(exception e) system.out.println(服务
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030年中国稀土冶炼分离市场运行动态及发展前景分析报告
- 2025甘肃省安全员考试题库附答案
- 南京医科大学《课程论文写作与学术规范》2023-2024学年第二学期期末试卷
- 黔西南民族职业技术学院《外国建筑史》2023-2024学年第二学期期末试卷
- 青海交通职业技术学院《传感检测技术》2023-2024学年第二学期期末试卷
- 天津商业大学《学术论文选题与写作》2023-2024学年第二学期期末试卷
- 湖北大学《财务会计一》2023-2024学年第二学期期末试卷
- 2025上海市建筑安全员考试题库及答案
- 西藏大学《软件交互设计》2023-2024学年第二学期期末试卷
- 做账实操-驾校行业的账务处理分录
- 新版《医疗器械经营质量管理规范》(2024)培训试题及答案
- 2025年人教版数学五年级下册教学计划(含进度表)
- 2024年我国人口老龄化问题与对策
- 中心静脉压测量技术-中华护理学会团体标准2023
- 部编人教版二年级道德与法治下册同步练习(全册)
- 4.7 数学建模活动:生长规律的描述教学设计
- 手术风险及医疗意外险告知流程
- 综合实践活动六年级下册 饮料与健康课件 (共16张PPT)
- 数量金融的概况和历史课件
- 专业医院lovo常用文件产品介绍customer presentation
- 叉车日常使用状况点检记录表(日常检查记录)
评论
0/150
提交评论