网络编程基础

IP


ip地址:InetAddress

  • 唯一定位一台网络上计算机
  • 127.0.0.1 :本机localhost
  • ip地址的分类
    • ipv4/ipv6
      • IPV4 127.0.0.1 ,4个字节组成,0-255,,42亿
      • IPV6 fe80::d544:28da:5eb2%13 128位,8个无符号整数
    • 公网(互联网)-私网(局域网)
      • ABCD类地址
      • 192.168.xx.xx 专门给组织内部使用的
  • 域名:记忆IP问题

端口


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

  • 不同的进程有不同的端口号!用来区分软件!
  • 被规定0-65535
  • TCP,UDP:65535 *2 单个协议下,端口号不能冲突
  • 端口分类
    • 公有端口0-1023
      • HTTP:80
      • HTTPS:443
      • FTP:21
      • Telnet:23
    • 程序注册端口:1024-49151,分配用户或者程序
      • Tomcat 8080
      • MySQL:3306
      • Oracle:1521
    • 动态、私有:49152-65535
      • netstat -ano

通信协议


网络通信协议:速率、传输码率、代码结构、传输控制…
TCP/IP协议簇

  • TCP:用户传输协议
  • UDP:用户数据报协议
  • IP:网络互联协议
    TCP UPD对比
    TCP:打电话
  • 连接、稳定
  • 三次握手、四次挥手
1.最少需要三次,保证稳定连接
2.A:你瞅啥?
2.B:瞅你咋地?
3.A:干一场!

1.A:我要断开了
2.B:我知道你要断开了
3.B:你真的要断开了吗?
4.A:我真的要断开了
  • 客户端、服务端
  • 传输完成,释放连接,效率低
    UDP:发短信
  • 不连接、不稳定
  • 客户端、服务端:没有明确的界限
  • 不管有没有准备好,都可以发给你
  • 导弹
  • DDOS:洪水攻击(饱和攻击)

TCP


客户端

  1. 连接服务器Socket
  2. 发送消息
public class TCPClientDemo1 {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream os = null;
        try {
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            socket = new Socket(inetAddress,port);
            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();
                }
            }
        }
    }
}

服务端

  1. 建立服务的端口ServerSocket
  2. 等待用户的连接accept
  3. 接收用户的消息
public class TCPServerDemo1 {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            serverSocket = new ServerSocket(9999);
            while(true) {
                socket = serverSocket.accept();
                is = socket.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(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

上传文件
客户端

public class TCPClientDemo2 {
    public static void main(String[] args) throws Exception{
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);
        OutputStream os = socket.getOutputStream();
        FileInputStream fis = new FileInputStream(new File("3.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while((len=fis.read(buffer))!= -1){
            os.write(buffer,0,len);
        }
        socket.shutdownOutput();

        InputStream is = socket.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer2 = new byte[1024];
        int len2;
        while((len2= is.read(buffer2))!= -1){
            baos.write(buffer2,0,len2);
            System.out.println(baos.toString());
        }
        baos.close();
        is.close();
        fis.close();
        os.close();
        socket.close();
    }
}

服务端

public class TCPServerDemo2 {
    public static void main(String[] args) throws Exception{
        ServerSocket serverSocket = new ServerSocket(9000);
        Socket socket = serverSocket.accept();
        InputStream is = socket.getInputStream();
        FileOutputStream fos = new FileOutputStream(new File("receive.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while((len = is.read(buffer))!= -1){
            fos.write(buffer,0,len);
        }

        OutputStream os = socket.getOutputStream();
        os.write("我已接收完毕,可以断开了".getBytes());

        os.close();
        fos.close();
        is.close();
        socket.close();
        serverSocket.close();
    }
}

Tomcat
服务端

  • 自定义S
  • Tomcat服务器S
    客户端
  • 自定义C
  • 浏览器B

UDP


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

DatagramPacket

DatagramSocket

发送消息

public class UDPClientDemo1 {
    public static void main(String[] args) throws Exception{
        DatagramSocket socket = new DatagramSocket();
        String msg = "你好啊,服务器!";
        InetAddress localhost = InetAddress.getByName("localhost");
        int port = 9090;
        DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
        socket.send(packet);
        socket.close();
    }
}

接收消息

public class UDPServerDemo1 {
    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());
        System.out.println(new String(packet.getData(),0,packet.getLength()));
        socket.close();
    }
}

循环发送

public class UDPSenderDemo1 {
    public static void main(String[] args) throws Exception{
        DatagramSocket socket = new DatagramSocket();

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while(true) {
            String data = reader.readLine();
            DatagramPacket packet = new DatagramPacket(data.getBytes(), 0, data.getBytes().length, new InetSocketAddress("localhost", 6666));

            socket.send(packet);
            if(data.equals("bye")){
                break;
            }
        }
        socket.close();
    }
}

循环接收

public class UDPReceiveDemo1 {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(6666);
        while(true) {
            byte[] buffer = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);

            socket.receive(packet);
            byte[] data = packet.getData();
            String receivedata = new String(data,0, packet.getLength());
            System.out.println(receivedata.toString());
            if(receivedata.equals("bye")){
                break;
            }
        }
        socket.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值