IP,端口,协议
ip地址:InetAddress
-
唯一定位一台电脑
-
127.0.0.1:本机localhost
-
ip地址分类
-
ipv4/ipv6
-
ipv4 127.0.0.1 ,4个字节组成。0-255,42亿,30亿都在北美
亚洲有4亿,2011年以用尽;
-
ipv6 :128位,8个无符号整数!
-
-
端口
端口表示计算机上的一个程序的进程
-
不同的进程有不同的端口号
-
被规定0-65535
-
TCP;UDP
通信协议
协议:约定就好比普通话
网络通信协议:速率,传输码率
TCP/IP协议簇
重要
-
TCP:用户传输协议
-
UDP:用户数据报协议
出名的协议
-
tpc:
-
iP:网络互连协议
tcp udp对比
tcp:打电话
-
链接稳定
udp :发短息
-
不连接,不稳定
TCP
客户端
package com.atuo.less01;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
//客户端
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
//要知道服务端的地址,和端口号
InetAddress serverIp = InetAddress.getByName("127.0.0.1");
int port = 9999;
//创建一个socket链接
socket = new Socket(serverIp, 9999);
//发送消息IO流
os = socket.getOutputStream();
os.write("hello,My name is jinyaxing".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务端
package com.atuo.less01;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket accept = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//有一个地址
serverSocket = new ServerSocket(9999);
//等待连接
accept = serverSocket.accept();
//读取客户端消息
is = accept.getInputStream();
//管道流
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
baos.write(buffer,0,len);
}
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 (accept!=null){
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket!=null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
udp
package com.atuo.less02;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UdpClientDemo01 {
public static void main(String[] args) throws Exception {
//创建Socket链接
DatagramSocket socket = new DatagramSocket();
//
String string = "hello,好久不见";
DatagramPacket packet = new DatagramPacket(string.getBytes(), 0, string.getBytes().length, InetAddress.getByName("127.0.0.1"), 9090);
//发送
socket.send(packet);
//
socket.close();
}
}
package com.atuo.less02;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UdpSeverDemo01 {
public static void main(String[] args) throws Exception {
//开放接口
DatagramSocket socket = new DatagramSocket(9090);
//接受包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
//接收链接
socket.receive(packet);
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
下载网络资源
package com.atuo.less02;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class UdpUrlDemo1 {
public static void main(String[] args) throws Exception {
URL url = new URL("https://webfs.tx.kugou.com/202204040720/0b9c0d36463ae1ef6ef072a124dceb41/part/0/960141/KGTX/CLTX001/028c92723e0709730d8eb3b764965db2.mp3");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = connection.getInputStream();
FileOutputStream fos = new FileOutputStream("2.mp4");
byte[] buffer = new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
}
}