- 套接字(Socket)由IP地址和端口号组成。
- IP地址可以自行查询手动输入,也可以利用域名通过InetAddress.getByName()获取。
- 访问量较大的域名通过对应多个IP地址实现负载均衡,访问时InetAddress.getByName()会随机获取其中一个IP地址,InetAddress.getAllByName()会获取所有的IP地址。
- 服务器的输出会成为客户端的输入,客户端的输出会成为服务器的输入。
- 服务器的工作流程:
(1) 通过输入流接收客户端输出流发送的请求命令;
(2) 解析请求命令;
(3) 收集所需信息;
(4) 通过输出流发送信息给客户端;
(5) 循环执行以上步骤。 - TCP连接是先开服务器,再开客户端;UDP连接是先开接收方,再开发送方。
- 利用多线程可以使服务器同时为多个客户端提供服务。
- 统一资源标识符(URI)包含用来指定Web资源的字符串的各种组成部分。
- 统一资源定位符(URL):协议://IP地址:端口号/文件目录,URL是URI的一个特例。
- 使用URLConnection及其子类获取Web资源,一般使用HttpURLConnection类。
import org.junit.Test;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
public class InternetTest {
/**
* TCP服务器
*/
@Test
public void server() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
OutputStream outputStream = null;
try {
serverSocket = new ServerSocket(8189);
socket = serverSocket.accept();
inputStream = socket.getInputStream();
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len;
while ((len = inputStream.read(data)) != -1) {
byteArrayOutputStream.write(data, 0, len);
}
System.out.println(byteArrayOutputStream);
outputStream = socket.getOutputStream();
outputStream.write((LocalDateTime.now() + " Hello, client! This is server.").getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* TCP客户端
*/
@Test
public void client() {
Socket socket = null;
OutputStream outputStream = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
socket = new Socket(InetAddress.getLocalHost(), 8189);
outputStream = socket.getOutputStream();
outputStream.write((LocalDateTime.now() + " Hello, server! This is client.").getBytes(StandardCharsets.UTF_8));
socket.shutdownOutput();
inputStream = socket.getInputStream();
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len;
while ((len = inputStream.read(data)) != -1) {
byteArrayOutputStream.write(data, 0, len);
}
System.out.println(byteArrayOutputStream);
} 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 (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* UDP接收方
*/
@Test
public void receiver() {
DatagramSocket datagramSocket = null;
DatagramPacket datagramPacket = null;
try {
datagramSocket = new DatagramSocket(8189);
byte[] data = new byte[1024];
datagramPacket = new DatagramPacket(data, 0, data.length);
datagramSocket.receive(datagramPacket);
System.out.println(new String(datagramPacket.getData(), 0, datagramPacket.getLength()));
} catch (IOException e) {
e.printStackTrace();
} finally {
datagramSocket.close();
}
}
/**
* UDP发送方
*/
@Test
public void sender() {
DatagramSocket datagramSocket = null;
DatagramPacket datagramPacket = null;
try {
datagramSocket = new DatagramSocket();
String s = LocalDateTime.now() + " Hello, receiver. This is sender.";
datagramPacket = new DatagramPacket(s.getBytes(StandardCharsets.UTF_8), 0, s.length(), InetAddress.getLocalHost(), 8189);
datagramSocket.send(datagramPacket);
} catch (IOException e) {
e.printStackTrace();
} finally {
datagramSocket.close();
}
}
/**
* 获取Web资源
*/
@Test
public void url() {
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
URL url = new URL("https://img9.doubanio.com/view/photo/l_ratio_poster/public/p2641910067.jpg");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
inputStream = urlConnection.getInputStream();
outputStream = new FileOutputStream("D:\\Desktop\\Image.jpg");
byte[] data = new byte[1024];
int len;
while ((len = inputStream.read(data)) != -1) {
outputStream.write(data, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
urlConnection.disconnect();
}
}
}