网络编程(笔记)

十四.网络编程

(1)网络编程概述

网络编程的目的:

直接或间接地通过网络协议与其它计算机实现数据交换,进行通讯。

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

如何准确地定位网络上一台或多台主机;定位主机上的特定的应用

找到主机后如何可靠高效地进行数据传输

端口号:

端口号:正在计算机上运行的进程。
要求:不同的进程有不同的端口号
范围:被规定为一个16位的整数0~65535。

端口号与IP地址的组合得出一个网络套接字:socket

(2)InetAddress

实例化两个方法:getByName,getLocalHost

常用方法:

getHostName():获取域名

getHostAddress():获取ip地址

(3)TCP网络编程

    //客户端
    public void client() {

        Socket socket = null;
        OutputStream ops = null;
        try {
            //1.创建Socket对象,指明端口号和ip
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            //获取本机ip
//            System.out.println(InetAddress.getLocalHost().getHostAddress());
            socket = new Socket(inet,1314);
            //2.获取输出流,用于输出数据
            ops = socket.getOutputStream();
            //3.写数据
            ops.write("ccw----sqs".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ops != null){
                try {
                    ops.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    @Test
    //服务端
    public void server() {

        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            //ServerSocket是等待客户端的请求,一旦获得一个连接请求,就创建一个Socket示例来与客户端进行通信
            ss = new ServerSocket(1314);
            //调用accept()表示接收来自客户端的socket
            socket = ss.accept();

            is = socket.getInputStream();

            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[5];
            int len;
            while((len = is.read(buffer)) != -1){
                baos.write(buffer,0,len);//写入到ByteArrayOutputStream中的默认数组中
            }
            System.out.println(baos.toString());
            System.out.println("收到了来自"+socket.getInetAddress().getHostAddress()+"的数据");

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

(4)文件互传

/**
 * 客户端发送文件给服务端,服务端将文件保存在本地。
 *
 *
 * @author: ccw
 * @date: 2022/5/5 12:27
 */
public class InetAddressTest2 {
    @Test
    public void client() {

        Socket socket = null;
        OutputStream ops = null;
        try {
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inet,8848);

            ops = socket.getOutputStream();
            ops.write("喜羊羊".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ops != null){
                try {
                    ops.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
@Test
    public void server() throws IOException {
        
        //ServerSocket是等待客户端的请求,一旦获得一个连接请求,就创建一个Socket示例来与客户端进行通信
        ServerSocket ss = new ServerSocket(8848);
        Socket socket = ss.accept();

        InputStream is = socket.getInputStream();
        FileOutputStream fos = new FileOutputStream(new File("E:\\java\\StudyProject\\src\\com\\ccw\\Exer33\\hello.txt"));


        byte[] buffer = new byte[5];
        int len;
        while ((len = is.read(buffer)) != -1){
            fos.write(buffer,0,len);
        }
    
        ss.close();
        is.close();
        socket.close();
    }

}

(5)UDP网络编程

public void sender() throws IOException {

    DatagramSocket socket = new DatagramSocket();
    String str = "我是UDP";

    byte[] data = str.getBytes();
    //指明接收端的ip
    InetAddress inet = InetAddress.getByName("127.0.0.1");
    DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,8848);
    socket.send(packet);

    socket.close();

}
public void receive() throws IOException {
    DatagramSocket socket = new DatagramSocket(8848);

    byte[] buffer = new byte[100];
    DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
    socket.receive(packet);

    System.out.println(new String(packet.getData(), 0, packet.getLength()));

    socket.close();

}

(6)URL网络编程

URL:统一资源定位符 ,对应着互联网的某一地址

一个URL对象生成后,其属性是不能被改变的,但可以通过它给定的方法来获取这些属性:

public String getProtocol()获取该URL的协议名
public String getHost( )获取该URL的主机名
public String getPort( )获取该URL的端口号
public String getPath( )获取该URL的文件路径
public String getFile( )获取该URL的文件名
public String getQuery( )获取该URL的查询名
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值