Java项目实训.pdf

上传人:l**** 文档编号:72836437 上传时间:2023-02-13 格式:PDF 页数:9 大小:246.08KB
返回 下载 相关 举报
Java项目实训.pdf_第1页
第1页 / 共9页
Java项目实训.pdf_第2页
第2页 / 共9页
点击查看更多>>
资源描述

《Java项目实训.pdf》由会员分享,可在线阅读,更多相关《Java项目实训.pdf(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

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

13、(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 i=0;ianswer.length;i+)String s=in.readUTF();/in读取信息,堵塞状态

14、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 class Server1 public static void main(String args)String answer;ServerSocke

15、t serverForClient=null;Socket socketOnServer=null;DataOutputStream out=null;DataInputStreamin=null;Scanner scanner=new Scanner(System.in);try serverForClient=new ServerSocket(2010);catch(IOException e1)System.out.println(e1);try System.out.println(等待客户呼叫);socketOnServer=serverForClient.accept();/堵塞状

16、态,除非有客户呼叫out=new DataOutputStream(socketOnServer.getOutputStream();in=new DataInputStream(socketOnServer.getInputStream();while(true)String s=in.readUTF();/in读取信息,堵塞状态System.out.println(服务器收到客户的发言:+s);System.out.print(服务器发言:);answer=scanner.nextLine();if(answer.equals(bye)break;out.writeUTF(answer);

17、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 Scanner(System.in);String mess;Strings;Socket mysocket;DataInputStream in=null;DataOutputStream out=null;trymysocket=new Sock

18、et(127.0.0.1,2010);in=new DataInputStream(mysocket.getInputStream();out=new DataOutputStream(mysocket.getOutputStream();while(true)System.out.print(客户发言:);mess=scanner.nextLine();if(mess.equals(bye)break;out.writeUTF(mess);s=in.readUTF();/in 读取信息,堵塞状态System.out.println(客户收到服务器的信息:+s);catch(Exception

19、 e)System.out.println(服务器已断开+e);3、服务器端代码不变,客房端按以下届面设计客户端的程序代码如下:public class Client2 public static void main(String args)Cc win=new Cc();win.setBounds(100,100,460,360);win.setTitle(聊天程序客户端)import java.awt.event.*;import java.awt.*;import javax.swing.*;import .*;import java.io.*;import java.util.*;pu

20、blic class Cc extends JFrame JTextField inputText;JTextArea textShow;JButton button;PoliceListen listener;public Cc()init();setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);void init()Socket mysocket;DataInputStream in=null;DataOutputStream out=null;trymysocket=new Socket(127.0.0.1,20

21、10);in=new DataInputStream(mysocket.getInputStream();out=new DataOutputStream(mysocket.getOutputStream();setLayout(new FlowLayout();textShow=new JTextArea(9,30);inputText=new JTextField(10);button=new JButton(发送);listener=new PoliceListen();listener.setJTextField(inputText);listener.setJTextArea(tex

22、tShow);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(服务器已断开+e);System.exit(-1);import java.awt.event.*;import javax.swing.*;import .*;impor

23、t java.io.*;import java.util.*;public class PoliceListen implements ActionListener JTextField textInput;JTextArea textShow;DataInputStream in;DataOutputStream out;public void setJTextField(JTextField text)textInput=text;public void setJTextArea(JTextArea area)textShow=area;public void setIn(DataInpu

24、tStream inn)in=inn;public void setOut(DataOutputStream out1)out=out1;public void actionPerformed(ActionEvent e)String mess;Strings;trymess=textInput.getText();out.writeUTF(mess);s=in.readUTF();/in 读取信息,堵塞状态textShow.append(客户收到服务器的信息:+s+n);textInput.setText();catch(Exception e1)System.out.println(服务器已断开+e1);4、请参照客户端届面,设计服务器端程序

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 工作报告

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁