网络编程(初步认识TCP、UDP、URL)

目录

一、网络编程中有两个主要的问题:

二、网络编程中的两个要素:

三、通信要素一:IP和端口号

四、实现TCP网络编程

例子1:客户端发送信息到服务端,服务端将信息展示在控制台上。

例题2:客户端发送文件给服务端,服务端将文件保存在本地。

例题3:从客户端发送文件给服务端,服务端保存到本地。并返回发送成功给客户端。并关闭相应的连接。

五、UDP协议的网络编程

六、URL:统一资源定位符


 

一、网络编程中有两个主要的问题:

1.如何准确地定位网络上一台或多台主机;定位主机上的特定的应用
2.找到主机后如何可靠高效地进行数据传输

二、网络编程中的两个要素:

1.对应问题一:IP和端口号
2.对应问题二:提供网络通信协议:TCP/IP参考模型(应用层、传输层、网络层、数据链路层、物理层)

三、通信要素一:IP和端口号

1.IP:唯一的标识Internet上的计算机(通信实体)
2.在java中使用InetAddress类代表IP
3.Ip分类: IPv4和IPv6 ;万维网和局域网
4.域名:www.baidu.com
5.本机回路地址:127.0.0.1  对应着:localhost
6.如何实例化InetAddress:两个方法: getByName(String host) , getLocalHost();
         两个常用方法: getHostName() / getHostAddress()
7.端口号:正在计算机上运行的进程。要求:不同的进程有不同的端口号
  范围:被规定为一个16位的整数0~65535。
8.端口号与IP地址的组合得出一个网络套接字:Socket

四、实现TCP网络编程

直接代码展示

例子1:客户端发送信息到服务端,服务端将信息展示在控制台上。

public class TCPTest {
    //客户端
    @Test
    public void client() {
        Socket socket = null;
        OutputStream ops = null;
        try {
//1、创建Socket对象,指明服务器端的ip和端口号
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inet, 666);
//2、获取到一个输出流
            ops = socket.getOutputStream();
//3、用输出流写出数据
            ops.write("我是客户端".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
//4、资源关闭:(流和socket)
            if (ops != null) {
                try {
                    ops.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }

    //服务端
    @Test
    public void server() {
        ServerSocket serverSocket = null;
        InputStreamReader isr = null;
        try {
//1、创建服务器端的ServerSocket,指明自己的端口号
            serverSocket = new ServerSocket(666);
//2、调用accept()表明可以接收客户端的socket
            Socket accept = serverSocket.accept();
//3、获取一个输入流
            //有汉字,字节流过来可能会乱码.所以转换流改一下
            isr = new InputStreamReader(accept.getInputStream());
//4、读取输入流中的数据
            char[] c = new char[100];
            int len;
            while ((len = isr.read(c)) != -1) {
                String s = new String(c, 0, len);
                System.out.println(s);
                //其他需求
                System.out.println("收到了来自客户端为:" + serverSocket.getInetAddress().getHostName() + "发来的消息");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
//5、关闭资源(流和socket)
            if (isr != null) {

                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket != null) {

                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

例题2:客户端发送文件给服务端,服务端将文件保存在本地。

public class TCPTest2 {
    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"),622);
            os = socket.getOutputStream();
            fis = new FileInputStream(new File("lizhi.png"));

            byte[] b = new byte[1024];
            int len;
            while ((len = fis.read(b)) != -1){
                os.write(b,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void server(){
        ServerSocket ss = null;
        InputStream inputStream = null;
        FileOutputStream fos = null;
        Socket accept = null;
        try {
            ss = new ServerSocket(622);
            accept = ss.accept();
            inputStream = accept.getInputStream();

            fos = new FileOutputStream(new File("励志.png"));

            byte[] b = new byte[1024];
            int len ;
            while ((len = inputStream.read(b)) != -1){
                fos.write(b,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                if (fos != null)
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (inputStream != null)
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (accept != null)
               accept.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ss != null)
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }



    }
}

例题3:从客户端发送文件给服务端,服务端保存到本地。并返回发送成功给客户端。并关闭相应的连接。

public class TCPTest3 {
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 622);
            os = socket.getOutputStream();
            fis = new FileInputStream(new File("lizhi.png"));

            byte[] b = new byte[1024];
            int len;
            while ((len = fis.read(b)) != -1) {
                os.write(b, 0, len);
            }
//            这里在客户端必须手动的停止这个read()的执行,因为这是一个阻塞式的方法,不知道什么时候停。
            socket.shutdownOutput();

//            在这里需要接收服务端的消息
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            char[] chars = new char[5];
            int len1;
            while ((len1 = isr.read(chars)) != -1) {
                String s = new String(chars, 0, len1);
                System.out.print(s);
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void server() {
        ServerSocket ss = null;
        InputStream inputStream = null;
        FileOutputStream fos = null;
        Socket accept = null;
        try {
            ss = new ServerSocket(622);
            accept = ss.accept();
            inputStream = accept.getInputStream();

            fos = new FileOutputStream(new File("励志.png"));

            byte[] b = new byte[1024];
            int len;
            while ((len = inputStream.read(b)) != -1) {
                fos.write(b, 0, len);
            }

//            这里需要给客户端回馈
            OutputStream os = accept.getOutputStream();
            os.write("你的消息发送成功了".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (accept != null)
                    accept.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ss != null)
                    ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }
}

五、UDP协议的网络编程

public class UDPTest {
    //发送端
    @Test
    public void sender() {
        DatagramSocket Socket = null;
        try {
            Socket = new DatagramSocket();
            String s = new String("我是udp发来的");
            byte[] bytes = s.getBytes();
            InetAddress inet = InetAddress.getByName("127.0.0.1");

            DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, inet, 666);

            Socket.send(packet);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            Socket.close();
        }


    }

    //接收端
    @Test
    public void receiver()  {
        DatagramSocket datagramSocket = null;
        try {
            datagramSocket = new DatagramSocket(666);

            byte[] bytes = new byte[100];
            DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);

            datagramSocket.receive(packet);
            System.out.println(new String(packet.getData(), 0, packet.getLength()));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            datagramSocket.close();
        }


    }
}

六、URL:统一资源定位符

URL的5个基本结构
* http://localhost:8080/examples/beauty.jpg?username=Tom
* 协议     主机名  端口号   资源地址           参数列表
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java塑造中...

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值