




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 商业智慧教育美妆品牌的市场策略与方向
- 医疗科技的创新之路基于设计思维的实践与探索
- 全球教育趋势与国际化发展战略规划
- 从学生心理角度看教育改革与发展趋势
- 商业培训中的互动游戏化教学策略研究
- 抖音商户直播样品签收确认制度
- 抖音商户直播停留时长优化方案执行制度
- 公交优先战略下2025年城市交通拥堵治理的智能交通设施布局分析报告
- CDK9-IN-38-生命科学试剂-MCE
- 浙江省温州市各学校2025届化学九年级第一学期期末检测试题含解析
- 医院智能化弱电设计方案
- 汽车租赁项目可行性报告
- 矿井灾变时期通风理论与技术及案例分析
- (苏教 译林版)三年级英语上册同步预习练习
- 2021年新《建设工程施工合同司法解释(一)》逐条解读4课件
- 绿城物业工程承接查验工作手册
- Q∕GDW 12185-2021 输变电设备物联网边缘计算应用软件接口技术规范
- 幼儿园一日活动流程保教细则
- 开利42CE系列风机盘管最新版样本
- 木托盘采购合同
- 2016年湖北省咸宁市中考数学试卷(解析版)(共22页)
评论
0/150
提交评论