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

Java网络编程基础学习

2014-09-06 10:58 417 查看
1、网络编程概述

Java是Internet上的语言,它从语言级上提供了对网络应用程序的支持,程序员能够很容易开发常见的网络应用程序
Java提供的网络类库,可以实现无痛的网络连接,互联网的底层细节被隐藏在Java的本机安装系统里,由JVM进行控制。并且Java实现了一个跨平台的网络库,程序员面对的是一个统一的网络编程环境。

2、如何实现网络中的主机互相通信:

通信双方地址

一定的规则(有两套参考模型):

OSI参考模型:模型过于理想化,未能在因特网上进行广泛推广(把网络分为七层,每层有每层的协议)

TCP/IP参考模型(或TCP/IP协议):事实上的国际标准





3、通讯要素1:
① IP 和 端口号
IP:InetAddress
唯一的标识Internet上的计算机
本地回环地址(hostAddress):127.0.0.1 主机名(hostName):localhost
不易记忆
② 端口号标识正在计算机上运行的进程(程序)
不同的进程会有不同的端口号
被规定为一个16位的整数0~65535.其中0~1023被预先定义的服务通信占用(如MySQL占用端口3306,http占用端口80等)。除非我们需要访问这些特定服务,否则,就应该使用1024~65535这些端口号中的某一个进行通信,以免发生端口冲突。
注:端口号与IP地址的组合得出一个网络套接字
通讯要素2:网络通信协议:
① 网络通信协议:计算机网络中实现通信必须有一些约定,即通信协议,对速率、传输代码、代码结构、传输控制步骤、出错控制等制定标准
② 通信协议分层的思想:由于结点之间联系复杂,在制订协议时,把复杂成分分解成一些简单的成分,再将它们复合起来。最常用的复合方式是层次方式,即同层间可以通信、上层可以调用下层,而与再下一层不发生关系。各层互不影响,有利于系统的开发和扩展
4、InetAddress:位于java.net包下:
① InetAddress用于代表IP地址,一个InetAddress的对象代表一个IP地址
② 如何创建InetAddress的对象:getByName(String host)
③ getHostName() : 获取IP地址对应 的域名
getHostAddress() : 获取IP地址
package com.net;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.junit.Test;

public class TestInetAddress {
@Test
public void test1(){
InetAddress ia = null;
try {
ia = InetAddress.getByName("www.baidu.com");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(ia);
System.out.println(ia.getHostAddress());
System.out.println(ia.getHostName());
}
}


运行结果:



5、TCP/IP协议簇:
传输层协议中有两个非常重要的协议:
传输控制协议TCP
永固数据报协议UDP
TCP/IP以其两个主要协议:传输控制协议(TCP)和网络互联协议(IP)而得名,实际上十一组协议,包括多个具有不同功能且互为关联的协议
IP协议是网络层的主要协议,支持网间互连的数据通信
TCP/IP协议模型从更实用的角度出发,形成了高效的四层体系结构,即物理链路层、IP层、传输层和应用层
6、TCP和UDP
TCP协议:

使用TCP协议前,须先建立TCP连接,形成传输数据通道
传输前,采用“三次握手”方式,是可靠地
TCP协议进行通信的两个应用进程:客户端、服务端
在连接中可进行大数据量的传输
传输完毕,需释放已建立的连接,效率低

UDP协议:

将数据、源、目的封装成数据包,不需要建立连接
每个数据报的大小限制在64K内
因无需连接、故是不可靠的
发送数据结束时无需释放资源,速度快

7、Socket

利用套接字(Socket)开发网络应用程序早已经被广泛的采用,以至于成为事实上的标准
通信的两端都要有Socket,是两台机器间通信的短点
网络通信其实就是Socket间的通信
Socket允许程序把网络连接当成一个流,数据在两个Socket间通过IO传输
一般主动发起通信的应用程序属客户端,等待通信请求的为服务端

8、TCP编程

package com.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import org.junit.Test;

public class TestSocket {
@Test
public void client(){
Socket socket = null;
OutputStream out = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
out = socket.getOutputStream();
out.write("我是客户端".getBytes());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
out.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Test
public void server(){
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
try {
ss = new ServerSocket(9090);
s = ss.accept();
is = s.getInputStream();
byte[] b = new byte[20];
int len;
while((len=is.read(b))!=-1){
String str = new String(b,0,len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
is.close();
s.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


运行结果:



客户端:

① 创建一个Socket的对象,通过构造器指明服务端的IP地址,以及其接收程序的端口号

② getOutputStream() : 发送数据,方法返回OutputStream的对象

③ 具体的输出过程

④ 关闭响应的流和Socket对象

服务端:

① 创建一个ServerSocket的对象,通过构造器指明自身的端口号

② 调用其accept() 方法,返回一个Socket的对象

③ 调用Socket对象的getInputStream()获取一个从客户端发送过来的输入流

④ 对获取的输入流进行操作

⑤ 关闭相应的流以及ServerSocket和Socket对象

注:网络编程实际上也是Socket编程

9、TCP编程2.交互

package com.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import org.junit.Test;

public class TestSocket {
@Test
public void client(){
Socket socket = null;
OutputStream out = null;
InputStream in =null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
out = socket.getOutputStream();
out.write("我是客户端".getBytes());
//由于InputStream中的read()是阻塞方法,无法确认客户端发完请求,
//所以需要显示的说明客户端发完请求,使用socket.shutdownOutput()
socket.shutdownOutput();

in = socket.getInputStream();
byte[] b = new byte[20];
int len;
while((len = in.read(b))!=-1){
String str =  new String(b,0,len);
System.out.println(str);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
out.close();
socket.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Test
public void server(){
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
OutputStream out = null;
try {
ss = new ServerSocket(9090);
s = ss.accept();
is = s.getInputStream();
byte[] b = new byte[20];
int len;
while((len=is.read(b))!=-1){
String str = new String(b,0,len);
System.out.println(str);
}
System.out.println(InetAddress.getLocalHost().getAddress().toString());
out = s.getOutputStream();
out.write("我已收到".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
is.close();
s.close();
ss.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
运行结果:

客户端:



服务器端:



10、客户端从本地发一个文件给服务端,服务端接收到反馈信息

package com.net;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import org.junit.Test;

public class TestSocketFile {

@Test
public void client(){
Socket s = null;
OutputStream os = null;
FileInputStream fis = null;
InputStream is = null;
try {
s = new Socket(InetAddress.getByName("127.0.0.1"),2222);
os = s.getOutputStream();
fis = new FileInputStream(new File("C:\\11.jpg"));
byte[] b = new byte[1024];
int len;
while((len=fis.read(b))!=-1){
os.write(b, 0, len);
}
s.shutdownOutput();
is = s.getInputStream();
while((len=is.read(b))!=-1){
System.out.println(new String(b,0,len));
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fis.close();
os.close();
is.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}

@Test
public void server(){
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
FileOutputStream fos = null;
OutputStream os = null;
try {
ss=new ServerSocket(2222);
s = ss.accept();
is = s.getInputStream();
fos = new FileOutputStream(new File("2.jpg"));
byte[] b = new byte[1024];
int len;
while((len=is.read(b))!=-1){
fos.write(b, 0, len);
}
//			s.shutdownInput();
os = s.getOutputStream();
os.write("您发送的图片可以接收成功!!!".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fos.close();
is.close();
s.close();
ss.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


11、UDP网络通信

类DatagramSocket 和 DatagramPacket实现了基于UDP协议网络程序
UDP数据报通过数据报套接字DatagramSocket发送和接收,系统不保证UDP数据报一定能够安全送到目的地,也不能确定什么时候可以抵达
DatagramPacket对象封装了UDP数据报,在数据报中包含了发送端的IP地址和端口号以及接收端的IP地址和端口号
UDP协议中每个数据报都给出了完整的地址信息,因此无须建立发送方和接收方的连接

12、UDP网络通信

流程:

① DatagramSocket 与 DatagramPacket

② 建立发送端,接收端

③ 建立数据包

④ 调用Socket的发送。接收方法

⑤ 关闭Socket

发送端与接收端是两个独立的运行程序

13、UDP编程1

package com.net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.junit.Test;

public class TestUDP {

//发送端
@Test
public void send(){
DatagramSocket ds = null;
DatagramPacket dp = null;
try {
ds = new DatagramSocket();
byte[] b = "您好,是我发送的数据...".getBytes();
/*
* 创建一个数据报,每一个数据报不能大于64K,每一个都记录着数据信息,以及发送端的IP,
* 端口号,以及要发送到的接收端IP以及端口号
*/
dp = new DatagramPacket(b, 0, b.length, InetAddress.getByName("127.0.0.1"),9090);
ds.send(dp);
} catch (SocketException e) {
e.printStackTrace();
}catch (UnknownHostException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
ds.close();
}

}

//接收端
@Test
public void receive(){
DatagramSocket ds =null;
DatagramPacket dp = null;
try {
ds = new DatagramSocket(9090);
byte[] b = new byte[1024];
dp = new DatagramPacket(b, 0, b.length);
ds.receive(dp);
String str = new String(dp.getData(),0,dp.getLength());
System.out.println(str);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
ds.close();
}

}
}


运行结果



14、URL编程

URL(Uniform Resource Locator):统一资源定位符,他表示Internet上某一资源的地址。通过URL我们可以访问Internet上的各种网络资源,比如最常见的www,ftp站点。浏览器通过解析给定的URL可以再网络上查找相应的文件或其他资源

URL的基本结构由5部分组成 <传输协议>://<主机名>:<端口号>/<文件名>

15、URL编程

类URL的构造方法都声明抛出非运行时异常,必须要对这一异常进行处理,通常是用try-catch语句进行捕获

一个URL对象生成后,其属性是不能改变的,但可以通过它给定的方法来获取这些属性:

public String getProtocol() 获取该URL的协议名
public String getHost() 获取该URL的主机名
public String getPort() 获取该URL的端口号
public String getPath() 获取该URL的文件路径
public String getFile() 获取该URL的文件名
public String getRef() 获取该URL在文件中的相对位置
public String getQuery() 获取该URL的查询名

注:URL:统一资源定位符,一个URL的对象,对应着互联网上的一个资源

我们可以通过URL的对象调用其相应的方法,将此资源读取(”下载“)

16、针对HTTP协议的URLConnection类

URL的方法openStream() :能从网络上读取数据
若希望输出数据,例如向服务器端的CGI(公共网管接口-Common Gateway Interface的简称,是用户浏览器和服务器端的应用程序进行连接的接口)程序发送一些数据,则必须先与URL建立连接,然后才能对其进行读写,此时需要使用URLConnection。
URLConnection:表示到URL所引用的远程对象的连接。当与一个URL建立连接时,首先要在一个URL对象上通过方法openConnection()生成对应的URLConnection对象。如果连接过程失败,将产生IOException。

URL netchinaren = new URL("http://210.30.208.140/%282uvldc55n2bp0x55z1ngftme%29/logo/login_pic.png");
URLConnection u = netchinaren.openConnection();

17、URLConnection类

通过URLConnection对象获取的输入流和输出流,即可以与现有的CGI程序进行交互

public Object getContent() throws IOException
public int getContentLength()
public String getContentType()
public long getDate()
public long getLastModified()
public InputStream getInputStream() throws IOException
public OutputStream getOutputStream() throws IOException

18、使用URL从网络上下载图片(使用openStream)

package com.net;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.junit.Test;

public class TestURL {
@Test
public void test(){
InputStream is = null;
FileOutputStream os = null;
try {
URL url = new URL("http://210.30.208.140/%282uvldc55n2bp0x55z1ngftme%29/logo/login_pic.png");
is = url.openStream();
os = new FileOutputStream(new File("xx.png"));
byte[] b = new byte[1024];
int len;
while((len=is.read(b))!=-1){
os.write(b, 0, len);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


19、使用URL从网络上下载图片(使用URLConnection)

package com.net;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.junit.Test;

public class TestURL1 {
@Test
public void test(){
InputStream is =null;
FileOutputStream os =null;
URLConnection con = null;
try {
URL url = new URL("http://210.30.208.140/%282uvldc55n2bp0x55z1ngftme%29/logo/login_pic.png");
con = url.openConnection();
is = con.getInputStream();
os = new FileOutputStream(new File("ss.png"));
byte[] b = new byte[1024];
int len;
while((len=is.read(b))!=-1){
os.write(b, 0, len);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
}


20、要想实现网络传输,需要考虑的问题

① 如何才能准确的定位网络上的一台主机

②如何才能进行可靠地、高效的数据传输

21、Java如何实现的网络通信

① 使用IP地址--定位一台主机 使用端口--定位一个应用 ====》InetAddress类

》如何创建一个InetAddress的对象?getByName("") 比如:InetAddress inet = InetAddress.getByName("192.168.90.165")

》如何获取本机的一个InetAddress的IP地址?InetAddress.getHostName()

》如何获取本机的一个InetAddress的域名? InetAddress.getHostAddress()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: