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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

实验八 网络编程基础.docx

1、实验八 网络编程基础实验八 网络编程基础1实验目的(1)掌握Socket通信。(2)掌握UDP通信2实验内容实验题1 使用InetAddress类的方法获取import .*;public class myIP public static void main(String args) try InetAddress address1=InetAddress.getByName(); System.out.println(address1.toString(); InetAddress address2=InetAddress.getByName(null); System.out.printl

2、n(address2.toString(); System.out.println(address2.getHostName(); catch (UnknownHostException e) System.out.println(无法找到); 实验题2 使用URL类下载西北农林科技大学首页import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import .URL;import javax.swing.JButton;import javax.swing.JEditor

3、Pane;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextField;public class WindwoURL extends JFrame implements ActionListener,Runnable JButton button; URL url; JTextField text; JEditorPane editPane; byte b=new byte118;

4、 Thread thread; public WindwoURL() text=new JTextField(20); editPane=new JEditorPane(); editPane.setEditable(false); button=new JButton (确定); button.addActionListener(this); thread=new Thread(this); JPanel p=new JPanel(); p.add(new JLabel(输入网址:); p.add(text); p.add(button); JScrollPane scroll=new JS

5、crollPane(editPane); add(scroll,BorderLayout.CENTER); add(p,BorderLayout.NORTH); setBounds(160,60,420,300); setVisible(true); validate(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public void actionPerformed(ActionEvent e) if(!thread.isAlive() thread=new Thread(this); try thread.start(); catch

6、(Exception ee) text.setText(我正在读取。+url); public void run() try int n=-1; editPane.setText(null); url=new URL(text.getText().trim(); editPane.setPage(url); catch(Exception e) text.setText(+e); return; public class main public static void main(String args) WindwoURL win=new WindwoURL(); 实验题3 利用Socket类

7、和ServerSocket类编写一个C/S程序,实现C/S通信。客户端向服务器端发送Time命令,服务器端接受到该字符串后将服务器端当前时间返回给客户端;客户端向服务器端发送Exit命令,服务器端向客户端返回“Bye”后退出。基本要求 编写完整程序;两人一组,一个作为服务器端,另一人作为客户端。服务器端和客户端都需要打印出接受到的消息和发出的命令。 package network; import .*; import java.io.*; import java.util.*; public class myclient public static void main(String a) So

8、cket client=null; OutputStream out=null; InputStream is=null; try client =new Socket(127.0.0.1,8080); while(true) out=client.getOutputStream(); is=client.getInputStream(); DataInputStream dis=new DataInputStream(is); Scanner x = new Scanner(System.in); String b = x.nextLine(); out.write(b.getBytes()

9、; System.out.println(dis.readUTF(); if(dis.readUTF()=Bye!) client.close();break; else Scanner x1= new Scanner(System.in); String b1= x1.nextLine(); out.write(b1.getBytes(); dis.close(); catch(Exception err) err.printStackTrace(); package network; import java.io.*; import java.util.*; import .*; publ

10、ic class TestServer public static void main(String a) ServerSocket s=null; try s=new ServerSocket(8080); while(true) Socket s1=s.accept(); InputStream in=s1.getInputStream(); bytebs=new byte60; int length=in.read(bs); OutputStream os=s1.getOutputStream(); DataOutputStream dos=new DataOutputStream(os

11、); String string=new String(bs,0,length); if(string.equals(Time) System.out.println(string); Date d=new Date(); String str=现在时间是:+d.getHours()+:+d.getMinutes()+:+d.getSeconds(); dos.writeUTF(str); else if(string.equals(Exit) dos.writeUTF(Bye!); break; else System.out.println(string); Scanner x = new

12、 Scanner(System.in); String b = x.next(); dos.writeUTF(b); s1.close(); catch(IOException e) System.out.println(程序正在运行出错:+e); 实验题4 编写一数据报通信程序,实现简单的聊天功能。图3.14 聊天程序界面基本要求 两人一组编写完整程序。“聊天内容”和“输入文本”分别为当前聊天的历史信息和当前要传送出去的聊天文本。“确定”、“清空”、“退出”三个按钮分别实现发送当前聊天文本、清空当前聊天文本和退出系统的功能。 package network; import javax.swi

13、ng.*; import java.awt.*; import java.awt.event.*; import java.io.*; import .*; import java.util.*; public class text4 extends JFrame public JTextArea l; public JTextField j; public JButton b1,b2,b3; public text4() this.setLayout(null); this.setBounds(0, 0, 500, 400); this.setTitle(小松); l=new JTextAr

14、ea(); l.setBounds(20, 20, 300, 250); this.add(l); j=new JTextField(); j.setBounds(20,285,300,65); this.add(j); b1=new JButton(确定); b1.setFont(new Font(,Font.BOLD,23); b1.setBounds(360, 20, 100, 90); b1.addActionListener(new ButtonListener(); this.add(b1); b2=new JButton(清空); b2.setFont(new Font(,Fon

15、t.BOLD,23); b2.setBounds(360,140, 100,90); b2.addActionListener(new ButtonListener(); this.add(b2); b3=new JButton(退出); b3.setFont(new Font(,Font.BOLD,23); b3.setBounds(360, 260, 100, 90); b3.addActionListener(new ButtonListener(); this.add(b3); this.setVisible(true); Thread thread=new Thread(new se

16、rver(); thread.start(); public static void main(Stringa) text4 t=new text4(); public class ButtonListener implements ActionListener public void actionPerformed(ActionEvent e) if(e.getSource()=b1) server udpserver=new server(); try udpserver.go(); catch(IOException ex) ex.printStackTrace(); System.ex

17、it(1); else if(e.getSource()=b2) l.setText(); else if(e.getSource()=b3) System.exit(0); public class server implements Runnable InetAddress clientAddress; public void run() DatagramPacket inDataPacket=null; DatagramSocket datagramSocket=null; byte msg=new byte128; try inDataPacket =new DatagramPacke

18、t(msg,msg.length); datagramSocket=new DatagramSocket(8012); catch(Exception e) while(true) if(datagramSocket=null) break; else try datagramSocket.receive(inDataPacket); String str1=new String(inDataPacket.getData(),0,inDataPacket.getLength(); l.append(str1+n); catch(Exception e) datagramSocket.close

19、(); public void go()throws IOException Date d=new Date(); String time=我是小松+ +d.getHours()+:+d.getMinutes()+:+d.getSeconds()+n; String str=time+j.getText(); byte by=str.getBytes(); clientAddress=InetAddress.getLocalHost(); DatagramPacket outDataPacket=new DatagramPacket(by,by.length,clientAddress,891

20、2); DatagramSocket datagramSocket1=new DatagramSocket(); datagramSocket1.send(outDataPacket); j.setText(); datagramSocket1.close(); package network; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import .*; import java.util.*; public class text3 extends JFrame pu

21、blic JTextArea l; public JTextField j; public JButton b1,b2,b3; public text3() this.setLayout(null); this.setBounds(0, 0, 500, 400); this.setTitle(小鱼); l=new JTextArea(); l.setBounds(20, 20, 300, 250); this.add(l); j=new JTextField(); j.setBounds(20,285,300,65); this.add(j); b1=new JButton(确定); b1.s

22、etFont(new Font(,Font.BOLD,23); b1.setBounds(360, 20, 100, 90); b1.addActionListener(new ButtonListener(); this.add(b1); b2=new JButton(清空); b2.setFont(new Font(,Font.BOLD,23); b2.setBounds(360,140, 100,90); b2.addActionListener(new ButtonListener(); this.add(b2); b3=new JButton(退出); b3.setFont(new

23、Font(,Font.BOLD,23); b3.setBounds(360, 260, 100, 90); b3.addActionListener(new ButtonListener(); this.add(b3); this.setVisible(true); Thread thread1=new Thread(new client(); thread1.start(); public static void main(Stringa) text3 t=new text3(); public class ButtonListener implements ActionListener p

24、ublic void actionPerformed(ActionEvent e) if(e.getSource()=b1) client udpclient=new client(); try udpclient.go(); catch(IOException ex) ex.printStackTrace(); System.exit(1); else if(e.getSource()=b2) l.setText(); else if(e.getSource()=b3) System.exit(0); public class client implements Runnable InetA

25、ddress serverAddress; public void run() DatagramPacket inDataPacket=null; DatagramSocket datagramSocket=null; byte inMsg=new byte128; try inDataPacket=new DatagramPacket(inMsg,inMsg.length); datagramSocket=new DatagramSocket(8912); catch(Exception e) while(true) if(datagramSocket=null) break; else t

26、ry datagramSocket.receive(inDataPacket); String str1=new String(inDataPacket.getData(),0,inDataPacket.getLength(); l.append(str1+n); catch(Exception e) datagramSocket.close(); public void go()throws IOException,UnknownHostException Date d=new Date(); String time=我是小鱼+ +d.getHours()+:+d.getMinutes()+

27、:+d.getSeconds()+n; String str=time+j.getText(); byte msg=str.getBytes(); serverAddress=InetAddress.getLocalHost(); DatagramPacket outDataPacket=new DatagramPacket(msg,msg.length,serverAddress,8012); DatagramSocket datagramSocket=new DatagramSocket(); datagramSocket.send(outDataPacket); j.setText(); datagramSocket.close(); 实验题5 客户机之间通过服务器交换数据。提示:为了适应传输各种类型的数据,必须

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

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