UDP协议
使用UDP协议来进行网络交流与TCP有些不同
TCP协议注重流的传输,连接套接字后要有流的变化
UDP注重数据报的传输,建立套接字后要再建立一个数据包
数据包一般用数组形式表示
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;
import javax.swing.plaf.synth.SynthSpinnerUI;
public class Receiver {
public static void main(String[] args) {
try {
DatagramSocket receive = new DatagramSocket(8080);
DatagramSocket send = new DatagramSocket();
byte[] arr = new byte[1024];
Scanner scanner = new Scanner(System.in);
while(true){
DatagramPacket receiver = new DatagramPacket(arr, 1024);
receive.receive(receiver);
System.out.println("Receiver接收到:"+new String(arr));
String sendstr = scanner.next();
System.out.println("Receiver发送:"+sendstr);
byte[] data = sendstr.getBytes();
DatagramPacket packetsend = new DatagramPacket(data,
data.length, InetAddress.getByName("LocalHost"), 8088);
send.send(packetsend);
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;
public class Sender {
public static void main(String[] args) {
try {
DatagramSocket receive = new DatagramSocket(8080);
DatagramSocket send = new DatagramSocket();
Scanner scanner = new Scanner(System.in);
byte [] arr = new byte[1024];
while(true){
String s = scanner.next();
byte [] array = s.getBytes();
DatagramPacket sender = new DatagramPacket(array,
array.length,InetAddress.getByName("LocalHost"), 8080);
System.out.println("Sender发送:"+s);
send.send(sender);
DatagramPacket packetreceive = new DatagramPacket(arr, arr.length);
receive.receive(packetreceive);
byte[] data = packetreceive.getData();
System.out.println("Sender收到:"+new String(data,0,data.length));
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果:
URL
类 URL 代表一个统一资源定位符,它是指向互联网“资源”的指针。资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用。
找到网站对应的源文件
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
public class URLTest {
public static void main(String[] args) {
try {
URL url = new URL("https://www.baidu.com");
InputStream in = url.openStream();
InputStreamReader reader = new InputStreamReader(in);
BufferedReader br = new BufferedReader(reader);
String l = br.readLine();
while(l!=null){
System.out.println(l);
l = br.readLine();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果:
把网页上的图片转存到系统中
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class ImgTest {
public static void main(String[] args) {
try {
URL url = new URL("http://img.sccnn.com/bimg/307/013.jpg");
File fileImg = new File("e://youpiao.jpg");
if(!fileImg.exists()){
fileImg.createNewFile();
}
OutputStream out =new FileOutputStream(fileImg);
InputStream in = url.openStream();
byte[] arr = new byte[1024];
int i = in.read(arr);
while(i!=-1){
out.write(arr, 0, i);
i = in.read(arr);
}
out.flush();
out.close();
in.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果: