您的位置:首页 > 理论基础 > 计算机网络

java基础-网络编程

2018-03-02 18:11 393 查看

一.网络编程的基础知识

1。IP协议是一种重要的通讯协议,支持网络间的数据报协议,提供网间连接的完善的功能,包括IP数据报规定互联网络范围内的地址格式。

2。TCP协议:传输控制协议,它规定一种可靠的数据信息传递服务

IP地址和端口号

1。IP地址用于唯一标识网络中一个通信实体。ip地址是数字型的,是一个32位整数,为了便于记忆,将它划分为4个8位的二进制数,每8位之间用圆点分隔,每个8位整数可以转换成一个0-255的十进制整数

2。ip地址分为A、B、C、D、E五类

A类:10.0.0.0 ~ 10.255.255.255

B类:172.16.0.0 ~ 172.31.255.255

C类:192.168.0.0 ~ 192.168.255.255

3.端口:是一个16位的整数标识数据交给那个通信程序处理。端口就是应用程序与外界交流的一个出入口。它是一种抽象的软件结构,包括一些数据结构和 I/O缓冲区。同一个机器上不允许有两个程序使用同一个端口,端口的范围为0-65535

公认端口:从0到1024,他们紧密的绑定一些特定的服务

注册端口:从1024-49151,他们松散的绑定一些服务,应用程序通常使用这个范围的端口。

动态或私有端口:从49152-65535,这些端口是应用程序使用的动态端口,应用程序一般不会主动使用这个范围的端口。

java的基础网络支持

package com.gdy.net;

import java.io.IOException;
import java.net.InetAddress;

public class InetAddressDemo {
public static void main(String[] args) throws IOException {
//根据主机名来获取赌赢的InetAddress实例
InetAddress ip = InetAddress.getByName("www.baidu.com");
System.out.println("ip:"+ip);

//判断是否可达
System.out.println("是否可达:"+ip.isReachable(2000));

//获取该InetAddress实例的ip字符串
System.out.println("实例ip字符串:"+ip.getHostAddress()+" 主机名: "+ip.getHostName() +" "+ ip.getAddress());

//获取原始的ip地址来获取对应的InetAddress实例
InetAddress local = InetAddress.getByAddress(new byte[]{127,0,0,1});
System.out.println("ip是否可达:"+local.isReachable(2000));
System.out.println("获取该IP地址的全限定域名:"+local.getCanonicalHostName());

}
}


ip:www.baidu.com/180.97.33.107
是否可达:true
实例ip字符串:180.97.33.107 主机名: www.baidu.com [B@6e0be858
ip是否可达:true
获取该IP地址的全限定域名:127.0.0.1


URLDecoder和URLEncoder

URLDecoder和URLEncoder用于完成普通的字符串和application/x-www-form-urlencoded MIME字符串之间的相互转换。

application/x-www-form-urlencoded MIME就是下面图片中看似乱码的字符串



package com.gdy.net;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class URLDecoderDemo {
public static void main(String[] args) throws UnsupportedEncodingException {

/**
* 将普通字符串装换成application/x-www-form-urlencoded字符串
*/
String url = "学习java中的net开发";
String url_encode = URLEncoder.encode(url,"utf-8");

System.out.println(url_encode);

/**
* 将application/x-www-form-urlencoded字符串转换成普通字符串
*/
System.out.println(URLDecoder.decode(url_encode));

}
}


%E5%AD%A6%E4%B9%A0java%E4%B8%AD%E7%9A%84net%E5%BC%80%E5%8F%91
学习java中的net开发


URL、URLConnection和URLPermission

URL代表统一资源定位器,指向互联网“资源”的指针,资源可以是简单的目录或文件,亦可以是复杂对象的应用,例如对数据库或者所有引擎的查询。

通常的格式

协议名 主机 端口 资源组成
protocol://host:port/resourceName


URL构造方法

//通过给定的参数(协议、主机名、端口号、文件名)创建URL。
public URL(String protocol, String host, int port, String file) throws MalformedURLException.

//使用指定的协议、主机名、文件名创建URL,端口使用协议的默认端口。
public URL(String protocol, String host, String file) throws MalformedURLException

//通过给定的URL字符串创建URL
public URL(String url) throws MalformedURLException

//使用基地址和相对URL创建
public URL(URL context, String url) throws MalformedURLException


URL常用方法

//将此URL与其他对象进行比较。
boolean equals(Object obj)

//获取此的授权部分 URL 。
String getAuthority()

//获取此URL的内容。
Object getContent()

//获取此URL的内容。
Object getContent(class[] classes)

//获取与此 URL的协议的默认端口号。
int getDefaultPort()

//获取此 URL的文件名。
String getFile()

//获取此 URL的主机名(如适用)。
String getHost()

//获取此 URL的路径部分。
String getPath()

//获取此 URL的端口号。
int getPort()

//获取此 URL的协议名称。
String getProtocol()

//获取此 URL的查询部分。
String getQuery()

//获取此的锚定(也称为“参考”) URL 。
String getRef()

//获取该 URL的userInfo部分。
String getUserInfo()

//创建适合哈希表索引的整数。
int hashCode()

//返回一个URLConnection实例,表示与URL引用的远程对象的URL 。
URLConnection openConnection()

//与openConnection()相同,但连接将通过指定的代理进行; 不支持代理的协议处理程序将忽略代理参数并进行正常连接。
URLConnection openConnection(Proxy proxy)

//打开与此 URL ,并返回一个 InputStream ,以便从该连接读取。
InputStream openStream()

//比较两个URL,不包括片段组件。
boolean sameFile(URL other)

//设置应用程序的 URLStreamHandlerFactory 。
static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac)

//构造这个 URL的字符串 URL 。
String toExternalForm()

//构造此 URL的字符串表示 URL 。
String toString()

//返回相当于此URL的URI 。
URI toURI()


URLConnection方法

//检索URL链接内容
Object getContent()

//检索URL链接内容
Object getContent(Class[] classes)

//返回头部 content-encoding 字段值。
String getContentEncoding()

//返回头部 content-length字段值
int getContentLength()

//返回头部 content-type 字段值
String getContentType()

//返回头部 last-modified 字段值。
int getLastModified()

//返回头部 expires 字段值。
long getExpiration()

//返回对象的 ifModifiedSince 字段值。
long getIfModifiedSince()

//URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输入,则将 DoInput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 true。
public void setDoInput(boolean input)

//URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输出,则将 DoOutput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 false。
public void setDoOutput(boolean output)

//返回URL的输入流,用于读取资源
public InputStream getInputStream() throws IOException

//返回URL的输出流, 用于写入资源。
public OutputStream getOutputStream() throws IOException

//返回 URLConnection 对象连接的URL
public URL getURL()


从网络中获取文件的输入流

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import com.google.gson.JsonParser;

public class ParseURI {

public static String getResult(String url) throws Exception
{
URL yahoo = null;
HttpURLConnection conn = null;
BufferedReader br = null;

String result = "";

try {
yahoo = new URL(url);
conn = (HttpURLConnection) yahoo.openConnection();
//设置超时间为200毫秒
conn.setConnectTimeout(200);
br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
String inputLine = null;
while ((inputLine = br.readLine()) != null) {
result = result.concat(inputLine);
}

} catch (Exception e) {
//e.printStackTrace();
throw e;

} finally {
try {
if(br != null)
{
br.close();
}
if(conn != null)
{
conn.disconnect();
}
} catch (IOException e) {
throw e;
}
}
return result;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: