网络编程两大问题(写一封信给某个人)
-
如何定位网络上的一台或多台主机,定位主机上的应用(写给人的地址)
-
如何进行可靠高效进行数据传输(需要贴什么邮票)
解决问题一:IP和端口号
解决问题二:提供网络通信协议:TCP/IP参考模型
IP和端口
IP地址
1.唯一标识Internet上的计算机(通信实体)
2.在Java中使用InetAddress类代表IP
3.分类方式1:IPV4和IPV6
分类方式2:万维网 和 局域网
4.域名:代替IP,方便记忆 例如:www.baidu.com
5.本地回路地址:120.0.0.1 对应:localhost
6.如何实例化:两种 getByName(String host) getLocalHost()
两个常用方法:getHostName() 获取域名
getHostAddress() 获取地址IP
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Main {
public static void main(String[] args) {
try {
InetAddress byName = InetAddress.getByName("www.baidu.com");
System.out.println(byName.toString());
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
System.out.println(byName.getHostName());
System.out.println(byName.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
端口号
1.标识正在计算机上运行的进程(程序)
2.不同进程要有不同的端口号
3.范围: 16位的整数 0-65535
端口号和IP地址的组合得出一个网络套接字:Socket
协议
TCP协议
三次握手(建立,确保两个人都在 客户端向服务器发消息问在不在,服务器回消息在,客户端回复)
四次挥手(释放,两个人都先对方发了消息说要断开,各自再回一条信息,客户端先,服务器发的时候,客户端回消息只是印证服务器也没有关闭)
可靠,
需要释放连接,效率低
UDP协议
将数据,源,目的封装在数据包
不可靠
速度快,无需释放资源,开销小
TCP
//客户端
@Test
public void client() {
Socket socket = null;
OutputStream outputStream = null;
try {
// 设置IP和端口
InetAddress inetAddress = InetAddress.getByName(InetAddress.getLocalHost().getHostAddress());
socket = new Socket(inetAddress, 8899);
// 获得一个输出流,用于输出数据
outputStream = socket.getOutputStream();
// 输出数据,可以是一句话也可以是文档图片(涉及IO流)
outputStream.write("您好,我是客户端mm".getBytes());
// socket.shutdownOutput();
// 当然也可以从服务器收到消息,那么上面就要加以句
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.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 inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
// 1.创建服务器端的SeverSocket,指明自己的端口号
serverSocket = new ServerSocket(8899);
// 2.用accept(),表示接收来自客户端的socket
socket = serverSocket.accept();
// 3.获取输出流
inputStream = socket.getInputStream();
// 4.读取输入流的数据
byteArrayOutputStream = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[1024];
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer,0,len);
}
System.out.println(byteArrayOutputStream.toString());
// 如果想要给客户端发送点什么
// socket.getOutputStream(),获取输出流,然后在进行操作
} catch (IOException e) {
e.printStackTrace();
} finally {
// 5.关闭
if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.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();
}
}
}
}
UDP
DatagramSocket
DatagramPacket
s@Test
public void sender() throws IOException {
DatagramSocket datagramSocket = new DatagramSocket();
String str = "UDP发送方式";
byte[] bytes = str.getBytes();
InetAddress inetAddress = InetAddress.getLocalHost();
DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length,inetAddress,8899);
datagramSocket.send(datagramPacket);
datagramSocket.close();
}
@Test
public void receiver() throws IOException {
DatagramSocket datagramSocket = new DatagramSocket(8899);
byte[] bytes = new byte[200];
DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length);
datagramSocket.receive(datagramPacket);
System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength()));
datagramSocket.close();
}
URL
统一资源定位符
@Test
public void downLoad() {
HttpsURLConnection urlContent = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
URL url = new URL("https://pic.cnblogs.com/avatar/2183514/20201017110603.png");
// 打开连接
urlContent = (HttpsURLConnection) url.openConnection();
// 连接
urlContent.connect();
// 获得输出流
inputStream = urlContent.getInputStream();
fileOutputStream = new FileOutputStream("1.png");
int len;
byte[] bytes = new byte[1024];
while ((len = inputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes,0,len);
}
System.out.println("下载完成");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (urlContent != null) {
urlContent.disconnect();
}
}
}