10 网络编程
1、 网络模型
OSI参考模型
TCP/IP参考模型
TCP/IP参考模型 | OSI参考模型 | ||||||||
应用层 | 应用层 | ||||||||
表示层 | |||||||||
会话层 | |||||||||
传输层 | 传输层 | ||||||||
网络层 | 网际层 | ||||||||
数据链路层 | 主机网络层 | ||||||||
物理层 |
2、网络通讯要素
IP地址:网络中设备的标示。
端口号:用于标示进程的逻辑地址、不同进程的标示。
传输协议:通讯的规则。常见协议:TCP、UDP。
3、TCP和UDP
TCP
建立连接,形成传输数据的通道
在链接中进行大量数据传输
通过三次握手完成连接,是可靠协议
必须建立连接,效率会降低
UDP
将数据及源和目的封装在数据包中,不需要建立连接
每个数据报的大小限制在64K内
因无连接,是不可靠协议
不需要建立连接,速度快
4、获取主机的IP地址和名称
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IPDemo {
public static void main(String[] args) throws UnknownHostException {
InetAddress i = InetAddress.getLocalHost();
//InetAddress i = InetAddress.getByName("192.168.0.101");
System.out.println(i.toString());
System.out.println("名字:"+i.getHostName());
System.out.println("地址:"+i.getHostAddress());
}
}
10.1 Socket
Socket就是为网络服务提供的一种机制
通信的两端都有Socket
网络通信其实就是Socket间的通信
数据在两个Socket间通过IO传输
10.1.1 UDP传输
DatagramSocket与DatagramPacket
建立发送端、接收端
建立数据包
调用Socket的发送接收方法
关闭Socket
发送端和接收端是两个独立的运行程序
1、通过UDP传输方式,将一段数据发送出去
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPSend {
public static void main(String[] args) throws IOException {
DatagramSocket ds =new DatagramSocket(8888);
byte[] b = "adasfsdklgjfsdl".getBytes();
DatagramPacket dp =
new DatagramPacket(b,b.length,InetAddress.getByName("192.168.0.100"), 1000);
ds.send(dp);
}
}
2、接收UDP协议传输的数据并处理
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPRes {
public static void main(String[] args) throws IOException {
DatagramSocket ds =new DatagramSocket(1000);
//定义数据包,用于存储数据
byte[] b = new byte[1024];
DatagramPacket dp =new DatagramPacket(b, b.length);
//通过receive方法将接收到的数据存入数据包中
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(), 0, dp.getLength());
int port = dp.getPort();
System.out.println("ip="+ip+"\n"+"data="+data+"\n"+"port="+port);
ds.close();
}
}
3、通过UDP协议,持续发送和接收数据
发送:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPSend2 {
public static void main(String[] args) throws IOException {
DatagramSocket ds =new DatagramSocket();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line=br.readLine())!=null) {
if (line.equals("886")) {
break;
}
byte[] b = line.getBytes();
DatagramPacket dp =new DatagramPacket(b, b.length, InetAddress.getByName("192.168.0.100"), 1000);
ds.send(dp);
}
ds.close();
}
}
接收:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPRes2 {
public static void main(String[] args) throws IOException {
DatagramSocket ds =new DatagramSocket(1000);
while(true){
byte[] b = new byte[1024];
DatagramPacket dp =new DatagramPacket(b, b.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(), 0, dp.getLength());
int port = dp.getPort();
System.out.println("ip="+ip+"\n"+"data="+data+"\n"+"port="+port);
}
}
}
10.1.2 TCP传输
Socket和ServerSocket
建立客户端和服务器端
建立连接后,通过Socket中的IO流进行数据的传输
关闭Socket
客户端和服务器端是两个独立的运行程序
1、TCP传输
客户端:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket s = new Socket("192.168.0.100", 1001);
OutputStream os = s.getOutputStream();
os.write("qweqwrqwr".getBytes());
InputStream is = s.getInputStream();
byte[] b =new byte[1024];
int len = is.read(b);
System.out.println(new String(b, 0, len));
s.close();
}
}
服务器端:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket sso = new ServerSocket(1001);
Socket so = sso.accept();
String ip = so.getInetAddress().getHostAddress();
System.out.println("ip="+ip);
InputStream is = so.getInputStream();
byte[] b = new byte[1024];
int len = is.read(b);
System.out.println(new String(b, 0, len));
OutputStream os = so.getOutputStream();
os.write("adasfsdgfdg".getBytes());
so.close();
sso.close();
}
}
2、客户端给服务端发送数据,服务端接收后给客户端反馈信息
客户端:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket s = new Socket("192.168.0.100", 1001);
BufferedReader bw = new BufferedReader(new FileReader("1.txt"));
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
String line = null;
while((line=bw.readLine())!=null){
pw.println(line);
}
s.shutdownOutput();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String res = in.readLine();
System.out.println(res);
s.close();
}
}
服务器端:
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket sso = new ServerSocket(1001);
Socket so = sso.accept();
String ip = so.getInetAddress().getHostAddress();
System.out.println("ip="+ip);
BufferedReader br = new BufferedReader(new InputStreamReader(so.getInputStream()));
PrintWriter pw =new PrintWriter(new FileWriter("2.txt"),true);
String s = null;
while((s=br.readLine())!=null){
pw.println(s);
}
PrintWriter res = new PrintWriter(so.getOutputStream(),true);
res.println("上传成功!");
pw.close();
so.close();
sso.close();
}
}
3、自定义简单浏览器
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ExploreDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(1100);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress());
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
InputStream is= s.getInputStream();
byte[] b = new byte[1024];
int len =is.read(b);
System.out.println(new String(b, 0, len));
pw.println("客户端你好");
s.close();
ss.close();
}
}
10.1.3 URL
1、方法:
String getProtocol();获取协议名称
String getPath();获取URL路径
String getHost();获取URL的主机名
String getPort();获取URL的端口号
String getFile();获取URL的文件名
String getQuery();获取URL的查询部分
2、获取URL的相关信息
import java.net.MalformedURLException;
import java.net.URL;
public class URLDemo {
public static void main(String[] args) throws MalformedURLException {
URL u=new URL("http://192.168.1.11:8080/myweb/text.html");
System.out.println("getProtocol:"+u.getProtocol());
System.out.println("getPath:"+u.getPath());
System.out.println("getHost:"+u.getHost());
System.out.println("getPort:"+u.getPort());
System.out.println("getFile:"+u.getFile());
System.out.println("getQuery:"+u.getQuery());
}
}
3、通过URL获取信息
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class URLConnectionDemo {
public static void main(String[] args) throws IOException {
URL u=new URL("https://www.baidu.com/");
URLConnection uc = u.openConnection();
InputStream is = uc.getInputStream();
byte[] b =new byte[1024*1024*10];
int len = is.read(b);
System.out.println(new String(b, 0, len));
}
}