java实验报告网络基础.doc_第1页
java实验报告网络基础.doc_第2页
java实验报告网络基础.doc_第3页
java实验报告网络基础.doc_第4页
java实验报告网络基础.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

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

文档简介

实验八 网络编程基础1实验目的(1)掌握Socket通信。(2)掌握UDP通信2实验内容实验题1 使用InetAddress类的方法获取/的主机的IP地址;获取本地机的名称和IP地址。运行结果:主要代码:package package1;import .*;public class Ex7_1 /* * param args */public static void main(String args) / TODO Auto-generated method stubtry InetAddress myNet = InetAddress.getByName();System.out.println(myNet.toString();System.out.println(InetAddress.getLocalHost().toString(); catch (UnknownHostException e) / TODO Auto-generated catch blocke.printStackTrace();实验题2 使用URL类下载西北农林科技大学首页/,并统计下载得到网页文件的大小。运行结果:主要代码:public void run() tryint n=0;int sum = 0;jEditorPane1.setText(null);url = new URL(http:/+jTextField1.getText().trim();InputStream in = url.openStream();while(n = in.read(b)!=-1)sum+=n;jEditorPane1.setPage(url);jTextField2.setText(+sum);catch(Exception e1)jTextField1.setText(+e1);private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here:if(!(thread.isAlive()thread = new Thread(this);trythread.start();catch(Exception e)jTextField1.setText(+url);实验题3 利用Socket类和ServerSocket类编写一个C/S程序,实现C/S通信。客户端向服务器端发送Time命令,服务器端接受到该字符串后将服务器端当前时间返回给客户端;客户端向服务器端发送Exit命令,服务器端向客户端返回“Bye”后退出。基本要求 编写完整程序;两人一组,一个作为服务器端,另一人作为客户端。服务器端和客户端都需要打印出接受到的消息和发出的命令。运行结果:服务器端客户端 主要代码:服务器端:package package3;import java.io.*;import .*;import java.util.Date;public class Server /* * param args */public static void main(String args) / TODO Auto-generated method stubServerSocket server = null;Socket you = null;InputStreamReader in = null;OutputStreamWriter out =null;Date today = new Date();tryserver = new ServerSocket(18949);catch(IOException e)System.out.println(e);trySystem.out.println(等待客户连接);you = server.accept();in = new InputStreamReader(you.getInputStream();out =new OutputStreamWriter(you.getOutputStream();BufferedWriter bout = new BufferedWriter(out);BufferedReader bin = new BufferedReader(in);String temp = null;while(true)System.out.println(等待客户信息 );temp=bin.readLine();if(temp.equals(Exit)System.out.println(收到客户信息 +temp);bout.write(Bye);bout.newLine();bout.flush();break;System.out.println(收到客户信息 +temp);bout.write(today.toString();bout.newLine();bout.flush();server.close();you.close();catch(IOException e1)System.out.println(客户端断开连接 +e1);客户端:package package3;import java.io.*;import .*;public class Client /* * param args */public static void main(String args) / TODO Auto-generated method stubSocket mysocket;InputStreamReader in = null;OutputStreamWriter out =null;trymysocket = new Socket(,18949);in = new InputStreamReader(mysocket.getInputStream();out =new OutputStreamWriter(mysocket.getOutputStream();BufferedWriter bout = new BufferedWriter(out);BufferedReader bin = new BufferedReader(in);while(true)bout.write(Time);System.out.println(Client send : Time);bout.newLine();bout.flush();String temp = null;temp = bin.readLine();System.out.println(From server: +temp);bout.write(Exit);System.out.println(Client send : Exit);bout.newLine();bout.flush();temp = bin.readLine();if(temp.equals(Bye)System.out.println(From server: +temp);break;mysocket.close();catch(Exception e)System.out.println(e);实验题4 编写一数据报通信程序,实现简单的聊天功能。聊天内容输入文本确定清空退出图3.14 聊天程序界面基本要求 两人一组编写完整程序。“聊天内容”和“输入文本”分别为当前聊天的历史信息和当前要传送出去的聊天文本。“确定”、“清空”、“退出”三个按钮分别实现发送当前聊天文本、清空当前聊天文本和退出系统的功能。运行结果:主要代码:public void run()jTextArea1.append(开始接收信息:+n);DatagramPacket pack = null;DatagramSocket mailPack =null;byte data = new byte8192;trypack = new DatagramPacket(data,data.length);mailPack = new DatagramSocket(29249);catch(Exception e)System.out.println(e+n);while(true)if(mailPack = null)break;elsetry mailPack.receive(pack);String message = new String(pack.getData(),0,pack.getLength();if(message.equals(bye)jTextArea1.append(对方已下线 !+n);break;jTextArea1.append(接收到信息:+message+n); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();private void oKActionPerformed(ActionEvent evt) /System.out.println(oK.actionPerformed, event=+evt);/TODO add your code for oK.actionPerformedString message = jTextField1.getText();byte buffer=message.trim().getBytes();jTextArea1.append(发出的信息:+message+n);tryInetAddress address = InetAddress.getByName();DatagramPacket dataPack = new DatagramPacket(buffer,buffer.length,address,8976);DatagramSocket mailPack = new DatagramSocket();mailPack.send(dataPack);catch(Exception e)private void clearActionPerformed(ActionEvent evt) /System.out.println(clear.actionPerformed, event=+evt);/TODO add your code for clear.actionPerformedjTextField1.setText();private void quitActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here:String message = bye;byte buffer = message.trim().getBytes();jTextArea1.append(您已退出程序! + n);try InetAddress address = InetAddress.getByName();DatagramPacket dataPack = new DatagramPacket(buffer, buffer.length,address,inportnum);DatagramSocket mailPack = new DatagramSocket();mailPack.send(dataPack); catch (Exception e) 实验题5 客户机之间通过服务器交换数据。提示:为了适应传输各种类型的数据,必须统一客户机的数据格式,为此可以将数据封装在一个对象中,数据传输以对象的形式传输。因此必须定义一个数据封装的类,此类实现Serializable接口。运行结果:将对象写入文本从文本读取打印结果主要代码:package package5;import java.io.Serializable;public class Student implements SerializableString name = null;int age = 0;public Student(String name,int age) = name;this.age = age;public String toString()return(姓名:+name+ 年龄:+age);package package5;import java.io.*;public class ObjectInFile public void writerObject()File outF = new File(object.txt);Students = new Student4;s0 = new Student(1sadas,21);s1 = new Student(2qwert,22);s2 = new Student(3yuiop,23);s3 = new Student(4mnbbv,24);tryFileOutputStream fileOut = new FileOutputStream(outF);ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);for(int i=0;i4;i+)objectOut.writeObject(si);objectOut.close();fileOut.close();catch(IOException e)System.out .println(e);package package5;import java.io.*;public class ObjectOutFile public void readerObject()File outF = ne

温馨提示

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

评论

0/150

提交评论