`
阅读更多
一、实验目的
1. 掌握InetAddress类的使用。
2. 掌握TCP与UDP编程:Socket与Datagram的概念和编程方法。
3. 掌握URL类的使用:URL的概念和编程。
二、实验要求
    通过Socket编程,掌握网络应用程序的开发方法;完成数据库的连接;掌握利用Java提供的基本组件进行网络传输;掌握Java提供的多线程机制,异常处理机制和低层对协议的通信机制,通过Socket编程,掌握网络应用程序的开发方法;设计测试,性能评估。上机练习之前,必须先完成程序的书写,再上机调试。
三、实验内容
(一)使用InetAddress类的方法
通过使用InetAddress类的方法,获取http://www.ytu.edu.cn/的主机的IP地址;获取本地机的名称和IP地址。
(二)Socket编程
使用Socket编程,客户机发送数据到服务器,服务器将接收到的数据返回给客户机。
(三)UDP编程
使用UDP编程,客户机发送数据到服务器,服务器将接收到的数据返回给客户机
(四)获取URL信息
1. 编写KY12_1.java 程序文件,源代码如下。
import java.net.*;
import java.io.*;
public class URLTest {
          public static void main(String[] args){
             URL url=null;
             InputStream is;
             try{
                  url=new URL("http://localhost/index.html");
                  is=url.openStream();
                  int c;
                  try{
                while((c=is.read())!=-1)
                         System.out.print((char)c);
                  }catch(IOException e){
}finally{
                        is.close();
}
             }catch(MalformedURLException e){
  e.printStackTrace();
}catch(IOException e){
     e.printStackTrace();
}
        System.out.println("文件名:"+url.getFile());
        System.out.println("主机名:"+url.getHost());
        System.out.println("端口号:"+url.getPort());
        System.out.println("协议名:"+url.getProtocol());
       }
}
2. 编译并运行
(五)利用URL类获取网络资源
1. 编写KY12_2.java 程序文件,源代码如下。
import java.net.*;
import java.io.*;
public class URLReader {
     public static void main(String[] args) throws Exception {
         URL web = new URL("http://166.111.7.250:2222/");
                  BufferedReader in = new BufferedReader(new InputStreamReader(web.openStream()));
               String inputLine;
               while ((inputLine = in.readLine()) != null)System.out.println(inputLine);
               in.close();
     }
}
2. 编译并运行
(六)利用URLConnection对URL资源的读取
1. 编写KY12_3.java 程序文件,源代码如下。
import java.net.*;
import java.io.*;
public class URLConnectionReader {
     public static void main(String[] args) throws Exception {
         URL web = new URL("http://166.111.7.250:2222/");
         URLConnection webc=web.openConnection();   
//get an instance of URLConnection
         BufferedReader in = new BufferedReader(new InputStreamReader(
                          webc.getInputStream()));           //use of URLConnection
         String inputLine;
         while ((inputLine = in.readLine()) != null) System.out.println(inputLine);
         in.close();
     }
}
2. 编译并运行
(七)掌握URLConnection对URL资源的写入
1. 编写KY12_4.java 程序文件,源代码如下。
import java.io.*;
import java.net.*;
public class Reverse {
     public static void main(String[] args) throws Exception {
               if (args.length != 1) {
                      System.err.println("Usage:  java Reverse string_to_reverse");
                      System.exit(1);    
               }
               String stringToReverse=args[0];
               URL url = new URL("http://java.sun.com/cgi-bin/backwards");
               URLConnection connection = url.openConnection();
               connection.setDoOutput(true);
               PrintWriter out = new PrintWriter(connection.getOutputStream());
               out.println("string=" + stringToReverse); 
               out.close();
               BufferedReader in = new BufferedReader(new InputStreamReader(
                                            connection.getInputStream()));
               String inputLine;
               while ((inputLine = in.readLine()) != null) System.out.println(inputLine);
               in.close();
               }
     }
2. 编译并运行
四、思考题
1. 什么是URL?一个URL地址由哪些部分组成?
2. 网络环境下的C/S模式的基本思想是什么?什么是客户机?什么是服务器?它们各自的作用如何?C/S模式的基本工作过程如何?
3. 简述流式Socket的通信机制。它的最大特点是什么?
4. 数据报通信有何特点?简述Java实现数据报通信的基本工作过程。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics