Oracle培训(十五)——Core Java第十五章知识点总结——网络编程
Core Java第十五章知识点总结——网络编程
知识点预览
网络应用的结构
网络中的基本概念
TCP编程
UDP编程
URL编程
网络应用的结构
1. 网络应用的结构
2. IP地址
a) 概念:用于唯一识别连接到Internet上的计算机
b) 地址表示形式
i. IPV4:点分十进制表示方式,四个整数用点分开,每个十进制数字代表一个8bits整数 如 192.168.123.1
ii. IPV6:点分十六进制表示方式,八个整数用点分开,每个十六进制数字代表一个16bits整数 如:CDCD:910A:2222:5498:8475:1111:3900:2020
3.域名 便于记忆 由DNS(域名服务器)进行解析
4.本地IP 127.0.0.1等同于localhost
5.端口号 数字 计算机网络中的计算机唯一标识一个软件(或者说进程)
6.协议
TCP编程
1.TCP协议简介
TCP协议是一个有连接、可靠的协议。
类似于打电话
可靠:数据不会丢失,接收方接收的数据包有序
2. TCP编程
java.net.Socket//套接字
java.net.ServerSocket
3. TCP编程的核心思路
a)开发服务器端
ServerSocket ss = newServerSocket();
ss.accept();
b) 开发客户端
Socket s = new Socket(“150.236.56.101”,9000);
4.总结
a)服务器端
i. 创建ServerSocket对象(并绑定端口)
ii.调用accept方法,等待客户端的连接
iii.调用getXXXStream,并进行I/O
iv.关闭Socket
b) 客户端
i.创建Socket对象,并连接服务器
ii. 调用getXXXStream,并进行I/O
iii.关闭Socket
5. 具体示例
a) 客户端发送一个helloworld串
package chp15.ex01;
import java.io.PrintWriter;
import java.net.Socket;
/**
*
* @Author: Wentasy
* @FullName: TCPClient.java
* @Description: TCP客户端的创建
* @Create Date: 2012-8-20
*/
public class TCPClient {
public static void main(String[] args) throws Exception {
//创建Socket对象(并连接服务器)
Socket s = new Socket("127.0.0.1", 9001);
//调用getXXXStream方法,进行I/O
PrintWriter pw = new PrintWriter(s.getOutputStream());
pw.println("hello world");
pw.flush();
//关闭
s.close();
}
}
b) 服务器端接受字符串并打印
package chp15.ex01;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @Author: Wentasy
* @FullName: TCPServer.java
* @Description: TCP服务器端的创建
* @Create Date: 2012-8-20
*/
public class TCPServer {
public static void main(String[] args) throws Exception {
//创建ServerSocket对象(并绑定端口)
ServerSocket ss = new ServerSocket(9001);
//调用accept方法
Socket s = ss.accept();
//调用getXXXStream方法,进行I/O
BufferedReader br =
new BufferedReader(
new InputStreamReader(s.getInputStream())
);
String line = br.readLine();
System.out.println(line);
//关闭Socket
s.close();
}
}
c)客户端发送一个helloworld串,并接受服务器端回传的from serve helloworld
package chp15.ex02;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
*
* @Author: Wentasy
* @FullName: TCPClient.java
* @Description: TCP 客户端 完成读写
* @Create Date: 2012-8-20
*/
public class TCPClient {
public static void main(String[] args) throws Exception {
//创建Socket对象(并连接服务器)
Socket s = new Socket("127.0.0.1", 9000);
//调用getXXXStream方法,进行I/O
PrintWriter pw = new PrintWriter(s.getOutputStream());
pw.println("hello world");
pw.flush();
BufferedReader br = new BufferedReader(
new InputStreamReader(s.getInputStream())
);
String line = br.readLine();
System.out.println(line);
//关闭Socket
s.close();
}
}
d)服务器端回传的from serve helloworld
package chp15.ex02;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @Author: Wentasy
* @FullName: TCPServer.java
* @Description: TCP服务器端 完成读写
* @Create Date: 2012-8-20
*/
public class TCPServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(9000);
Socket s = ss.accept();
BufferedReader br =
new BufferedReader(
new InputStreamReader(s.getInputStream())
);
String line = br.readLine();
PrintWriter pw = new PrintWriter(
s.getOutputStream());
pw.println("from server" + line );
pw.flush();
s.close();
}
}
6. 程序存在的问题:服务器只能运行一次
while(true){
调用accept()方法
调用getXXXStream方法,进行I/O
关闭Socket
}
package chp15.ex03;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @Author: Wentasy
* @FullName: TCPServer.java
* @Description: TCP 服务器端 带多线程
* @Create Date: 2012-8-20
*/
class ServerThread extends Thread{
private Socket s;
public ServerThread(Socket s) {
this.s = s;
}
public void run(){
try{
BufferedReader br =
new BufferedReader(
new InputStreamReader(s.getInputStream())
);
String line = br.readLine();
PrintWriter pw = new PrintWriter(
s.getOutputStream());
pw.println(line + " from server");
pw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{try {s.close();} catch (IOException e) {e.printStackTrace();}
}
}
}
public class TCPServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(9000);
while(true){
Socket s = ss.accept();
Thread st = new ServerThread(s);
st.start();
}
}
}
package chp15.ex03;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
*
* @Author: Wentasy
* @FullName: TCPClient.java
* @Description: TCP 客户端
* @Create Date: 2012-8-20
*/
public class TCPClient {
public static void main(String[] args) throws Exception {
//创建Socket对象(并连接服务器)
Socket s = new Socket("127.0.0.1", 9000);
//调用getXXXStream方法,进行I/O
PrintWriter pw = new PrintWriter(s.getOutputStream());
pw.println("hello world");
pw.flush();
BufferedReader br = new BufferedReader(
new InputStreamReader(s.getInputStream())
);
String line = br.readLine();
System.out.println(line);
//关闭Socket
s.close();
}
}
UDP编程
1.UDP协议简介
UDP是一个无连接,不可靠的协议。即:数据的发送方只负责将数据发送出去,数据的接收方值负责接收数据。发送方和接收方不会相互确认数据的传输是否成功。
存在的意义:效率高,在实时性较强的场合适用。
示例:发短信
2.UDP编程
java.net.DatagramSocket(想象成传真机)
java.net.DatagramPacket(想象成传真机上的纸)
3.UDP编程的核心思路
a) 服务器端 DatagramSocket socket = new DatagramSocket(9000);
b) 客户端 DatagramSocket socket = new DatagramSocket();
客户端不用端口号
端口号和协议相关 本机TCP和UDP可以使用相同的端口号
4. DatagramPacket
DatagramPacket(byte[] buf, int offset,int length);
DatagramPacket(byte[] buf, int offset,int length,SocketAddress);
SocketAddress:接口 使用时用它的实现类 InetSocketAddress
getAddress()/getSoketAddress()/getPort()/getLength()
setAddress()/setSoketAddress()/setPort()/setLength()
package chp15.ex05;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
*
* @Author: Wentasy
* @FullName: UDPServer.java
* @Description: UDP 服务器端 完成读写
* @Create Date: 2012-8-20
*/
public class UDPServer {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(9000);
byte[] buf = new byte[100];
DatagramPacket paper = new DatagramPacket(
buf, 0, buf.length);
socket.receive(paper);
String str = new String(buf, 0 , paper.getLength());
System.out.println(str);
byte[] data = "hello client".getBytes();
DatagramPacket packet = new DatagramPacket(
data, 0 ,data.length,
paper.getSocketAddress()
);
socket.send(packet);
socket.close();
}
}
package chp15.ex05;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
/**
*
* @Author: Wentasy
* @FullName: UDPClient.java
* @Description: UDP 客户端 完成读写
* @Create Date: 2012-8-20
*/
public class UDPClient {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
String str = "hello server";
byte[] data = str.getBytes();
DatagramPacket packet = new DatagramPacket(
data, 0, data.length,
new InetSocketAddress("127.0.0.1", 9000)
);
socket.send(packet);
byte[] buf = new byte[100];
DatagramPacket paper = new DatagramPacket(
buf, 0, buf.length
);
socket.receive(paper);
String msg = new String(buf, 0, paper.getLength());
System.out.println(msg);
socket.close();
}
}
URL编程
1. URL概念
URL统一资源定位符——互联网中唯一定位一个资源
2. URL组成
协议名://主机名(或者IP地址):端口号/资源
http://localhost:8080/web/a.jpg
http://www.oracle.com/index.html
3. URL编程
a) URL
b)URLConnection
4. URL编程思路
a) 创建URL对象
b) 调用URL对象的openConnection方法获得URLConnection对象
c) 调用URLConnection对象的getInputStream方法,获得输入流,从而获得资源
d) I/O操作
e) 关闭资源
package chp15.ex06;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
/**
*
* @Author: Wentasy
* @FullName: TestURL.java
* @Description: URL 编程
* a)创建URL对象
b)调用URL对象的openConnection方法获得URLConnection对象
c)调用URLConnection对象的getInputStream方法,获得输入流,从而获得资源
d)I/O操作
e)关闭资源
* @Create Date: 2012-8-20
*/
public class TestURL {
public static void main(String[] args) throws Exception {
//创建URL对象
URL url = new URL("http://www.oracle.com");
//调用URL对象的openConnection方法,获得URLConnection
URLConnection conn = url.openConnection();
//调用URLConnection方法的getInputStream
InputStream in = conn.getInputStream();
//I/O操作
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
String line = null;
while( (line=br.readLine()) != null ){
System.out.println(line);
}
br.close();
}
}