实验08 Java之网络编程.doc_第1页
实验08 Java之网络编程.doc_第2页
实验08 Java之网络编程.doc_第3页
实验08 Java之网络编程.doc_第4页
实验08 Java之网络编程.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验七 网络编程1实验目的(1)掌握Socket通信。(2)掌握UDP通信2实验内容实验题1 利用Socket类和ServerSocket类编写一个C/S程序,实现C/S通信。客户端向服务器端发送Time命令,服务器端接受到该字符串后将服务器端当前时间返回给客户端;客户端向服务器端发送Exit命令,服务器端向客户端返回“Bye”后退出。package cn.e;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.DataInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import .Socket;import .UnknownHostException;import java.util.Scanner;public class Client public static void main(String args) Socket client = null; InputStreamReader in = null;/将字节流变为字符流,有转换功能 OutputStreamWriter out = null; try client = new Socket(localhost,4331); catch (UnknownHostException e2) / TODO Auto-generated catch blocke2.printStackTrace(); catch (IOException e2) / TODO Auto-generated catch blocke2.printStackTrace(); try in = new InputStreamReader(client.getInputStream(); catch (IOException e1) / TODO Auto-generated catch blocke1.printStackTrace(); try out=new OutputStreamWriter(client.getOutputStream(); catch (IOException e1) / TODO Auto-generated catch blocke1.printStackTrace(); BufferedWriter bout=new BufferedWriter(out); BufferedReader bin=new BufferedReader(in); while(true)/从客户端向服务器传输数据 System.out .println(请输入Time或者Exit); Scanner reader=new Scanner(System.in); try bout.write(reader.next(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); try bout.newLine(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); try bout.flush(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); String massage = null; try massage=bin.readLine(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); if(massage.equals(Bye) System.out.println(Fromserver:+massage); break; else System.out.println(从服务器返回的时间:+massage); try client.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); package cn.e;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import .ServerSocket;import .Socket;import java.text.SimpleDateFormat;import java.util.Date;public class Server public static void main(String args) ServerSocket server = null;try server = new ServerSocket(4331); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();System.out.println(服务器启动完毕);System.out.println(等待客户端连接.);Socket you = null;try you = server.accept(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();InputStreamReader in = null;OutputStreamWriter out = null;try in = new InputStreamReader(you.getInputStream(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();try out = new OutputStreamWriter(you.getOutputStream(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();BufferedWriter bout = new BufferedWriter(out);BufferedReader bin = new BufferedReader(in);if (you.isConnected() System.out.println(客户端名称为 + you.getInetAddress().getHostAddress()+ 连接成功!);while (true) String s = null;try s = bin.readLine(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();if (s.equals(Time) Date date = new Date();System.out.println(客户端请求当前时间);SimpleDateFormat format = new SimpleDateFormat(yyyy/MM/ddHH:mm:ss); else if (s.equals(exit) try bout.write(bye!); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();try bout.newLine(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();try bout.flush(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();try you.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();基本要求 编写完整程序。实验题2 编写一数据报通信程序,实现简单的聊天功能。聊天内容输入文本确定清空退出图3.9 聊天程序界面基本要求 两人一组编写完整程序。“聊天内容”和“输入文本”分别为当前聊天的历史信息和当前要传送出去的聊天文本。“确定”、“清空”、“退出”三个按钮分别实现发送当前聊天文本、清空当前聊天文本和退出系统的功能。package cn.e;import java.awt.*;import java.awt.event.*;import java.io.IOException;import .*;import javax.swing.*;public class UDPmessage extends JFrame implements ActionListener private JTextArea text;private JTextField ipText;private JTextField sendText;private JButton button1;private JButton button2;private JButton button3;private DatagramSocket socket;private JScrollBar vsBar;public UDPmessage() 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 = textPanel.getVerticalScrollBar();add(textPanel, BorderLayout.CENTER);JPanel panel = new JPanel();/一定要有的BorderLayout panelLayout = new BorderLayout();panelLayout.setHgap(5);panel.setLayout(panelLayout);ipText = new JTextField(03);panel.add(ipText, BorderLayout.WEST);sendText = new JTextField();panel.add(sendText, BorderLayout.CENTER);button1 = new JButton(确定);/button2 = new JButton(清空);/button3 = new JButton(退出);panel.add(button1, BorderLayout.EAST);add(panel, BorderLayout.SOUTH);setVisible(true); server();button1.addActionListener(this);private void server() try socket=new DatagramSocket(9527); catch (SocketException e) / TODO Auto-generated catch blocke.printStackTrace();/实例化数据报套接字byte buf=new byte1024;final DatagramPacket dpl=new DatagramPacket(buf,buf.length);/创建接收数据的数据包Runnable runnable=new Runnable()public void run()while(true)try Thread.sleep(100); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();try socket.receive(dpl); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();int length=dpl.getLength();String message=new String(dpl.getData(),0,length);/获取数据包的字符串信息String ip=dpl.getAddress().getHostAddress();try if(!InetAddress.getLocalHost().getHostAddress().equals(ip); catch (UnknownHostException e) / TODO Auto-generated catch blocke.printStackTrace();text.append(ip+:n+message+n);vsBar.setValue(vsBar.getMaximum();/控制信息滚动;public void actionPerformed(ActionEvent ev) String ip=ipText.getText();/获取IP文本框内容InetAddress address = null;try address = InetAddress.getByName(ip); catch

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论