ImageVerifierCode 换一换
你正在下载:

JAVA22.docx

[预览]
格式:DOCX , 页数:18 ,大小:19.27KB ,
资源ID:4036975      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/4036975.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(JAVA22.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

JAVA22.docx

1、JAVA22第二十二课 网络编程案例:2201 URL应用举例案例文件:URLReader.java目 标:使用URL类,体会网络网络数据传输代 码:/ * 范例名称:URL应用举例 * 源文件名称:URLReader.java * 要 点: * 1. URL概念及URL类的使用 * 2. URL类的openStream()及其它方法 */import java.io.*;import .*;public class URLReader public static void main(String args) try URL tirc = new URL( BufferedReader in

2、= 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); 案例:2202 简单的client/server程序案例文件:TestServer.java/TestClient.java目

3、标:掌握Java Socket编程机制及其实现代 码:/ * 范例名称:简单的client/server程序 * 源文件名称:TestClient.java/TestServer.java * 要 点: * 1. Java Socket编程步骤 * 2. Socket/ServerSocket类用法 * 3. 通过Socket对象可以获取通信对方Socket的信息 */文件TestServer.javaimport .*;import java.io.*;public class TestServer public static void main(String args) try Serve

4、rSocket s = new ServerSocket(8888); while (true) Socket s1 = s.accept(); OutputStream os = s1.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); dos.writeUTF(Hello, + s1.getInetAddress() + port# +s1.getPort() + bye-bye!); dos.close(); s1.close(); catch (IOException e) System.out.pri

5、ntln(程序运行出错: + e); /文件TestClient.javaimport .*;import java.io.*;public class TestClient public static void main(String args) try Socket s1 = new Socket(127.0.0.1, 8888); InputStream is = s1.getInputStream(); DataInputStream dis = new DataInputStream(is); System.out.println(dis.readUTF(); dis.close()

6、; s1.close(); catch (ConnectException connExc) System.err.println(服务器连接失败!); catch (IOException e) 案例:2203 URL提取及应用举例案例文件:URLParser.java目 标:在Java应用程序中实现网络数据传输和处理代 码:/ * 范例名称:URL提取及应用举例 * 源文件名称:URLParser.java * 描 述:通过一个URL入口,读出该页上的所有超连接 * 要 点: * 1. URL/URLConnection类及相关方法的使用 * 2. Collection/ArrayList

7、/Iterator/StringTokenizer类 * 3. 注释的使用 * 4. 系统属性的使用 * 用 法:java URLParser * 如果使用了代理服务器,则添加相应的系统属性: * java -Dhttp.proxyHost= -Dhttp.proxyPort= URLParser * author bily * version v1.0 */ import java.io.IOException;import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Collection;

8、import java.util.ArrayList;import java.util.Iterator;import java.util.StringTokenizer; import .URL;import .URLConnection;import .MalformedURLException;public class URLParser /判断一段字串中是否含有子串http(超连接) private boolean hasMatch(String token) return token.indexOf(http:)!=-1; /* *从含有http子串的字符串中提取完整的URL */

9、private String trimURL(String url1) String tempStr=null; int beginIndex=url1.indexOf(http); int endIndex=url1.length(); tempStr=url1.substring(beginIndex,endIndex); endIndex=tempStr.indexOf(); if(endIndex=-1) endIndex=tempStr.length(); return tempStr.substring(0,endIndex); /* *遍历输入的页面里的内容,以找出url *pa

10、ram urlString 输入的URL *return urls ArrayList对象,其中包含已提取出的全部URL信息 */ public Collection searchURL(String urlString) URL url2=null; URLConnection conn=null; String nextLine=null; StringTokenizer tokenizer=null; Collection urlCollection=new ArrayList(); try url2=new URL(urlString); conn=url2.openConnectio

11、n(); conn.setDoOutput(true); conn.connect(); BufferedReader Reader1=new BufferedReader(new InputStreamReader(conn.getInputStream(); while(nextLine=Reader1.readLine()!=null) tokenizer=new StringTokenizer(nextLine); while(tokenizer.hasMoreTokens() String urlToken=tokenizer.nextToken(); if (hasMatch(ur

12、lToken) urlCollection.add(trimURL(urlToken); catch(MalformedURLException ex) ex.printStackTrace(); catch(IOException ex) ex.printStackTrace(); return urlCollection; /* *主方法,需要一个命令行参数-要处理的URL */ public static void main(String args) if(args.length!=1) System.out.println(用法: java URLParser ); System.ex

13、it(-1); String url3=args0; System.out.println(Searching web site:+url3); URLParser example=new URLParser(); Collection urlCollection=example.searchURL(url3); Iterator iter=urlCollection.iterator(); while (iter.hasNext() System.out.println(iter.next(); 案例:2204 聊天室程序案例文件:CreateTemp.java目 标:ChatServer.

14、java/ChatClient.java代 码:/ * 范例名称:聊天室程序 * 源文件名称:ChatClient.java/ChatServer.java * 要 点: * 1. Java Socket编程 * 2. 多线程编程 * 3. 程序体系结构及设计思想 */源程序ChatServer.javaimport java.io.*;import .*;import java.awt.*;import java.awt.event.*;import java.util.Vector;public class ChatServer extends Frame implements Windo

15、wListener TextArea display; int num=1; int port; ServerSocket server; BufferedReader input; BufferedWriter output; Vector msgV; /* *构造方法完成初始化工作 */ public ChatServer(int port1) super(Chat Server,Running.); display=new TextArea(200,300); this.add(Center,display); addWindowListener(this); setSize(300,4

16、00); show(); this.port=port1; msgV=new Vector(10); /* *关闭窗口事件 *1.显示正在关闭chat server *2.在消息队列msgV中加入shutdown字符串 *3.线程睡眠5001ms */ public void windowClosing(WindowEvent e) display.append(Server is now shutting down.n); display.append(Informing the Chatters.n); display.append(Please Wait.); msgV.add(shut

17、down); tryThread.sleep(5000); catch(Exception e2) dispose(); System.exit(0); public void windowOpened(WindowEvent e) public void windowActivated(WindowEvent e) public void windowClosed(WindowEvent e) public void windowDeactivated(WindowEvent e) public void windowDeiconified(WindowEvent e) public voi

18、d windowIconified(WindowEvent e) public void run() try server=new ServerSocket(port,3); display.append(Chat Server is now running!n); while(true) Socket clientSocket=server.accept(); clientConnection conn=new clientConnection(clientSocket,num,display,msgV); display.append(No +(num+)+ chatmaten); cat

19、ch(IOException e) e.printStackTrace(); public static void main(String args) ChatServer chatServer=new ChatServer(7); chatServer.run(); class clientConnection extends Thread String name; Socket cSocket; BufferedReader input; PrintWriter output; int num; TextArea display; Vector msgV; public clientCon

20、nection(Socket cSocket,int num,TextArea display,Vector msgV) try input = new BufferedReader(new InputStreamReader(cSocket.getInputStream(); output = new PrintWriter(cSocket.getOutputStream(),true); this.name=input.readLine(); catch(IOException e) e.printStackTrace(); this.cSocket = cSocket; this.num

21、=num; this.display=display; this.msgV=msgV; this.start(); synchronized public void run() String line; try while(true) line = input.readLine(); if(line.equals(End) display.append( System Message: +name+ is out !n); break; if(line.equals(PleaseSendALL) if(!msgV.isEmpty() int ii=msgV.size() ; output.pr

22、intln(ii); for(int I=0;ImsgV.size();I+) output.println(msgV.get(I); /output.println(end); else output.println(0); else line=name+ say:+ line+n; display.append(line); msgV.add(line); catch(IOException ex) ex.printStackTrace(); finally try cSocket.close(); catch(IOException ex2) ex2.printStackTrace();

23、 /源程序ChatServer.javaimport java.io.*;import .*;import java.awt.*;import java.awt.event.*;public class ChatClient extends Frame implements Runnable,WindowListener,ActionListener int num=1; boolean flag=true; Thread receiveThread; Socket sClient; BufferedReader input; PrintWriter output; String name;

24、Label helloLabel=new Label(Please input :); TextField sendText=new TextField(20); Button sendButton=new Button(Send); TextArea messageText=new TextArea(100,20); public ChatClient(String name,String servername) super(I am +name); try sClient = new Socket(servername,7); input =new BufferedReader(new I

25、nputStreamReader(sClient.getInputStream(); output = new PrintWriter(sClient.getOutputStream(),true); output.println(name); catch(IOException e) e.printStackTrace(); setBackground(new Color(220,220,255); sendText.addActionListener(this); sendButton.addActionListener(this); sendButton.setSize(20,30);

26、Panel cp=new Panel(); cp.add(helloLabel); cp.add(sendText); cp.add(sendButton); Panel sp=new Panel(); sp.add(messageText); add(North,cp); add(Center,messageText); addWindowListener(this); setSize(400,400); show(); receiveThread=new Thread(this); receiveThread.start(); public void windowActivated(WindowEvent e) public void windowClosed(WindowEvent e) public void windowClosing(WindowEvent e) flag=false; tryThread.sleep(5000); catch(Exception e2) output.println(End); dispose(); System.exit(0); public void windowDeactivated(WindowEvent e) public void windowDeico

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1