64.【网络编程】(四)

简介: 64.【网络编程】

2.2(多线程实现双面的输出)

发送者是:学生/接收者是老师
package com.Kuangstudy.Net;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
//发送者1
public class UDPSenderDmeo1 implements Runnable {
        DatagramSocket datagramSocket=null;
        BufferedReader bufferedReader=null;
        private int fromPort;
        private String toIP;
        private int toPort;
        public UDPSenderDmeo1(){
        }
    public UDPSenderDmeo1( int fromPort, String toIP, int toPort) throws Exception {
        this.fromPort = fromPort;
        this.toIP = toIP;
        this.toPort = toPort;
        datagramSocket=new DatagramSocket(fromPort);
        bufferedReader=new BufferedReader(new InputStreamReader(System.in));
    }
    @Override
        public void run()  {
        while (true) {
            try {
                //读取一行
                String string_one = bufferedReader.readLine();
                byte[] buf = string_one.getBytes();
                //打包
                DatagramPacket packet = new DatagramPacket(buf, 0, buf.length, new InetSocketAddress(this.toIP, this.toPort));  //发送给谁?
                //发送包
                datagramSocket.send(packet);
                if (buf.equals("bye")) {
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        datagramSocket.close();
    }
}
发送者是:老师 接收者是:学生
package com.Kuangstudy.Net;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
//接收者1
public class UDPSenderDmeo2 implements Runnable {
    private String msgFrom;
    DatagramSocket datagramSocket=null;
    private int port;
    public UDPSenderDmeo2() {
    }
    public UDPSenderDmeo2(int port,String msgFrom) {
        this.msgFrom=msgFrom;
        this.port = port;
        try {
            datagramSocket=new DatagramSocket(port);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    @Override
    public void run() {
        while (true) {
            try {
                //准备接受包裹
                byte[] buf = new byte[1024];
                DatagramPacket datagramPacket = new DatagramPacket(buf, 0, buf.length );
                datagramSocket.receive(datagramPacket);   //阻塞接受
                //断开连接
                byte []buf_two=datagramPacket.getData();  //获得数据
                //接受的数据
                String string_one=new String(buf_two,0,buf_two.length);
                //打印输出
                System.out.println(msgFrom+":"+string_one.trim());
                if (string_one.trim().equals("bye".trim())){
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        datagramSocket.close();
    }
}
实现发送者是:学生的接口 ,接收者是:老师
import com.Kuangstudy.Net.UDPSenderDmeo1;
import com.Kuangstudy.Net.UDPSenderDmeo2;
public class TalkStudent {
    public static void main(String[] args) throws Exception {
        //开启两个线程
        new Thread(new UDPSenderDmeo1(7777,"localhost",9999)).start();
        new Thread(new UDPSenderDmeo2(8888,"老师")).start();
    }
}
实现发送者是:老师的接口 ,接收者是:学生
• 1
import com.Kuangstudy.Net.UDPSenderDmeo1;
import com.Kuangstudy.Net.UDPSenderDmeo2;
public class TalkTeacher_02 {
    public static void main(String[] args) throws Exception{
        new Thread(new UDPSenderDmeo1(5555,"localhost",8888)).start();
        new Thread(new UDPSenderDmeo2(9999,"学生")).start();
    }
}

(八)、URL

1.定义:
统一资源定位符:定位资源的,定位互联网上的某一项。
2.协议:      //IP:端口/项目名/资源
3.
(1). DNS 域名解析  www.baidu.com 实质上是:IP

1.爬取资源包

import com.Kuangstudy.Net.UDPSenderDmeo1;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class Demo2 {
    public static void main(String[] args) throws Exception{
        //下载文件
        URL url = new URL("");
        //2.链接此资源
        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
        //3.
        InputStream inputStream = urlConnection.getInputStream();
        FileOutputStream fileOutputStream=new FileOutputStream("Security.jpg");
        byte []buf=new byte[1024];
        int count=0;
        while ((count=inputStream.read(buf))!=-1){
            fileOutputStream.write(buf,0,count);
        }
        inputStream.close();
        fileOutputStream.close();
        urlConnection.disconnect();
    }
}

2.URL知识点

import java.net.URL;
public class Demo1 {
    public static void main(String[] args)throws Exception {
        URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=liming&password=123");
        System.out.println(url.getProtocol());  //得到协议名
        System.out.println(url.getHost());      //得到主机名
        System.out.println(url.getPort());      //得到端口号
        System.out.println(url.getPath());      //获得文件
        System.out.println(url.getFile());      //全路径
        System.out.println(url.getQuery());     //全参数
    }
}

"http://dl.stream.qqmusic.qq.com/C400000LQTRH4d6iQO.m4a?guid=3228671748&vkey=32B4FCD4F80B08521BEA1EB37A2AF2B1C6433807F310F20C8F78D52F4976FCC1114EC84B5EEDAE4827FA3928DEB1880E807485D82E7C1760&uin=&fromtag=120032"
相关文章
|
6月前
|
消息中间件 Kubernetes 网络协议
网络编程一些问题总结
网络编程一些问题总结
|
2月前
|
网络协议 程序员 API
初识网络编程
本文介绍了网络编程的重要概念,包括IP地址、端口号和协议。IP地址是设备在网络中的唯一标识,IPv4已用尽,IPv6提供了更多地址。端口号用于区分设备上的不同应用程序,取值范围为0~65535。协议定义了网络传输规则,常分为TCP/IP五层模型和OSI七层模型。文章还讨论了TCP与UDP的区别,并提供了UDP协议的简单示例。
47 0
初识网络编程
|
6月前
|
网络协议 API
|
6月前
|
Linux
网络编程之 socket编程
网络编程之 socket编程
68 0
|
网络协议 关系型数据库 MySQL
网络编程初识
网络编程初识
56 0
|
Java C++
4. 网络编程
4. 网络编程
57 0
|
应用服务中间件
64.【网络编程】(三)
64.【网络编程】
41 0
|
网络协议
64.【网络编程】(一)
64.【网络编程】
41 0
|
网络协议
64.【网络编程】(二)
64.【网络编程】
39 0