UDP协议的网络编程
发送端
@Test
public void sender() throws IOException {
DatagramSocket socket = new DatagramSocket();
String str = "我是UDP方式发送的导弹";
byte[] data = str.getBytes();
InetAddress inet = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data, 0, data.length, inet, 9090);
socket.send(packet);
socket.close();
}
接收端
@Test
public void receiver() throws IOException {
DatagramSocket socket = new DatagramSocket(9090);
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();
}
URL网络编程(Uniform Resource Locator)
-
URL:统一资源定位符,对应着互联网的某一资源地址
-
格式:
http://localhost:8080/examples/beauty.jpg?username=Tom
协议 主机名 端口号 资源地址 参数列表
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的查询名 |
package com.atguigu.java1;
import java.net.MalformedURLException;
import java.net.URL;
public class URLTest {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8080/examples/beauty.jpg?username=Kevin");
// public String getProtocol() 获取该URL的协议名
System.out.println(url.getProtocol());
// public String getHost() 获取该URL的主机名
System.out.println(url.getHost());
// public String getPort() 获取该URL的端口号
System.out.println(url.getPort());
// public String getPath() 获取该URL的文件路径
System.out.println(url.getPath());
// public String getFile() 获取该URL的文件名
System.out.println(url.getFile());
// public String getQuery() 获取该URL的查询名
System.out.println(url.getQuery());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
URL实现数据下载
package com.atguigu.java1;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLTest1 {
public static void main(String[] args) {
HttpURLConnection urlConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL("http://localhost:8080/examples/beauty.jpg");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
is = urlConnection.getInputStream();
fos = new FileOutputStream("day10/beauty1.jpg");
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1) {
fos.write(buffer,0,len);
}
System.out.println("下载完成");
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(urlConnection != null) {
urlConnection.disconnect();
}
}
}
}