java dategramsocket_java实验报告网络编程

《java实验报告网络编程》由会员分享,可在线阅读,更多相关《java实验报告网络编程(21页珍藏版)》请在人人文库网上搜索。

1、信息工程学院Java语言课内实习报告 (20132014学年第 二学期)实习题目:网络编程姓 名:学 号:专 业:年级班级:一、实习目的(1)掌握Socket通信(2)掌握UDP通信。二、实习设计过程任务一:InetAddress类练习使用InetAddress类的方法获取www.nwsuaf.edu.cn的主机 的IP地址;获取本地机的名称和IP地址。运行结果如图所示:任务二:Socket类和ServerSocket类练习l 利用Socket类和ServerSocket类编写一个C/S程序,实现C/S通信。l 客户端向服务器端发送“Time”命令,服务器端接受到该字符串后将服务器端当前时间返。

2、回给客户端;客户端向服务器端发送“Exit”命令,服务器端向客户端返回“Bye”后退出服务器端代码:import java.io.*;import java.net.*;import java.text.SimpleDateFormat;import java.util.Date;public class Sever public static void main(String args) / TODO Auto-generated method stubtry ServerSocket server;server = new ServerSocket(500);System.out.prin。

3、tln(服务器已启动!);Socket you;you = server.accept();while(true)Date time= new Date();SimpleDateFormat dateformate=new SimpleDateFormat(hh:mm:ss);String s=new String();DataOutputStream out = new DataOutputStream(you.getOutputStream();DataInputStream in =new DataInputStream(you.getInputStream();s=in.readUTF。

4、();if(s.equals(Time)System.out.println(s);out.writeUTF(dateformate.format(time);Thread.sleep(300);else if(s.equals(Exit)System.out.println(连接已经断开!);out.writeUTF(Bye!);Thread.sleep(300);server.close();return; catch (Exception e1) / TODO Auto-generated catch blockSystem.out.println(连接已经断开!);e1.printSt。

5、ackTrace();客户端代码:import java.io.*;import java.net.*;public class Client public static void main(String args) / TODO Auto-generated method stubSocket socket;socket=new Socket();try InetAddress address = InetAddress.getByName(127.0.0.1);InetSocketAddress socketAddress= new InetSocketAddress(address,50。

6、0);socket.connect(socketAddress);DataInputStream in =new DataInputStream(socket.getInputStream();DataOutputStream out = new DataOutputStream(socket.getOutputStream(); while(true)BufferedReader br=new BufferedReader(new InputStreamReader(System.in); /System.out.println(br.readLine();out.writeUTF(br.r。

7、eadLine();String s=in.readUTF();System.out.println(s);if(s.equals(Bye!)socket.close();return;catch(Exception e)System.out.println(连接已经断开!);return;客户端运行结果:服务器端运行结果:任务三:DatagramSocket类和DatagramPacket类练习l 编写一个数据报通信程序,实现简单的聊天功能。l 基本要求:两人一组编写完整程序。发送信息部分代码:public void sendMessage()DatagramSocket mail_data。

8、=null;trybyte buffer=(InetAddress.getLocalHost().getHostAddress()+n +sendText.getText().trim()+n).getBytes();InetAddress address=InetAddress.getLocalHost();DatagramPacket datapack=new DatagramPacket(buffer,buffer.length,address,888);mail_data= new DatagramSocket();mail_data.send(datapack);text.appen。

9、d(InetAddress.getLocalHost().getHostAddress()+n +sendText.getText().trim()+n);sendText.setText(null);catch(Exception ee)ee.printStackTrace();接收信息部分代码:DatagramPacket pack=null;DatagramSocket mail_data=null;byte data=new byte8192;trypack=new DatagramPacket(data,data.length);mail_data=new DatagramSocke。

10、t(666);catch(Exception ee)ee.printStackTrace();while(true)if(mail_data=null)break;elsetrymail_data.receive(pack);String message=new String(pack.getData(),0,pack.getLength();text.append(message);catch(Exception ee)ee.printStackTrace();运行结果如下所示(因为在同一电脑上运行调试,所以发送信息时都是发送到本机的IP):任务四:传输对象输入与输出流 客户端:try So。

11、cket socket;socket=new Socket();InetAddress address = InetAddress.getByName(127.0.0.1);InetSocketAddress socketAddress= new InetSocketAddress(address,666);socket.connect(socketAddress);DataInputStream in =new DataInputStream(socket.getInputStream();while(true)String s=in.readUTF();System.out.println。

12、(s);Thread.sleep(300);socket.close();服务器端:try ServerSocket server;server = new ServerSocket(666);System.out.println(服务器已启动!);Socket you;you = server.accept();DataOutputStream out = new DataOutputStream(you.getOutputStream();String message=+student.id+ +student.name+ +student.age;out.writeUTF(message。

13、);Thread.sleep(300);you.close();server.close(); 运行结果:三、调试过程中存在问题分析调试运行过程中要选用对正确的端口值,可能端口当时正在被使用。四、心得、体会与建议这次实习感觉有点难,端口值的使用查询,还有IP的获取。老师,还有问题就是,我开始调试时找了一个同学,但是她的电脑上没有装java虚拟机,jar文件不能运行,我在网上找工具exej4,打包成exe文件,但是还是不能运行,具体的方法还是不知道怎么办。附录:任务一:import java.net.InetAddress;import java.net.UnknownHostException。

14、;public class TryInetAddress public static void main(String args) / TODO Auto-generated method stubInetAddress id1;try id1 = InetAddress.getByName(www.nwsuaf.edu.cn);System.out.println(学校IP地址:+id1.getHostAddress(); catch (UnknownHostException e) / TODO Auto-generated catch blocke.printStackTrace(); 。

15、InetAddress id2;try id2 = InetAddress.getLocalHost();System.out.println(本地的IP地址为:+id2.getHostAddress();System.out.println(本地的名称为:+id2.getHostName(); catch (UnknownHostException e) / TODO Auto-generated catch blocke.printStackTrace(); 任务二:服务器端:import java.io.*;import java.net.*;import java.text.Simpl。

16、eDateFormat;import java.util.Date;public class Sever public static void main(String args) / TODO Auto-generated method stubtry ServerSocket server;server = new ServerSocket(500);System.out.println(服务器已启动!);Socket you;you = server.accept();while(true)Date time= new Date();SimpleDateFormat dateformate。

17、=new SimpleDateFormat(hh:mm:ss);String s=new String();DataOutputStream out = new DataOutputStream(you.getOutputStream();DataInputStream in =new DataInputStream(you.getInputStream();s=in.readUTF();if(s.equals(Time)System.out.println(s);out.writeUTF(dateformate.format(time);Thread.sleep(300);else if(s。

18、.equals(Exit)System.out.println(连接已经断开!);out.writeUTF(Bye!);Thread.sleep(300);server.close();return; catch (Exception e1) / TODO Auto-generated catch blockSystem.out.println(连接已经断开!);e1.printStackTrace();客户端:import java.io.*;import java.net.*;public class Client public static void main(String args) 。

19、/ TODO Auto-generated method stubSocket socket;socket=new Socket();try InetAddress address = InetAddress.getByName(127.0.0.1);InetSocketAddress socketAddress= new InetSocketAddress(address,500);socket.connect(socketAddress);DataInputStream in =new DataInputStream(socket.getInputStream();DataOutputSt。

20、ream out = new DataOutputStream(socket.getOutputStream(); while(true)BufferedReader br=new BufferedReader(new InputStreamReader(System.in); /System.out.println(br.readLine();out.writeUTF(br.readLine();String s=in.readUTF();System.out.println(s);if(s.equals(Bye!)socket.close();return;catch(Exception 。

21、e)System.out.println(连接已经断开!);return;任务三:窗口一:import java.awt.*;import java.awt.event.*;import java.net.*;import javax.swing.*;public class Udppublic static void main(String args) Udp1 u1= new Udp1();class Udp1 extends JFrame implements ActionListener,Runnablepublic JTextArea text;public JTextField i。

22、pText;public JTextField sendText;public JButton button;public DatagramSocket socket;public JScrollBar vsBar;Thread thread;public Udp1() setTitle(UDP聊天程序一);setBounds(100, 100, 400, 300);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLayout(new BorderLayout();text = new JTextArea();text.setEditable。

23、(false);JScrollPane textPanel = new JScrollPane(text);vsBar = textPanel.getVerticalScrollBar();add(textPanel, BorderLayout.CENTER);JPanel panel = new JPanel();BorderLayout panelLayout = new BorderLayout();panelLayout.setHgap(5);panel.setLayout(panelLayout);try ipText = new JTextField(InetAddress.get。

24、LocalHost().getHostAddress(); catch (UnknownHostException e) / TODO Auto-generated catch blocke.printStackTrace();panel.add(ipText, BorderLayout.WEST);sendText = new JTextField();panel.add(sendText, BorderLayout.CENTER);button = new JButton(发送);panel.add(button, BorderLayout.EAST);add(panel, BorderL。

25、ayout.SOUTH);setVisible(true);thread=new Thread(this);thread.start();button.addActionListener(this);sendText.addKeyListener(new KeyListener()public void keyPressed(KeyEvent e) / 当按下回车时if (e.getKeyCode() = KeyEvent.VK_ENTER) sendMessage();public void keyReleased(KeyEvent e) public void keyTyped(KeyEv。

26、ent e);public void sendMessage()DatagramSocket mail_data=null;trybyte buffer=(InetAddress.getLocalHost().getHostAddress()+n +sendText.getText().trim()+n).getBytes();InetAddress address=InetAddress.getLocalHost();DatagramPacket datapack=new DatagramPacket(buffer,buffer.length,address,888);mail_data= 。

27、new DatagramSocket();mail_data.send(datapack);text.append(InetAddress.getLocalHost().getHostAddress()+n +sendText.getText().trim()+n);sendText.setText(null);catch(Exception ee)ee.printStackTrace();public void actionPerformed(ActionEvent ev) DatagramSocket mail_data=null;trybyte buffer=(InetAddress.g。

28、etLocalHost().getHostAddress()+n +sendText.getText().trim()+n).getBytes();InetAddress address=InetAddress.getLocalHost();DatagramPacket datapack=new DatagramPacket(buffer,buffer.length,address,888);mail_data=new DatagramSocket();mail_data.send(datapack);text.append(InetAddress.getLocalHost().getHost。

29、Address()+n +sendText.getText().trim()+n);sendText.setText(null);catch(Exception ee)ee.printStackTrace();public void run()DatagramPacket pack=null;DatagramSocket mail_data=null;byte data=new byte8192;trypack=new DatagramPacket(data,data.length);mail_data=new DatagramSocket(666);catch(Exception ee)ee。

30、.printStackTrace();while(true)if(mail_data=null)break;elsetrymail_data.receive(pack);String message=new String(pack.getData(),0,pack.getLength();text.append(message);catch(Exception ee)ee.printStackTrace();窗口二:import java.awt.*;import java.awt.event.*;import java.net.*;import javax.swing.*;public cl。

31、ass UdpTwo public static void main(String args) / TODO Auto-generated method stubUdp2 u2= new Udp2();SuppressWarnings(serial)class Udp2 extends JFrame implements ActionListener,Runnableprivate JTextArea text;private JTextField ipText;private JTextField sendText;private JButton button;public Datagram。

32、Socket socket;public JScrollBar vsBar;Thread thread;public Udp2() setTitle(UDP聊天程序二);setBounds(100, 100, 400, 300);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLayout(new BorderLayout();text = new JTextArea();text.setEditable(false);JScrollPane textPanel = new JScrollPane(text);vsBar = textPane。

33、l.getVerticalScrollBar();add(textPanel, BorderLayout.CENTER);JPanel panel = new JPanel();BorderLayout panelLayout = new BorderLayout();panelLayout.setHgap(5);panel.setLayout(panelLayout);ipText = new JTextField(172.17.38.11);panel.add(ipText, BorderLayout.WEST);sendText = new JTextField();panel.add(。

34、sendText, BorderLayout.CENTER);button = new JButton(发送);panel.add(button, BorderLayout.EAST);add(panel, BorderLayout.SOUTH);setVisible(true);thread=new Thread(this);thread.start();button.addActionListener(this);sendText.addKeyListener(new KeyListener()public void keyPressed(KeyEvent e) / 当按下回车时if (e。

35、.getKeyCode() = KeyEvent.VK_ENTER) sendMessage();public void keyReleased(KeyEvent e) public void keyTyped(KeyEvent e);public void sendMessage()DatagramSocket mail_data=null;trybyte buffer=(172.17.38.11+n +sendText.getText().trim()+n).getBytes();InetAddress address2=InetAddress.getLocalHost();Datagra。

36、mPacket datapack=new DatagramPacket(buffer,buffer.length,address2,666);/因为在一台电脑上调试,所以目标的ip都是本机的IP,才能接收到信息mail_data=new DatagramSocket();mail_data.send(datapack);text.append(172.17.38.11+n +sendText.getText().trim()+n);sendText.setText(null);catch(Exception ee)ee.printStackTrace();public void actionP。

37、erformed(ActionEvent ev) DatagramSocket mail_data=null;trybyte buffer=(172.17.38.11+n +sendText.getText().trim()+n).getBytes();InetAddress address=InetAddress.getLocalHost();DatagramPacket datapack=new DatagramPacket(buffer,buffer.length,address,666);/因为在一台电脑上调试,所以目标的ip都是本机的IP,才能接收到信息mail_data=new D。

38、atagramSocket();mail_data.send(datapack);text.append(172.17.38.11+n +sendText.getText().trim()+n);sendText.setText(null);/catch(Exception ee)ee.printStackTrace();public void run()DatagramPacket pack=null;DatagramSocket mail_data=null;byte data=new byte8192;trypack=new DatagramPacket(data,data.length。

39、);mail_data=new DatagramSocket(888);catch(Exception ee)ee.printStackTrace();while(true)if(mail_data=null)break;elsetrymail_data.receive(pack);String message=new String(pack.getData(),0,pack.getLength();text.append(message);catch(Exception ee)ee.printStackTrace();任务四:服务器端:import java.io.*;import java。

40、.net.*;public class Sever4 public static void main(String args) Student student= new Student(10090,小明,20);try ServerSocket server;server = new ServerSocket(666);System.out.println(服务器已启动!);Socket you;you = server.accept();DataOutputStream out = new DataOutputStream(you.getOutputStream();/DataInputSt。

41、ream in =new DataInputStream(you.getInputStream();String message=+student.id+ +student.name+ +student.age;out.writeUTF(message);Thread.sleep(300);you.close();server.close(); catch (Exception e) / TODO Auto-generated catch block/System.out.println(连接已经断开!);e.printStackTrace(); 客户端:import java.io.*;im。

42、port java.net.*;public class Client4 public static void main(String args) try Socket socket;socket=new Socket();InetAddress address = InetAddress.getByName(127.0.0.1);InetSocketAddress socketAddress= new InetSocketAddress(address,666);socket.connect(socketAddress);DataInputStream in =new DataInputStream(socket.getInputStream();while(true)String s=in.readUTF();System.out.println(s);Thread.sleep(300);socket.close();catch(Exception e)/System.out.println(连接已经断开!);return。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值