实验12 网络编程

一、实验目的
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实现数据报通信的基本工作过程。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值