博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
URL 和 UDP 测试
阅读量:5035 次
发布时间:2019-06-12

本文共 2802 字,大约阅读时间需要 9 分钟。

1.URL

1096107-20170220231358851-2085535735.png
1096107-20170220231813710-347713174.png
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import java.net.URL;

public class Test_URL {

public static void main(String[] args) throws IOException, InterruptedException {
URL url = new URL("");
System.out.println("URL:"+url.toString()+"\n"
+"协议:"+url.getProtocol()+"\n"
+"域名:"+url.getHost()+"\n"
+"端口:"+(url.getPort()==-1? 80:url.getPort())+"\n"
+"资源:"+url.getFile()+"\n"
+"锚点:"+url.getRef()+"\n"
+"参数:"+url.getQuery()+"\n" //若锚点存在,则返回null
+"相对路径:"+url.getPath());
Thread.sleep(3000);
BufferedReader br =new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("test/www.html"), "UTF-8"));
String msg = null;
while(null!=(msg=br.readLine())){
System.out.println(msg);
bw.append(msg);
bw.newLine();
}
bw.flush();
bw.close();
br.close();
}
}
2.UDP
1096107-20170220231908601-953284184.png

import java.io.IOException;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

public class Test_UDP {

public static class Server implements Runnable {        public void run() {        try {            DatagramSocket server;            server = new DatagramSocket(3333);            byte[] container = new byte[1024];            DatagramPacket packet = new DatagramPacket(container, container.length);            server.receive(packet);            byte[] data = packet.getData();            int len = packet.getLength();            System.out.println("服务器已接受信息");            Thread.sleep(800);            System.out.println("信息:"+new String(data, 0, len));            server.close();        } catch (IOException e) {            e.printStackTrace();        } catch (InterruptedException e) {            e.printStackTrace();        }    }}public static class Client implements Runnable {        public void run() {        try {            DatagramSocket client  = new DatagramSocket(5555);            String msg = "udp测试成功!";            byte[] data = msg.getBytes();            DatagramPacket packet = new DatagramPacket(data, data.length, new InetSocketAddress("myLaptop", 3333));            client.send(packet);            client.close();        } catch (IOException e) {            e.printStackTrace();        }    }}public static void main(String[] args) throws IOException, InterruptedException {       new Thread(new Server()).start();    System.out.println("服务器接收就绪");    Thread.sleep(1600);    System.out.print("客户端正在发送信息");    for(int i=0; i<3; i++){        Thread.sleep(400);        System.out.print(".");    }    System.out.println();    Thread.sleep(1200);    new Thread(new Client()).start();}

}

转载于:https://www.cnblogs.com/liu-shui/p/6422082.html

你可能感兴趣的文章
android 拍照上传照片
查看>>
ArchLinux安装开源VMware Tools
查看>>
系统用户分析模型
查看>>
DB2 锁升级示例1
查看>>
16.RDD实战
查看>>
MainFrame知识小结(20120210)—dfsort/syncsort中的数据类型
查看>>
jsp题库 (一)小测(25/21)
查看>>
D - Flip tile
查看>>
Java连接RabbitMQ之创建连接
查看>>
开户vim编程之--cscope支持
查看>>
python数据类型图解
查看>>
C#微信登录-手机网站APP应用
查看>>
HTML5实践 -- iPhone Safari Viewport Scaling Bug
查看>>
一位数据挖掘成功人士 给 数据挖掘在读研究生 的建议
查看>>
Python3.6.0安装
查看>>
hdu1049
查看>>
H5项目常见问题及注意事项
查看>>
索尼(SONY) SVE1512S7C 把WIN8降成WIN7图文教程
查看>>
时间模块 && time datetime
查看>>
jquery自动生成二维码
查看>>