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