Java项目实训.pdf
《Java项目实训.pdf》由会员分享,可在线阅读,更多相关《Java项目实训.pdf(9页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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();try url=new URL(source);InputSt
2、ream 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.*;import java.io.*;import java.util.*;public class Lx2 public st
3、atic 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 byte1024;int n=-1;FileOutputStream out=new FileOutputStream(urltext.txt);while(n=in
4、.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 public static void main(String args)Scanner scanner;URL url;Thread readURL;Look look=new Lo
5、ok();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)System.out.println(exp);readURL=new Thread(look);readURL.start();import .*;import java.io.*;public class Look
6、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)String str=new String(b,0,n);System.out.print(str);catch(IOException exp)二、读取主机地址和本机地址1、调试下列读取主机名称及IP 地址的程序import .*;public cla
7、ss Example13_2 public static void main(String args)tryInetAddress address_1=InetAddress.getByName();System.out.println(address_1.toString();InetAddress address_2=InetAddress.getByName(166.111.222.3);System.out.println(address_2.toString();catch(UnknownHostException e)System.out.println(无法找到);2、从键盘输入
8、任意主机名,读取主机地址、本机地址import .*;import java.io.*;import java.io.*;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();tryInetAddress address_1=InetAddress.getByNa
9、me(source);System.out.println(address_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(St
10、ring args)String answer=南非,进入世界杯了,哈哈.问题真逗!;ServerSocket serverForClient=null;Socket socketOnServer=null;DataOutputStream out=null;DataInputStreamin=null;try serverForClient=new ServerSocket(2010);catch(IOException e1)System.out.println(e1);try System.out.println(等待客户呼叫);socketOnServer=serverForClien
11、t.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);catch(Exception e)System.
12、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;DataInputStreamin=null;try serverForClient=new ServerSocket(2010);catch
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 项目
限制150内