文档介绍:第9章网络多媒体应用
内容提要:
Java网络编程基础
URL类和URLConnection类
InetAddress类
套接字Socket的应用
数据报套接字的应用
Applet基础
Applet事件响应和处理
Applet多媒体应用
Java网络编程基础
TCP/UDP协议
TCP (Transmission Control Protocol)和UDP(User Datagram Protocol)协议属于传输层协议。
IP协议
IP协议( Protocol)又称互联网协议,是支持网间互连的数据报协议,它与TCP协议(传输控制协议)一起构成了TCP/IP协议族的核心。
IP地址
TCP/IP用IP地址来标识源地址和目的地址
IP地址功能:为实现网络中不同计算机之间的通信,每台机器都必须有一个与众不同的标识---IP地址;
IP地址格式:数字型,32位,由4个 8位的二进制数组成,每8位之间用圆点隔开,如:;
IP地址组成:网络标识(netId) + 主机标识(hostId);
Java网络功能
Java所提供的网络功能可大致分为三大类:
URL和URLConnection
Socket
Datagram
9. 2 URL类和URLConnection类
URL(Uniform Resource Locator)----统一资源定位器,上某一资源的地址。
URL组成: 协议名和资源名
protocol:resourceName
URL举例:
/
/home/
http:///developers/
创建URL类对象
常用构造方法
public URL(String spec);
URL u1 = new URL("/"); public URL(URL context, String spec);
URL u2 = new URL(u1, “”);
public URL(String protocol, String host, String file);
URL u3 = new URL(“http”, “”, “developers/”);
public URL (String protocol, String host, int port, String file);
URL u4 = new URL(“http”, “”, 80, “developers/”);
URL类中获取对象特征的方法
URL类中一些很基本的方法如下:
  public final Obect getContent(); //这个方法取得传输协议。
 public String getFile(); //这个方法取得资源的文件名。
public String getHost(); //这个方法取得机器的名称。
  public int getPort(); //这个方法取得端口号。
public String getProtocol(); //这个方法取得传输协议。
public String toString(); //这个方法把URL转化为字符串
URL对象的创建及使用
import . *;
import .*;
class TestUrl
{public static void main(String args[]) {
try {
URL url=new URL("/");
("the Protocol: "+());
("the hostname: " +());
("the port: "+());
("the file:"+());
(());
}
catch(MalformedURLException e) {
(e);
}
}
}
获取URL对象的内容
import .*;
import .*;
public class URLReader{
public static void main(String args[]){
try{ URL tirc = new URL("/");