Java项目实训.doc
《Java项目实训.doc》由会员分享,可在线阅读,更多相关《Java项目实训.doc(8页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、如有侵权,请联系网站删除,仅供学习与交流Java项目实训【精品文档】第 8 页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 = scann
2、er.nextLine(); 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.*;
3、import 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(); byte b = new byte10
4、24; 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.*;public class Example13_1 publ
5、ic 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); readURL = new Thread(look); catch(Exception exp) Syst
6、em.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(); byte b = new byte1024; int n=-1; while(n=in.read(b)!=-1) Strin
7、g 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(); System.out.println(address_1.toString(); InetAddress address_2=InetAddr
8、ess.getByName(166.111.222.3); System.out.println(address_2.toString(); catch(UnknownHostException e) System.out.println(无法找到 );2、从键盘输入任意主机名,读取主机地址、本机地址import .*;import java.io.*;import java.io.*;import java.util.*;public class Ex2 public static void main(String args) Scanner scanner; URL url; System
9、.out.println(输入URL资源,例如:); scanner = new Scanner(System.in); String source = scanner.nextLine(); try InetAddress address_1=InetAddress.getByName(source); System.out.println(address_1.toString(); InetAddress address_2=InetAddress.getLocalHost(); System.out.println(address_2.toString(); catch(UnknownH
10、ostException e) System.out.println(无法找到 );三、套接字应用1、编程实现客户端与服务端之间的应答通讯服务端代码: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
11、in=null; try 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.getInputStrea
12、m(); for(int 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);服务端代码import java.io.*;import .*;public class Server public static void main(String args) String answer
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 项目
限制150内