Java网络编程(学习狂神说网络编程的)

一、一些概念理解

网络中的包:可以理解为现实中包裹,信封,需要快递员送到我们的手上

丢包:可以理解为包裹在运输途中丢失

单机:无网,没有与其他电脑资源共享

网络编程的目的:无线电台,传播交流信息,数据交换,通信

如何实现网络的通信?

通信双方的地址:

ip

端口号

192.168.16.124:5900

规则:网络通信的协议

TCP/IP参考模型

二、IP 

IP地址在 Java.Inet.Address类中

  • 唯一定位一台网络上的计算机
  • 本机 127.0.0.1:localhost
  • IP地址的分类:
    • ipv4 / ipv6
    • 公网-私网
  • 域名:记忆IP问题
    • IP:www.vip.com

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

//测试IP
public class TestInetAddress {
    public static void main(String[] args) {
        try {
            //查询本机地址
            InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
            System.out.println(inetAddress1);
            InetAddress inetAddress3 = InetAddress.getByName("localhost");
            System.out.println(inetAddress3);
            InetAddress inetAddress4 = InetAddress.getLocalHost();
            System.out.println(inetAddress4);

            //查询网站ip地址
            InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");  //www.baidu.com/14.215.177.38
            System.out.println(inetAddress2);

            //常用方法
//            System.out.println(inetAddress2.getAddress());
            System.out.println(inetAddress2.getCanonicalHostName());    //规范的名字
            System.out.println(inetAddress2.getHostAddress());  //ip
            System.out.println(inetAddress2.getHostName()); //域名,或者自己电脑的名字
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

三、端口

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

不同的进程有不同的端口号,用来区分软件!
被规定 0~65535
TCP,UDP 每个协议都有65535。单个协议下,端口号不能冲突
端口分类:
公有端口 0 ~ 1023:
HTTP:80
HTTPS:443
FTP:21
Telent:23
程序注册端口:1024 ~ 49151,分配用户或者程序
Tomcat:8080
MySql:3306
Oracle:1521

netstat -ano #查看所有的端口
netstat -ano|findstr "5900" #查看指定的端口
tasklist|findstr "8696" #查看指定端口的进程
import java.net.InetSocketAddress;

public class TestInetSocketAddress {
    public static void main(String[] args) {
        InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080);
        InetSocketAddress socketAddress2 = new InetSocketAddress("localhost", 8080);
        System.out.println(socketAddress);
        System.out.println(socketAddress2);

        System.out.println(socketAddress.getAddress());
        System.out.println(socketAddress.getHostName());    //地址
        System.out.println(socketAddress.getPort());    //端口

    }
}

四、通信协议 

网络通信协议:速率,传输码率,代码结构,传输控制…

问题非常复杂:分层!大事化小

TCP/IP协议簇:实际上是一组协议

TCP:用户传输协议

UDP:用户数据报协议

IP:网络互连协议

五、TCP 实现聊天

  • 客户端
    • 1.连接服务器 Socket
    • 2.发送消息
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 {
            //1.要知道服务器的地址,端口号
            InetAddress serverIP = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            //2.创建一个socket链接
            socket = new Socket(serverIP, port);
            //3.发送消息    IO流
            os = socket.getOutputStream();
            os.write("你好你好".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

  • 服务端
    • 1.建立服务的端口 ServerSocket
    • 2.等待用户的链接 accept
    • 3.接收用户的消息
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);

            while (true){
                //2.等待客户端链接过来
                socket = serverSocket.accept();
                //3.读取客户端的消息
                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 {
            //关闭资源
            try {
                baos.close();
                is.close();
                socket.close();
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

六、UDP

//发送端
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

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

        //2.建立个包
        String msg = "服务器";
        //发送给谁
        InetAddress localhost = InetAddress.getByName("localhost");
        int port = 9090;
        //数据,数据的长度起始,要发送给谁
        DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);

        //3.发送包
        socket.send(packet);

        //4.关闭流
        socket.close();
    }
}

//接收端
import java.net.DatagramPacket;
import java.net.DatagramSocket;

//还是要等待客户端的连接
public class UdpServerDemo01 {
    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();
    }
}

//发送端
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

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

        //准备数据:控制台读取 System.in
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        while (true){
            String data = reader.readLine();
            byte[] datas = data.getBytes();
            DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));

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


        socket.close();
    }
}

//学生端
public class TalkStudent {
    public static void main(String[] args) {
        //开启两个线程
        new Thread(new Talksend(7777, "localhost", 9999)).start();
        new Thread(new TalkReceive(8888, "老师")).start();
    }
}

//老师端
public class TalkTeacher {
    public static void main(String[] args) {
        new Thread(new Talksend(5555, "localhost", 8888)).start();
        new Thread(new TalkReceive(9999, "学生")).start();
    }
}

七、URL 

Uniform Resource Locator, 统一资源定位器

 URL是对互联网上得到的资源的位置和访问方法的一种简洁表示,是互联网上标准资源的地址。URL它具有全球唯一性,正确的URL应该是可以通过浏览器打开此网页的,但如果您访问外网,会提示网页无法打开,这并不能说明这个URL是错误的。只不过在国内不能访问而已。

https://www.baidu.com/
统一资源定位符:定位资源的,定位互联网上的某一个资源 

  • 使用 url 下载网络资源
  • 第一步:先在 tomcat 的对应路径下创建一个文件
  • 第二步:在浏览器输入:协议://ip地址:端口/项目名/资源

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值