IP: InetAddress
- 唯一定位网络上的计算机
- 127.0.0.1? ? ? ? 本机localhost
- ip地址分类? IPV4/IPV6
- IPV4? ? ? ? 4个字节(0~255)共42亿? ? ? ? 110.242.69.7
- IPV6? ? ? ? 128位16字节 8个无符号整数? ? ?fd6d:a06a:4864:0:6de5:84a5:ce7:de56
- 局域网? ? ? ? 128.168.x.x
- ABCD类? ? A:1.0.0.0---127.255.255.255(1677_7214台)? ?B:128.0.0.0---191.255.255.255(6万多台)C:192.0.0.0---223.255.255.255(254台)? D:224.0.0.0---239.255.255.255
package hk.tcp_udp_url.lesson1;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddress1 {
public static void main(String[] args) {
try{
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress);
InetAddress inetAddress0 = InetAddress.getByName("www.bilibili.com");
System.out.println(inetAddress0);
}catch (UnknownHostException e){
e.printStackTrace();
}
}
}
- ?
?端口
- 端口表示计算机上的一个程序进程
- 不同的进程有不同的端口号,用来区分软件
- TCP,UDP: 端口号0~65535? ? ? ? ? ? ? 单个协议下端口号不能冲突? ? ? ? ? ? ? ? ? ? ? ? 65535*2? ? ?
- ?端口分类
- 共有端口? ? ?0~1023? ? ? ? ? ?尽量不要占用?????????????????????????????????????????????????????
- ?HTTP:80
- HTTPS:443? ? ? ?
- FTP:21? ? ? ? ? T
- elent:23????????????????????????
- 程序注册端口:2014~49151
- Tomcat:8080
- MySQL:3306
- Oracle:1521
- 动态,私有? ? ? ? ? 49152~65535
- cmd查看所有端口命令
- ?查看指定端口
- ?查看指定端口进程
-
tasklist|findstr "xxx"
- ?InetSocketAddress
System.out.println(inetSocketAddress.getPort());//端口
System.out.println(inetSocketAddress.getHostName());//ip地址
package hk.tcp_udp_url.lesson1;
import java.net.InetSocketAddress;
public class Lesson2 {
public static void main(String[] args) {
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080);
System.out.println(inetSocketAddress);
System.out.println(inetSocketAddress.getAddress());
System.out.println(inetSocketAddress.getPort());//端口
System.out.println(inetSocketAddress.getHostName());//ip地址
}
}
?通信协议
TCP/IP协议簇? ? ? ? 一组协议
TCP:用户传输协议? ? ? ? ? ? ? ? 需要连接才能发送? ? ? ? 三次握手;四次挥手
UDP:用户数据报协议? ? ? ? ? ? ?不用连接就能发送(DDos攻击)
TCP 通信:
客户端:
1.? ? ? ? 获取地址,端口;连接服务器socket????????
socket = new Socket(byName【地址】,port【端口】);
2.? ? ? ? 发送消息
outputStream = socket.getOutputStream();
outputStream.write("你好,连接成功了吗?".getBytes(StandardCharsets.UTF_8));
package hk.tcp_udp_url.TCP.deom1;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class Client {
public static void main(String[] args) {
Socket socket =null;
OutputStream outputStream =null;
try {
//获取地址
InetAddress byName = InetAddress.getByName("127.0.0.1");
//端口
int port =9999;
//创建端口socket连接
socket = new Socket(byName,port);
//发送消息
outputStream = socket.getOutputStream();
outputStream.write("你好,连接成功了吗?".getBytes(StandardCharsets.UTF_8));
}catch (Exception 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();
}
}
}
}
}
服务端:
1.? ? ? ? 建立端口ServerSocket
2.? ? ? ? 等待客户端连接accept
3.? ? ? ? 接收信息inputStream
package hk.tcp_udp_url.TCP.deom1;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
ServerSocket serverSocket =null;
Socket socket=null;
InputStream inputStream =null;
ByteArrayOutputStream byteArrayOutputStream =null;
try {
//创建地址
serverSocket = new ServerSocket(9999);
while (true){
//等待客户端连接
socket=serverSocket.accept();
//读取客户端信息
inputStream = socket.getInputStream();
//管道流
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len;
while((len=inputStream.read(buffer))!=-1){
byteArrayOutputStream.write(buffer,0,len);
}
System.out.println(byteArrayOutputStream.toString());
}
}catch (IOException e){
e.printStackTrace();
}finally {
//关闭流
//后创建的先关闭
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();
}
}
}
}
}
TCP:? ? ? ? 传文件
服务端
{
public static void main(String[] args) {
ServerSocket serverSocket =null;
Socket accept =null;
InputStream inputStream =null;
FileOutputStream fileOutputStream =null;
try {
//创建地址
serverSocket = new ServerSocket(9611);
//等待客户连接
accept = serverSocket.accept();
//读取输入流
inputStream = accept.getInputStream();
//管道输出流
fileOutputStream = new FileOutputStream(new File("C:\\Users\\hk\\Desktop\\002.jpg"));
byte[] bytes = new byte[1024];
int len;
while((len=inputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
}
//通知客户端接收完毕
OutputStream outputStream = accept.getOutputStream();
outputStream.write("接收完毕,可以断开".getBytes(StandardCharsets.UTF_8));
//关闭资源
serverSocket.close();
accept.close();
inputStream.close();
fileOutputStream.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
客户端
package hk.tcp_udp_url.TCP.file;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class Client1 {
public static void main(String[] args) throws Exception{
//连接服务器socket
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9611);
//创建一个输出流
OutputStream outputStream = socket.getOutputStream();
//读取文件
FileInputStream fileInputStream = new FileInputStream(new File("C:\\Users\\hk\\Desktop\\《火影忍者》九只尾兽4k壁纸图片.jpg"));
//写出文件
byte[] bytes = new byte[1024];
int len;
while ((len=fileInputStream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
}
//终断输出流
socket.shutdownOutput();
//确定服务端接收完毕
InputStream inputStream = socket.getInputStream();
//管道流接收
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes1 = new byte[1024];
int len1;
while((len1=inputStream.read(bytes1))!=-1){
byteArrayOutputStream.write(bytes1,0,len1);
}
System.out.println(byteArrayOutputStream.toString());
//关闭资源
inputStream.close();
byteArrayOutputStream.close();
fileInputStream.close();
outputStream.close();
socket.close();
}
UDP? ? ? ? ? ? ? ? 不用连接,只需地址
udp发送消息
?发送端
package hk.tcp_udp_url.udp.deom1;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
public class Client {
public static void main(String[] args) throws Exception{
//建立个Socket
DatagramSocket datagramSocket = new DatagramSocket();
//建包
String msg="Hello,world";
InetAddress localhost = InetAddress.getByName("localhost");
int port=12900;
DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(StandardCharsets.UTF_8),0,msg.getBytes().length,localhost,port);
//发送包
datagramSocket.send(datagramPacket);
datagramSocket.close();
}
}
接收端?
package hk.tcp_udp_url.udp.deom1;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Server {
public static void main(String[] args) throws Exception{
//开放端口
DatagramSocket datagramSocket = new DatagramSocket(12900);
//接收包
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length);
datagramSocket.receive(datagramPacket);//阻塞接收
System.out.println(datagramPacket.getAddress().getHostAddress());
System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getData().length));
//关闭连接
datagramSocket.close();
}
}
单方向连续发送信息
发送方
package hk.tcp_udp_url.udp.deom2;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
public class Sender {
public static void main(String[] args) throws Exception {
DatagramSocket datagramSocket = new DatagramSocket(19000);
//从system.in读取数据
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
while(true){
String data= bufferedReader.readLine();
byte[] datas=data.getBytes(StandardCharsets.UTF_8);
DatagramPacket datagramPacket = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",7922));
datagramSocket.send(datagramPacket);
if (data.equals("拜拜")){
break;
}
}
datagramSocket.close();
}
}
接收方
package hk.tcp_udp_url.udp.deom2;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Receive {
public static void main(String[] args) throws Exception {
DatagramSocket datagramSocket = new DatagramSocket(7922);
while (true) {
//接收信息
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length);
datagramSocket.receive(datagramPacket);
//断开判断 “拜拜”
byte[] data = datagramPacket.getData();
String datas = new String(data, 0, datagramPacket.getLength());
System.out.println(datas);
if (datas.equals("拜拜")) {
break;
}
}
datagramSocket.close();
}
}
UDP? ? ? ? 多线程? ? ? ? 双向通信
package hk.tcp_udp_url.udp.deom3;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class TalkReceive implements Runnable{
DatagramSocket datagramSocket =null;
private int port;
private String msf;
public TalkReceive(int port,String msf)throws Exception {
this.port=port;
this.msf=msf;
datagramSocket=new DatagramSocket(this.port);
}
@Override
public void run() {
while(true){
try {
//接收信息
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length);
datagramSocket.receive(datagramPacket);
//断开判断 “拜拜”
byte[] data = datagramPacket.getData();
String datas = new String(data, 0, datagramPacket.getLength());
System.out.println(msf+" :"+datas);
if (datas.equals("拜拜")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
datagramSocket.close();
}
}
package hk.tcp_udp_url.udp.deom3;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
public class TalkSend implements Runnable{
DatagramSocket datagramSocket =null;
BufferedReader bufferedReader =null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromPort,String toIP,int toPort) {
this.fromPort=fromPort;
this.toIP=toIP;
this.toPort=toPort;
try{
datagramSocket = new DatagramSocket(fromPort);
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
String data= bufferedReader.readLine();
byte[] datas=data.getBytes(StandardCharsets.UTF_8);
DatagramPacket datagramPacket = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIP,this.toPort));
datagramSocket.send(datagramPacket);
if (data.equals("拜拜")){
break;
}
}catch (Exception e){
e.printStackTrace();
}
}
datagramSocket.close();
}
}
package hk.tcp_udp_url.udp.deom3;
public class Talk1 {
public static void main(String[] args) throws Exception {
//开启线程
new Thread(new TalkSend(2291,"127.0.0.1",2223)).start();
new Thread(new TalkReceive(2222,"P2")).start();
}
}
package hk.tcp_udp_url.udp.deom3;
public class Talk2 {
public static void main(String[] args) throws Exception {
new Thread(new TalkSend(2842,"127.0.0.1",2222)).start();
new Thread(new TalkReceive(2223,"P1")).start();
}
}
URL? ? ? ? ? ? ? ? 【爬虫,python方便】
package hk.tcp_udp_url;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class URL0 {
public static void main(String[] args) throws IOException {
//创建URL类
URL url = new URL("https://github.com");
//连接url的HTTP
HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream urltext = new FileOutputStream("urltext");
byte[] bytes = new byte[1024];
int len;
while ((len=inputStream.read(bytes))!=-1){
urltext.write(bytes,0,len);
}
urltext.close();
inputStream.close();
urlConnection.disconnect();//断开连接
}
}
|