网络编程基础

网络编程

1.1   IP

ip:地址inetAddress

唯一定位一台网络上的计算机

127.0.0.1 本地localhost

ip地址的分类:

ip地址分类:

ipv4/ipv6

ipv4:127.0.0.1 由四个字节组成,0-255,42亿;30亿都在北美,亚洲4亿,2011年就已经用尽

ipv6:128位,8个无符号整数表示,adcde 2001:0bb2:aaaa:...

公网(互联网)-私网(局域网):

192.168.xx.xx 专门给组织内存使用的

域名:记忆ip问题

package Ip01;

import java.net.InetAddress;
import java.net.UnknownHostException;

//测试ip
public class TestInetAddress {

public static void main(String[] args) throws UnknownHostException {
    try {
        //查询本机地址
        InetAddress inetAddresses1 = InetAddress.getByName("127.0.0.1");
        System.out.println(inetAddresses1);
        InetAddress inetAddresses3 = InetAddress.getByName("localhost");
        System.out.println(inetAddresses3);
        InetAddress inetAddresses4 = InetAddress.getLocalHost();
        System.out.println(inetAddresses4);
        //查询网站ip
        InetAddress inetAddresses2 = InetAddress.getByName("www.baidu.com");
        System.out.println(inetAddresses2);
        //常用方法
    }catch (UnknownHostException e){
        e.printStackTrace();
    }

}

}
1.2端口

端口表示计算机上的一个程序的进程;

不同的进程有不同的端口号,用来区分软件

被规定0-65535

TCP;UDP:65535*2 tcp:80,udp:80   单个协议下,端口号不能冲突

端口分离

公有端口 0-1023

http:80

https:443

ftp:21

telent:23

程序注册端口 :1024-49151  分配给用户或者程序

Tomcat:8080

MySQL:3306

Gracle:1521

动态,私有端口:49152-65535

netstat - ano//查询所有的窗口

netstat -ano| findstr"xxx"//查询指定的端口

tasklist|findstr"xx"//查看置顶端口的进程

TCP UDP的区别

TCP:打电话

连接稳定

三次握手,四次挥手

客户端,服务端

UDP:发短信

不连接,不稳定

客服端,服务端;没有明确的界限

不管有没有准备好,都可以发给你。。

导弹

DDOS:洪水攻击(饱和攻击)

1.3  TCP

客服端

1,连接服务器socker

2,发送消息

package TCP;

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) {
//1,要知道服务器的地址
Socket socket = null;
OutputStream os = null;
try {
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
////3,创建一个socket连接
socket = new Socket(serverIP,port);
// 发送消息
os = socket.getOutputStream();
os.write("你好".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();
            }
        }
    }
}

}
服务器

1,建立服务的窗口

2,等待用户的连接accept

3,接受客服端消息

package TCP;

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 socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1,我要有一个地址
serverSocket = new ServerSocket(9999);
//2,等待客户端链接进来
socket = serverSocket.accept();
//3,读取客户端的消息
is = socket.getInputStream();

        // 管道流
         baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length=is.read(buffer)) != -1){
            baos.write(buffer,0,length);
        }
        
        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 TCP;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class TcpClientDemo02 {
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("Tx.jpeg"));
    //4,写出文件
    byte[] buffer = new byte[1024];
    int len;
    while ((len=fis.read(buffer)) != -1){
        os.write(buffer,0,len);
    }
    //通知服务器,我已经结束了
    socket.shutdownOutput();//我已经完了

    //确定服务器接受完毕才能断开连接
    InputStream inputStream = socket.getInputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer2 = new byte[2014];
    int len2;
    while ((len2=inputStream.read(buffer2))!= -1){
        baos.write(buffer2,len2,0);
    }
    System.out.println(baos.toString());
    baos.close();
    inputStream.close();
    fis.close();
    os.close();
    socket.close();
}

}
package TCP;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServerDome02 {
public static void main(String[] args) throws Exception {
//1,创建服务
ServerSocket severSocket = new ServerSocket(9000);
//2,监听客户端的连接
Socket socket = severSocket.accept();//组塞式监听,会一直等待客户端链接
//3,获取输入流
InputStream is = socket.getInputStream();
//4,文件输出
FileOutputStream fos = new FileOutputStream(new File("receive.jpeg"));
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer)) !=1){
fos.write(buffer,len,0);
}
//通知客户端我接受完毕了
OutputStream os = socket.getOutputStream();
os.write("我接受完毕了".getBytes());
fos.close();
is.close();
socket.close();
severSocket.close();
}

}
Tomcat

服务器

1,自定义s

2,Tomcat服务器S:java后台开发

客户端

1,自定义c

2,浏览器b

1.4  UDP

发短信:不用连接,需要知道对方的地址

package UDP;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;

// 不需要连接服务器
public class UdpClientDemo01 {
public static void main(String[] args) throws Exception {
//1,建立一个socket
DatagramSocket socket = new DatagramSocket();

    //2,建个包
    String msg = "nihao";
    InetAddress localhost = InetAddress.getByName("localhost");
    int port = 9990;
    DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
    //3,发送包
    socket.send(packet);
    //4,关闭流
    socket.close();
}

}
package UDP;

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();
}
}
1.5URL

package URL;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class URLDemo01 {
public static void main(String[] args) throws IOException {
URL url = new URL("");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream();
byte[] buffer = new byte[2014];
int len;
while ((len=inputStream.read(buffer))!= -1){
fos.write(buffer,len,0);

        fos.close();
        inputStream.close();
        urlConnection.disconnect();
    }
}

}

posted @ 2021-05-17 14:44  期待aaa  阅读(65)  评论(0)    收藏  举报