ip
ip地址:InetAddress
- 唯一定位一台网络上的计算机
- 127.0.0.1:本机localhost
- ip地址的分类
- IPV4/IPV6
- IPV4 127.0.0.1 ,四个字节组成,0~255
- IPV6 fe80::718a:6f2b:110:e642%9 ,128位,8个无符号整数
- 公网(互联网)-私网(局域网)
- IPV4/IPV6
- 域名:记忆IP问题!
端口
端口表示计算机上的一个程序的进程
-
不同的进程有不同的端口号,用来区分软件
-
被规定 0~65535
-
单个协议下,端口号不能冲突
-
端口分类
-
公有端口0~1023
- HTTP:80
- HTTPS: 443
- FTP: 21
-
程序注册端口:1024~49151
- Tomcat: 8080
- MySQL: 3306
- Oracle: 1521
-
动态、私有:49152~65535
netstat -ano #查看所有的端口 netstat -ano|findstr "5900" #查看指定的端口 tasklist |findstr"8696 #查看指定端口的进程 Ctrl+Shift+ESC
-
通信协议
协议:约定,就好比我们说的普通话
网络通信协议:速率,传输码率,代码,传输控制......
TCP/IP协议簇(实际上是一组协议)
* TCP:用户传输协议
* UDP: 用户数据报协议
* IP:网络互联协议
TCP
客户端
-
连接服务器Socket
-
发送消息
//客户端
public class TcpClient01 {
public static void main(String[] args) {
//1.要知道服务器地址
InetAddress serverIp=null;
OutputStream os=null;
Socket socket=null;
try {
serverIp = InetAddress.getByName("127.0.0.1");
int port=9999;
//2. 创建一个socket连接
socket = new Socket(serverIp,port);
//3. 发送消息 IO流
os= socket.getOutputStream();
os.write("你好,欢迎学习JAVA".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();
}
}
}
}
}
服务器
- 建立服务的端口 ServerSocket
- 等待用户的连接 accept
- 接收用户的消息
//服务器
public class TcpServer01 {
public static void main(String[] args) {
//1.我得有一个地址
ServerSocket serverSocket=null;
ByteArrayOutputStream baos = null;
InputStream is=null;
Socket socket=null;
try {
serverSocket = new ServerSocket(9999);
//2等待客户等连接过来
socket = serverSocket.accept();
//3. 读取客户端的信息
is = socket.getInputStream();
//管道流
baos=new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len;
while ((len=is.read(bytes))!=-1){
baos.write(bytes,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(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(serverSocket!=null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件上传
服务器
package demo02;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer02 {
public static void main(String[] args) throws IOException {
//1.创建服务
ServerSocket serverSocket = new ServerSocket(9000);
//2. 监听客户端的连接
Socket socket = serverSocket.accept();//阻塞式监听,会一直等待客户端连接
//3. 获取输入流
InputStream is = socket.getInputStream();
//4.文件输出
FileOutputStream fos = new FileOutputStream(new File("receive.jpg"));
byte[] bytes = new byte[1024];
int len;
while ((len=is.read(bytes))!=-1){
fos.write(bytes,0,len);
}
//通知客户端我接收完毕
OutputStream os = socket.getOutputStream();
os.write("我接收完毕了,你可以断开了".getBytes());
//关闭资源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
客户端
public class TcpClient02 {
public static void main(String[] args) throws Exception {
//1.创建Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
//2.创建输出流
OutputStream os=socket.getOutputStream();
//3. 读取文件
FileInputStream fis = new FileInputStream(new File("sky.jpg"));
//4. 写出文件
byte[] bytes = new byte[1024];
int len;
while ((len=fis.read(bytes))!=-1){
os.write(bytes,0,len);
}
//通知服务器,我已经结束了
socket.shutdownOutput();//我已经传输完了
//确定服务器接收完毕,才能断开连接
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len2;
while ((len2=inputStream.read(buffer))!=-1){
baos.write(buffer,0,len2);
}
System.out.println(baos.toString());
//5.关闭资源
fis.close();
os.close();
socket.close();
}
}
UDP
类似于发短信:不需要直到别人的地址
发送端
public class UDPClient01 {
public static void main(String[] args) throws Exception{
//1. 建立一个Sockect
DatagramSocket socket = new DatagramSocket();
//2.建个包
String s="Hello!";
InetAddress localhost = InetAddress.getByName("localhost");
int port=9090;
DatagramPacket packet = new DatagramPacket(s.getBytes(),0,s.getBytes().length,localhost,port);
//3. 发送包
socket.send(packet);
//4.关闭流
socket.close();
}
}
接收端
//还是要等待客户端的连接!
public class UDPServer01 {
public static void main(String[] args) throws Exception{
//开放端口
DatagramSocket socket = new DatagramSocket(9090);
//接收数据包
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
socket.receive(packet);//阻塞接收
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
单向聊天
public class UDPSender01 {
public static void main(String[] args) throws Exception{
DatagramSocket socket = new DatagramSocket(8888);
//准备数据:控制台读取 System.in
Scanner input = new Scanner(System.in);
while (true){
String data = input.next();
byte[] sendData= data.getBytes();
DatagramPacket packet = new DatagramPacket(sendData, 0, sendData.length, new InetSocketAddress("localhost", 6666));
socket.send(packet);
if (data.equals("bye")){
break;
}
}
socket.close();
}
}
public class UDPReiceiver01 {
public static void main(String[] args) throws Exception{
DatagramSocket socket = new DatagramSocket(6666);
while (true){
//准备接收包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
socket.receive(packet);//阻塞式接收包裹
byte[] data = packet.getData();
String receiveData= new String(data,0,packet.getLength());
System.out.println(receiveData);
//断开连接,bye
if(receiveData.equals("bye")){
break;
}
}
socket.close();
}
}
双向聊天(多线程)
public class TalkSend implements Runnable {
DatagramSocket socket =null;
Scanner input=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 {
socket = new DatagramSocket(fromPort);
input = new Scanner(System.in);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true){
String s = input.next();
byte[] sendData = s.getBytes();
DatagramPacket packet = new DatagramPacket(sendData,0,sendData.length,new InetSocketAddress(this.toIP,this.toPort));
socket.send(packet);
if(sendData.equals("bye")){
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
socket.close();
}
}
public class TalkReceive implements Runnable{
DatagramSocket socket = null;
private int port;
private String s;
public TalkReceive(int port,String s) {
this.port = port;
this.s=s;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true) {
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);
byte[] data = packet.getData();
String receiveData = new String(data,0,data.length);
System.out.println(s+":"+receiveData);
if (receiveData.equals("bye")){
break;
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public class TalkStudent {
public static void main(String[] args) {
TalkSend talkSend = new TalkSend(7777,"127.0.0.1",9999);
TalkReceive talkReceive = new TalkReceive(8888,"Teacher");
new Thread(talkReceive).start();
new Thread(talkSend).start();
}
}
public class TalkTeacher {
public static void main(String[] args) {
TalkSend talkSend = new TalkSend(5555,"localhost",8888);
TalkReceive talkReceive = new TalkReceive(9999,"Student");
new Thread(talkSend).start();
new Thread(talkReceive).start();
}
}
URL
统一资源定位符:定位资源,定位互联网上的某个资源
协议://ip地址:端口/项目名/资源
下载网上资源
package demo04;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLDownload {
public static void main(String[] args) throws IOException {
//1. 下载资源
URL url = new URL("https://t7.baidu.com/it/u=2531125946,3055766435&fm=193&f=GIF");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos= new FileOutputStream("piying.gif");
byte[] bytes = new byte[1024];
int len;
while((len=inputStream.read(bytes))!=-1){
fos.write(bytes,0,bytes.length);//写出这个数据
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}
}