网络编程实例

一、TCP编程

1.实例:利用网络发送文字

 //客户端
    @Test
    public void client() {
        Socket ss = null;
        OutputStream outputStream = null;
        try {
            InetAddress ia = InetAddress.getByName("127.0.0.1");
            ss = new Socket(ia, 8899);
            outputStream = ss.getOutputStream();
            outputStream.write("你好,我是客户".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {

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


            }
            if (ss != null) {

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

    }

    //服务端
    @Test
    public void severTest(){
        ServerSocket serverSocket = null;
        Socket accept = null;
        InputStream inputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;//写到本类数组中,不够就扩充
        try {
            serverSocket = new ServerSocket(8899);
            accept = serverSocket.accept();
            inputStream = accept.getInputStream();
            //不建议这样写
//        byte[] buffer =new byte[20];
        int len;
        while ((len=inputStream.read(buffer))!=-1){
            String s = new String(buffer, 0, len);
            System.out.println(s);
        }
            byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[20];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, len);

            }
            System.out.println(byteArrayOutputStream.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (byteArrayOutputStream != null) {

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

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

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

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


    }

2.实例:利用网络发送图片(抛出异常方式应该使用try-catch-finally处理,此处偷懒抛出了)

 @Test
    public void client() throws IOException {
        //
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
        //
        OutputStream outputStream = socket.getOutputStream();
        //
        FileInputStream fis = new FileInputStream("001.jpg");
        byte[] buffer = new byte[5];
        int len;
        while ((len = fis.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);

        }
        fis.close();
        outputStream.close();
        socket.close();


    }

    //服务端
    @Test
    public void sever() throws IOException {
        //
        ServerSocket socket=new ServerSocket(9090);
        //
        Socket accept = socket.accept();
        //
        InputStream inputStream = accept.getInputStream();
        //
        FileOutputStream fos=new FileOutputStream("555.jpg");
        byte[] buffer=new  byte[5];
        int len;
        while ((len=inputStream.read(buffer))!=-1){
            fos.write(buffer,0,len);

        }

    }

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

public class NetTest03 {
    //客户端
    @Test
    public void client() throws IOException {
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9091);
        OutputStream ops = socket.getOutputStream();
        FileInputStream fis = new FileInputStream("001.jpg");
        byte[] buffer = new byte[5];
        int len;
        while ((len = fis.read(buffer)) != -1) {
            ops.write(buffer, 0, len);
        }
        socket.shutdownOutput();
        InputStream inputStream = socket.getInputStream();
        ByteArrayOutputStream baop = new ByteArrayOutputStream();
        byte[] buff = new byte[5];
        int len2 = 0;
        while ((len2 = inputStream.read(buff)) != -1) {
            baop.write(buff, 0, len2);
        }
        System.out.println(baop.toString());
        fis.close();
        ops.close();
        socket.close();
        baop.close();
        inputStream.close();


    }

    //服务端
    @Test
    public void sever() throws IOException {
        ServerSocket socket = new ServerSocket(9091);
        Socket accept = socket.accept();
        InputStream inputStream = accept.getInputStream();
        FileOutputStream fos = new FileOutputStream("777.jpg");
        byte[] buffer = new byte[5];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        //反馈客户端
        OutputStream os = accept.getOutputStream();
        os.write("已收到".getBytes());


        fos.close();
        inputStream.close();
        accept.close();
        socket.close();
        os.close();

    }
}

二、UDP编程(发送数据报)

(抛出异常方式应该使用try-catch-finally处理,此处偷懒抛出了)

public class UDPTest {
    public void sender() throws IOException {
        DatagramSocket socket = new DatagramSocket();
        InetAddress ia = InetAddress.getLocalHost();
        String ss = "我是udp报文";
        byte[] bytes = ss.getBytes();
        DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length, ia, 8888);
        socket.send(datagramPacket);
        socket.close();

    }

    public void reciver() throws IOException {
        DatagramSocket socket = new DatagramSocket(8888);
        byte[] by=new byte[100];
        DatagramPacket packet = new DatagramPacket(by, 0, by.length);
        socket.receive(packet);
        System.out.println(new String(packet.getData(),0,packet.getLength()));
        socket.close();

    }
}

三、URL网络编程
概念:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值