




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
2015 2016 学年第一学期学年第一学期 Java 程序设计程序设计 实验报告十实验报告十 班级班级计科三班学号学号20132155姓名姓名邢超超 实验时间实验时间12 17实验地点实验地点东实验楼 2 204实验成绩实验成绩 一 一 实验题目实验题目 1 验证 InetAddress 类的使用 2 验证 URL 类访问 WWW 资源 3 验证 Socket 类客户端程序设计方法 4 验证 ServerSocket 类服务器端程序设计方法 5 验证 Datagram 客户端 服务器端程序设计方法 6 验证基于客户 服务器模式的分布式程序设计方法 二 二 实验目的实验目的 学习和掌握网络程序设计的基本方法 三 实验过程实验过程 1 2 四 四 实验心得实验心得 五 五 源程序源程序 1 import javax swing import java awt import import java awt event public class LocalHost extends JApplet InetAddress hostAddr null public void start try hostAddr InetAddress getLocalHost catch UnknownHostException e System out println e getMessage repaint public void paint Graphics g g drawString Host name ip hostAddr toString 10 30 2 import import java io public class OpenStreamTest public static void main String args try URL yahoo new URL DataInputStream dis new DataInputStream yahoo openStream String inputLine while inputLine dis readLine null System out println inputLine dis close catch MalformedURLException me System out println MalformedURLException me catch IOException ioe System out println IOException ioe 3 import import java io public class ConnectionTest public static void main String args try URL yahoo new URL URLConnection yahooConnection yahoo openConnection DataInputStream dis new DataInputStream yahoo openStream String inputLine while inputLine dis readLine null System out println inputLine dis close catch MalformedURLException me System out println MalformedURLException me catch IOException ioe System out println IOException ioe 4 import import java io public class WebConnect public static void main String args try Socket connection new Socket args 0 80 System out println Connection established System out println Local Connection Information System out println t address connection getLocalAddress System out println t port connection getLocalPort System out println Remate Connection Information System out println t address connection getLocalAddress System out println t port connection getLocalPort connection close catch MalformedURLException uhe System out println Unknown host args 0 catch IOException ioe System out println IOException ioe 5 import import java io public class GetDayTime public static void main String args try Socket connection new Socket host 13 System out println Connection established BufferedReader in new BufferedReader new InputStreamReader connection getInputStream String daytime in readLine System out println DayTime received daytime Connection close catch MalformedURLException uhe System out println Host not found uhe catch IOException ioe System out println IOException ioe 6 import import java io public class EchoClient public static void main String args try Socket connection new Socket args 0 7 System out println Connection established DataInputStream in new DataInputStream connection getInputStream DataOutputStream out new DataOutputStream connection getOutputStream String line new String While line toupperCse equals QUIT System out println Enter string Line readString System out println tSending string to server out writeUTF line System out println tWaiting for server reponse Line in readUTF System out println Received line in close out close connection close catch MalformedURLException uhe System out println Unknown host args 0 catch IOException ioe System out println IOException ioe Public static String readString String string new String BufferedReader in new BufferedReader new InputStreamReader System in Try String in reaLine catch IOException e System out println Console readString Unknown error System exit 1 return string 7 import java io import import java util public class WebPage private LinkedList tokens new LinkedList public WebPage String host int port String file tokenizer loader host port file private void tokenizer String content StringTokenizer tags new StringTokenizer content if split hasMoreTokens tokens addLast new HTMLTag split nextToken if split hasMoreTokens tokens addLast new HTMLText split nextToken private String loader String host int port String file String content new String line new String try Socket client new Socket host port PrintWriter out new PrintWriter new OutputStreamWriter client getOutputStream BufferedReader in new BufferedReader new InputStreamReader client getInputStream out println GET file HTTP 1 0 out println out flush while line in readLine null content line in close out close client close catch MalformedURLException uhe content No content host host unknown catch IOException ioe content No content host host refused return content public String getContent String content new String for Iterator iterator tokens iterator iterator hasNext content iterator next toString return content import javax swing text html HTML public class HTMLTag extends HTML public HTMLTag String token String token token trim public String toString String token null String tag token toUpperCase if tag startsWith P tag startsWith DIV tag startsWith H tag startsWith UL tag startsWith DL tag startsWith UL tag startsWith DL tag sta rtsWith BR return n else if token startsWith LI return n else if token startsWith A return token else return import javax swing text html HTML public class HTMLText extends HTML public HTMLText String token String token token trim public class HTML protected String token public String toString return token import java awt import java awt event import import java util EventObject public class WebViewer extends Frame implements ActionListener private TextField address new TextField private TextArea display new TextArea private Button go new Button View Page private class WindowCloser extends WindowAdapter public void windowClosing WindowEvent we System exit 0 public WebViewer super Web Viewer Lite Panel north new Panel north setLayout new BorderLayout north add West new Label URL north add Center address north add East go go addActionListener this Panel center new Panel setLayout new BorderLayout add North north add Center display addWindowListener new WindowCloser validate pack setVisible true public void actionPerformed ActionEvent ae if ae getSource go showURL address getText private void showURL String address try setCursor new Cursor Cursor WAIT CURSOR URL url new URL address String host url getHost int port url getPort if port 0 port 80 WebPage page new WebPage host port url getFile display setText page getContent catch MalformedURLException murle display setText Invalid URL address finally setCursor new Cursor Cursor DEFAULT CURSOR public static void main String args WebViewer viewer new WebViewer 8 import import java io public class EchoServer private static boolean running true public static void main String args try ServerSocket server new ServerSocket Integer parseInt args 0 System out println Server started on server getLocalPort while running Socket connection server accept System out println New connection moved to thread EchoServerThread handler new EchoServerThread connection catch IOException ioe System out println Error ioe import java io import public class EchoServerThread extends Thread private Socket connection public EchoServerThread Socket connection connection connection start public void run try DataInputStream in new DataInputStream connection getInputStream DataOutputStream out new DataOutputStream connection getOutputStream String line new String while line equalsIgnoreCase QUIT line in readUTF System out println Echoing line out writeUTF line in close out close connection close System out println Connection closed catch IOException ioe System out println Connection closed unexpectedly 9 import 10 Public class UDPScan Public static void main String args for int port 1024 port 65535 port try DatagramSocket server new DatagramSocket port Server close catch SocketException e System out println there is a server in port port 10 import import java io public class UDPServer public static void main String args try DatagramSocket receiveSocket new DatagramSocket 5678 byte buf new byte 1024 DatagramPacket receivePacket new DatagramPacket buf buf length System out println 开始接受数据包 while true receiveSocket receive receivePacket String hostname receivePacket getAddress toString System out println 来自主机 hostname 端口 receivePacket getPort String s new String receivePacket getData 0 receivePacket getLength System out println 接收到的数据为 s catch SocketException e e printStackTrace System exit 1 catch IOException e System out println 网络通信出现错误 问题在 e toString import java io import public class UDPClient public static void main String args try DatagramSocket sendSocket new DatagramSocket 3456 String string Hello Beijing 2008 byte databyte new byte 100 databyte string getBytes DatagramPacket sendPacket new DatagramPacket databyte string length InetAddress getByName 127 0 0 1 567 8 sendSocket send sendPacket System out println 发送数据 你好 这是客户端发来的数据 System out println string catch SocketException e System out println 不能打开 Datagram Socket 或 Datagram Socket 无法与指定端 口连接 catch IOException e System out println 网络通信出现错误 问题在 e toString 11 import import java io import java util public class EchoServer public static void main String args throws IOException new EchoServerThread start import import java io import java util public class EchoServerThread extends Thread protected DatagramSocket socket null protected BufferedReader in null protected boolean hasMoreWork true protected int number 0 public EchoServerThread throws IOException this EchoServerThrea
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 风电场电网接入与调度方案
- 2024中级经济师《工商管理》试题及参考答案(考生回忆版)
- 污水处理厂电力与水力优化方案
- 二零二五年度建筑玻璃幕墙节能保温工程合同
- 二零二五年度国际工程项目经理聘任协议
- 2025版联合体投标协议书-现代农业园区建设项目模板
- 污水处理厂抗污染能力提升方案
- 二零二五版无人机技术应用高额抵押反担保合同
- 二零二五年度建筑工地水泥黄沙精细化管理承包合同
- 二零二五年度绿色生态住宅代理销售协议
- 建筑工程造价课程设计实例
- 2025年江苏省苏豪控股集团有限公司校园招聘笔试备考试题及答案详解(各地真题)
- 存款保险宣传培训
- 幼儿园速叠杯培训
- 质量检查员基础知识培训
- 燃气施工安全培训课件
- 新初中班主任培训
- 具有履行合同所必需的设备和专业技术能力的承诺书完整版
- 茶馆门店运营管理制度
- 中职宿舍规矩管理制度
- 燃气生产调度管理制度
评论
0/150
提交评论