java 网络编程知识点(一)

来源:互联网 发布:windows双系统怎么删除 编辑:程序博客网 时间:2024/06/08 07:09

java 网络编程要点(一)

java.net.*;==>包含了java网络编程所有需要的类;

1)掌握java网络编程,需要了解以下四点:

a)用来存储interne地址的类===InetAddress

b)用来管理web网络的的类===URL 和URLConnection

c)用于传统的socket(TCP)网络编程方式的类==Socket 和SeverSocket

d)用于UDP网络编程方式的类==DatagramPacket和DatagramSocket

2)详解以上各种类

InetAddress类

a)用于描述internet地址的类,封装了数字化的IP地址和对应的域名。使用IP主机的域名与这个类交互,与使用IP地址相比,使用IP地址域名更方便、易理解。InetAddress可以处理Ipv4和Ipv6。

b)没有可见的构造函数,但提供了可以创建InetAddress对象的方法。(系统会自动查询域名系统或者通过网络获取)

static InetAddress getLocalHost() thorws UnkonwHostException===>通过获取本主机来获得InetAddress对象;

static InetAddress getByName(String hostName) thorws UnkonwHostException===>通过主机名称来获得InetAddress对象;

static InetAddress[] getAllByName(String hostName) thorws UnkonwHostException===>通过主机名称来获得InetAddress对象;

static InetAddress getAllByAddress(byte[] addr) thorws UnkonwHostException===>通过IP地址来获得InetAddress对象;

static InetAddress getAllByAddress(String host,byte[] addr) thorws UnkonwHostException===>通过IP地址和主机名来获得InetAddress对象;

c)InetAddress类可用的方法

byte[] getAddress()==>获取IP地址

String getHostAddress()==>获取ip地址的字符串形式

String getHostName()==>获取主机名

boolean isMulticastAddress()==》判断是否是一个多播地址

String toString()==>返回一个主机名和地址的字符串

d)实例

import java.net.*;
public class Test {


/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成的方法存根


InetAddress mInetAddress;
try {
mInetAddress=InetAddress.getLocalHost();
System.out.println(mInetAddress);

mInetAddress=InetAddress.getByName("www.baidu.com");
System.out.println(mInetAddress);

mInetAddress=InetAddress.getByName("www.csdn.com");
System.out.println(mInetAddress);

String mIP="119.75.217.109";
               String[] ipStr = mIP.split("\\.");
                byte[] ipBuf = new byte[4];
                for(int i = 0; i < 4; i++){
                   ipBuf[i] = (byte)(Integer.parseInt(ipStr[i])&0xff);
                }//注意IP是四个字节,因此需要转化为四个字节形式,等同于下式

                    /*  ipBuf[0]=119;
ipBuf[1]=75;
ipBuf[2]=(byte) 217;
ipBuf[3]=109;

                    */
mInetAddress=InetAddress.getByAddress(ipBuf);
System.out.println(mInetAddress);

} catch (UnknownHostException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}

the result:

720-PC/169.254.10.84
www.baidu.com/119.75.217.109
www.csdn.com/114.112.73.194
/119.75.217.109


URL和URLConnection类

a)URL用于访问Web资源==》统一资源定位器

URL为唯一标识或寻址Internet上的信息,提供了一种相当容易理解的形式。代表一个统一资源定位符,它是指向互联网“资源”的指针。资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用,例如对数据库或搜索引擎的查询。

URL规范基于4个部分。使用协议:// 定位器。常用的协议 http、ftp、file等,协议与定位器之间以://分开;定位器之间用/或者(:端口)。

b)URL构建对象

URL(String spec)

URL(String protoclol,String host,int port,String file)

URL(String protocol,String host,String file)

URL(String protocol,String host, int post,String file,URLSteamHandler handle)

URL(URL context,String spec)

URL(URL context,String spec,URLStreamHandler handler)

c)常用的方法

String getHost()

String getPort()

String getProtoclo()

URLConnection openConnection() 获取一个URLConnection对象

InputStream openStream()从此URL出获取内容

c)URLConnection

用于访问远程资源属性的通用类,

常用方法:

int getContentLength()

String getContentType()==content-type

long getData()==>返回相应的时间和日期

InputStream getInputStream()

d)实例

public void testUrl(String strURL){
try {
URL mURL=new URL(strURL);
System.out.println(mURL.getHost());
System.out.println(mURL.getPort());
System.out.println(mURL.getProtocol());
System.out.println(mURL.getQuery());
System.out.println(mURL.getFile());

try {
InputStream minputStream=mURL.openStream();
BufferedReader mbuffer=new BufferedReader(new InputStreamReader(minputStream));
String line=null;
line=mbuffer.readLine();
while(line!=null)
{
System.out.println(line);
line=mbuffer.readLine();
}
mbuffer.close();
minputStream.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}


} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

}

public void testURLConnection(String strURL)
{

try {
URL mURL=new URL(strURL);
try {
URLConnection mCon=mURL.openConnection();

long len=mCon.getDate();
System.out.println(new Date(len));
System.out.println(mCon.getContentType());
System.out.println(mCon.getContentEncoding());
System.out.println(mCon.getDoInput());
System.out.println(mCon.getDoOutput());

Map<String,List<String>>m_Map=mCon.getHeaderFields();
System.out.println(m_Map);
Set<String>m_Set=m_Map.keySet();
Iterator<String> m_Iterator=m_Set.iterator();
for(int i=0;i<m_Map.size();i++)
{
System.out.println(m_Iterator.next());
}
InputStream minputStream=mCon.getInputStream();
BufferedReader mbuffer=new BufferedReader(new InputStreamReader(minputStream));
String line=null;
line=mbuffer.readLine();
while(line!=null)
{
System.out.println(line);
line=mbuffer.readLine();
}
mbuffer.close();
minputStream.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}


} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

}

the result:

Wed Apr 29 17:34:10 CST 2015
text/html; charset=utf-8
null
true
false
{null=[HTTP/1.1 200 OK], Expires=[Wed, 29 Apr 2015 09:33:57 GMT], X-UA-Compatible=[IE=Edge,chrome=1], Set-Cookie=[H_PS_PSSID=1464_7477_12772_13075_12867_13323_12691_10562_12722_13477_13201_13602_13162_13762_13257_11912_13085_8498; path=/; domain=.baidu.com, BD_HOME=0; path=/, BDSVRTM=0; path=/, BIDUPSID=8ED3781FF9FA37EBA95DA36A1806244D; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, BAIDUID=8ED3781FF9FA37EBA95DA36A1806244D:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com], Connection=[Keep-Alive], Server=[BWS/1.1], X-Powered-By=[HPHP], Cache-Control=[private], Date=[Wed, 29 Apr 2015 09:34:10 GMT], Transfer-Encoding=[chunked], BDQID=[0xe95331890001c960], Vary=[Accept-Encoding], P3P=[CP=" OTI DSP COR IVA OUR IND COM "], BDPAGETYPE=[1], Cxy_all=[baidu+b5dad76cb0728467bb700f68978afa18], BDUSERID=[0], Content-Type=[text/html; charset=utf-8]}
null
Expires
X-UA-Compatible
Set-Cookie
Connection
Server
X-Powered-By
Cache-Control
Date
Transfer-Encoding
BDQID
Vary
P3P
BDPAGETYPE
Cxy_all
BDUSERID
Content-Type
<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=Edge"><meta content="always" name="referrer"><link rel="dns-prefetch" href="//s1.bdstatic.com"/><link rel="dns-prefetch" href="//t1.baidu.com"/><link rel="dns-prefetch" href="//t2.baidu.com"/><link rel="dns-prefetch" href="//t3.baidu.com"/><link rel="dns-prefetch" href="//t10.baidu.com"/><link rel="dns-prefetch" href="//t11.baidu.com"/><link rel="dns-prefetch" href="//t12.baidu.com"/><link rel="dns-prefetch" href="//b1.bdstatic.com"/><title>鐧惧害涓?笅锛屼綘灏辩煡閬?/title>
<style index="index"  id="css_index">html,body{height:100%}html{overflow-y:auto}#wrapper{position:relative;_position:;min-height:100%}#head{padding-bottom:100px;text-align:center;*z-index:1}#ftCon{height:100px;position:absolute;bottom:44px;text-align:center;width:100%;margin:0 auto;z-index:0;overflow:hidden}#ftConw{width:720px;margin:0 auto}body{font:12px arial;text-align:;background:#fff}body,p,form,ul,li{margin:0;padding:0;list-style:none}body,form,#fm{position:relative}td{text-align:left}img{border:0}a{color:#00c}a:active{color:#f60}.bg{background-image:url(http://s1.bdstatic.com/r/www/cache/static/global/img/icons_2df80e9d.png);background-repeat:no-repeat;_background-image:url(http://s1.bdstatic.com/r/www/cache/static/global/img/icons_b5457670.gif)}.bg_tuiguang_browser{width:16px;height:16px;background-position:-600px 0;display:inline-block;vertical-align:text-bottom;font-style:normal;overflow:hidden;margin-right:5px}.bg_tuiguang_browser_big{width:56px;height:56px;position:absolute;left:10px;top:10px;background-position:-600px -24px}
.bg_tuiguang_weishi{wid*********************************


 



0 0