目录
一.?IP和端口号
? ? ? 1.?IP
? ? ? 2. 端口号
二.?网络协议
? ? ? 1.TCP/IP
?(1)客户端发送数据给服务器
?(2)客户端发送文件给服务器,服务器将文件保存到本地
?(3)客户端发送文件给服务器,服务器将文件保存到本地,并返回“发送成功”给? ?客户端
? ? ? 2.UDP/IP
三.?URL编程
网络编程的两个要素:① IP和端口号;② 提供网络通信协议
一.?IP和端口号
? ? ? 1.?IP
? ? ? ? ? ①IP:唯一标识Internet上的计算机;
? ? ? ? ? ②域名:www.baidu.com
? ? ? ? ? ③本地地址(localhost):172.0.0.1
? ? ? ? ? ④在Java中使用InetAddress类来代表IP,实例化InetAddress类:getByName(String host)、getLocalHost()
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Test{
public static void main(String[] args){
try {
InetAddress address1 = InetAddress.getByName("192.168.10.14");
System.out.println(address1); ///192.168.10.14
InetAddress address2 = InetAddress.getByName("www.baidu.com");
System.out.println(address2); //www.baidu.com/14.215.177.38
InetAddress address3 = InetAddress.getLocalHost();
System.out.println(address3);
//getHostName():获取域名
System.out.println(address2.getHostName()); //www.baidu.com
//getHostAddress():获取地址
System.out.println(address2.getHostAddress()); //14.215.177.39
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
? ? ? 2. 端口号
? ? ? ? ? 端口号:标识计算机上正在运行的进程,不同的进程有不同的端口号,如Tomcat的端口号是8080,MySQL端口号是3306。
? ? ? ? ? 端口号与IP地址组合得到网络套接字:Socket
二.?网络协议
? ? ? TCP:①使用TCP协议之前,需要先建立TCP连接,形成数据传输通道;②采用“三次握手”方式;③TCP协议用于客户端和服务器之间进行通信;④可以进行大量数据传输;⑤传输完毕后,需要释放已建立的连接,效率低。
? ? ? UDP:①将数据、源封装成数据包,不需要建立连接;②发送时不需要确定对方是否准备好,并且接收方收到后不需要确认;③可以广播发送;④每个数据包大小限制在64KB;⑤传输完毕后,不需要释放资源,开销小,速度快。
? ? ? 1.TCP/IP
? ? ? ? (1)客户端发送数据给服务器
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPTest {
//客户端
@Test
public void client() {
InetAddress address = InetAddress.getByName("10.177.205.186"); //指明服务器的IP
Socket socket = new Socket(address,8888); //指明服务器的端口号
OutputStream os = socket.getOutputStream();
os.write("Hello".getBytes());
os.close();
socket.close();
}
//服务器
@Test
public void server(){
ServerSocket serverSocket = new ServerSocket(8888);
Socket socket1 = serverSocket.accept();
InputStream is = socket1.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[20];
int len = is.read(buffer);
while(len != -1){
baos.write(buffer,0,len);
len = is.read(buffer);
}
System.out.println(baos.toString());
baos.close();
is.close();
socket1.close();
serverSocket.close();
}
}
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPTest {
//客户端
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
try {
InetAddress address = InetAddress.getByName("10.177.205.186");
socket = new Socket(address, 8888); //1.创建客户端的Socket,指明服务器的IP和端口号
os = socket.getOutputStream(); //2.获取一个输出流,用于输出数据
os.write("Hello".getBytes()); //3.写出数据(发送给客户端的数据)
} catch (IOException e) {
e.printStackTrace();
} finally { //4.关闭
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务器
@Test
public void server(){
ServerSocket serverSocket = null;
Socket socket1 = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
serverSocket = new ServerSocket(8888); //1.创建服务器的ServerSocket,指明自己的端口号
socket1 = serverSocket.accept(); //2.调用accept()用于接收来自客户端的socket
is = socket1.getInputStream(); //3.获取输入流
baos = new ByteArrayOutputStream(); //4.获取输入流中的数据
byte[] buffer = new byte[20];
int len = is.read(buffer);
while (len != -1) {
baos.write(buffer, 0, len);
len = is.read(buffer);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally { //5.关闭
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket1 != null) {
try {
socket1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
? ? ? ? (2)客户端发送文件给服务器,服务器将文件保存到本地
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPTest {
//客户端
@Test
public void client() {
InetAddress address = InetAddress.getByName("10.177.205.186");
Socket socket = new Socket(address,8888);
OutputStream os = socket.getOutputStream();
File file = new File("test1.jpg");
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = fis.read(buffer);
while(len != -1){
os.write(buffer,0,len);
len = fis.read(buffer);
}
fis.close();
os.close();
socket.close();
}
//服务器
@Test
public void server(){
ServerSocket serverSocket = new ServerSocket(8888);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
File file = new File("test2.jpg");
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = is.read(buffer);
while(len != -1){
fos.write(buffer,0,len);
len = is.read(buffer);
}
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPTest {
//客户端
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
try {
InetAddress address = InetAddress.getByName("10.177.205.186");
socket = new Socket(address,8888);
os = socket.getOutputStream();
File file = new File("test1.jpg");
fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = fis.read(buffer);
while(len != -1){
os.write(buffer,0,len);
len = fis.read(buffer);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务器
@Test
public void server(){
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
try {
serverSocket = new ServerSocket(8888);
socket = serverSocket.accept();
is = socket.getInputStream();
File file = new File("test2.jpg");
fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = is.read(buffer);
while(len != -1){
fos.write(buffer,0,len);
len = is.read(buffer);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
? ? ? ? (3)客户端发送文件给服务器,服务器将文件保存到本地,并返回“发送成功”给客户端
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPTest {
//客户端
@Test
public void client() {
InetAddress address = InetAddress.getByName("10.177.205.186");
Socket socket = new Socket(address,8888);
OutputStream os = socket.getOutputStream();
File file = new File("test1.jpg");
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = fis.read(buffer);
while(len != -1){
os.write(buffer,0,len);
len = fis.read(buffer);
}
socket.shutdownOutput(); //关闭数据的输出
//接收来自服务器的数据
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer1 = new byte[1024];
int len1 = is.read(buffer1);
while(len1 != -1){
baos.write(buffer1,0,len1);
len1 = is.read(buffer1);
}
System.out.println(baos.toString());
baos.close();
is.close();
fis.close();
os.close();
socket.close();
}
//服务器
@Test
public void server(){
ServerSocket serverSocket = new ServerSocket(8888);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
File file = new File("test2.jpg");
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = is.read(buffer);
while(len != -1){
fos.write(buffer,0,len);
len = is.read(buffer);
}
//向客户端发送数据
OutputStream os = socket.getOutputStream();
os.write("文件已收到");
os.close();
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPTest {
//客户端
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
InetAddress address = InetAddress.getByName("10.177.205.186");
socket = new Socket(address,8888);
os = socket.getOutputStream();
File file = new File("test1.jpg");
fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = fis.read(buffer);
while(len != -1){
os.write(buffer,0,len);
len = fis.read(buffer);
}
socket.shutdownOutput(); //关闭数据的输出
//接收来自服务器的数据
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer1 = new byte[1024];
int len1 = is.read(buffer1);
while(len1 != -1){
baos.write(buffer1,0,len1);
len1 = is.read(buffer1);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if(baos != null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务器
@Test
public void server(){
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
OutputStream os = null;
try {
serverSocket = new ServerSocket(8888);
socket = serverSocket.accept();
is = socket.getInputStream();
File file = new File("test2.jpg");
fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = is.read(buffer);
while(len != -1){
fos.write(buffer,0,len);
len = is.read(buffer);
}
//向客户端发送数据
os = socket.getOutputStream();
os.write("文件已收到".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
? ? ? 2.UDP/IP
import org.junit.Test;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPTest {
//客户端
@Test
public void client() {
DatagramSocket socket = new DatagramSocket();
String str = "Hello";
byte[] data = str.getBytes();
InetAddress address = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data,0,data.length,address,8888);
socket.send(packet);
socket.close();
}
//服务器
@Test
public void server(){
DatagramSocket socket = new DatagramSocket(8888);
byte[] buffer = new byte[100];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0, packet.getLength()));
socket.close();
}
}
import org.junit.Test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPTest {
//客户端
@Test
public void client() {
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
String str = "Hello";
byte[] data = str.getBytes();
InetAddress address = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data,0,data.length,address,8888);
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
} finally {
socket.close();
}
}
//服务器
@Test
public void server(){
DatagramSocket socket = null;
try {
socket = new DatagramSocket(8888);
byte[] buffer = new byte[100];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0, packet.getLength()));
} catch (IOException e) {
e.printStackTrace();
} finally {
socket.close();
}
}
}
三.?URL编程
? ? ? ? URL:统一资源定位符
????????getProtocol():获取URL的协议名
? ? ? ? getHost():获取URL的主机名
? ? ? ? getPort():获取URL的端口号
? ? ? ? getPath():获取URL的文件路径
? ? ? ? getFile():获取URL的文件名
? ? ? ? getQuery():获取URL的查询名
import java.net.MalformedURLException;
import java.net.URL;
public class Test{
public static void main(String[] args){
try {
URL url = new URL("https://www.baidu.com/");
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getPath());
System.out.println(url.getFile());
System.out.println(url.getQuery());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
|