Java中的网络通信协议:TCP/IP协议、HTTP协议、FTP协议
一、TCP/IP协议
1. Socket类
Socket类是Java中用于实现TCP/IP协议的基本类,它提供了建立网络连接、发送数据、接收数据等基本功能。
import java.net.Socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class TCPClient {
public static void main(String[] args) {
Socket client = null;
try {
// 创建Socket对象
client = new Socket("localhost", 8888);
// 获取输出流对象
OutputStream os = client.getOutputStream();
// 发送数据
os.write("Hello, TCP!".getBytes());
os.flush();
// 获取输入流对象
InputStream is = client.getInputStream();
byte[] buffer = new byte[1024];
int len;
// 接收数据
if ((len = is.read(buffer)) != -1) {
System.out.println(new String(buffer, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2. ServerSocket类
ServerSocket类是Java中用于实现TCP/IP协议的服务器端基本类,它提供了建立服务器、等待客户端连接、接受数据等基本功能。
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class TCPServer {
public static void main(String[] args) {
ServerSocket server = null;
Socket client = null;
try {
// 创建ServerSocket对象
server = new ServerSocket(8888);
// 等待客户端连接
System.out.println("等待客户端连接...");
client = server.accept();
System.out.println("客户端连接成功");
// 获取输入流对象
InputStream is = client.getInputStream();
byte[] buffer = new byte[1024];
int len;
// 接收数据
if ((len = is.read(buffer)) != -1) {
System.out.println(new String(buffer, 0, len));
}
// 获取输出流对象
OutputStream os = client.getOutputStream();
// 发送数据
os.write("Hello, TCP!".getBytes());
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (server != null) {
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
二、HTTP协议
1. HttpURLConnection类
HttpURLConnection类是Java中用于实现HTTP协议的基本类,它提供了建立网络连接、发送请求、接收响应等基本功能。
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class HTTPClient {
public static void main(String[] args) {
HttpURLConnection conn = null;
InputStream is = null;
BufferedReader br = null;
try {
// 创建URL对象
URL url = new URL("http://www.baidu.com");
// 打开连接
conn = (HttpURLConnection) url.openConnection();
// 设置请求方法
conn.setRequestMethod("GET");
// 设置连接超时时间
conn.setConnectTimeout(5000);
// 设置读取超时时间
conn.setReadTimeout(5000);
// 获取输入流
is = conn.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
String line;
// 读取数据
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (conn != null) {
conn.disconnect();
}
}
}
}
2. HttpClient类
HttpClient类是Apache Commons中用于实现HTTP协议的基本类,它提供了更加丰富和灵活的功能,例如支持HTTPS、支持Cookie、支持代理、支持文件上传等。
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class HTTPClient {
public static void main(String[] args) {
HttpClient client = HttpClientBuilder.create().build();
HttpGet get = new HttpGet("http://www.baidu.com");
try {
// 执行请求
HttpResponse response = client.execute(get);
// 获取响应数据
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
}
三、FTP协议
1. FTPClient类
FTPClient类是Apache Commons中用于实现FTP协议的基本类,它提供了建立连接、登录、上传文件、下载文件等基本功能。
import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;
public class FTPClient {
public static void main(String[] args) {
FTPClient client = new FTPClient();
InputStream is = null;
FileOutputStream fos = null;
try {
// 连接FTP服务器
client.connect("ftp.example.com", 21);
// 登录FTP服务器
client.login("username", "password");
// 切换到指定目录
client.changeWorkingDirectory("/test");
// 获取指定文件
is = client.retrieveFileStream("test.txt");
// 创建本地文件
fos = new FileOutputStream("C:/test.txt");
byte[] buffer = new byte[1024];
int len;
// 读取数据
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} 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 (client.isConnected()) {
try {
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2. FTPSClient类
FTPSClient类是Apache Commons中用于实现FTPS协议的基本类,它提供了更加安全的数据传输和更加灵活的配置,例如支持SSL/TLS、支持自定义证书、支持传输模式等。
import org.apache.commons.net.ftp.FTPSClient;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;
public class FTPSClient {
public static void main(String[] args) {
FTPSClient client = new FTPSClient();
InputStream is = null;
FileOutputStream fos = null;
try {
// 连接FTP服务器
client.connect("ftp.example.com", 21);
// 登录FTP服务器
client.login("username", "password");
// 切换到指定目录
client.changeWorkingDirectory("/test");
// 获取指定文件
is = client.retrieveFileStream("test.txt");
// 创建本地文件
fos = new FileOutputStream("C:/test.txt");
byte[] buffer = new byte[1024];
int len;
// 读取数据
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} 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 (client.isConnected()) {
try {
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
本文为翻滚的胖子原创文章,转载无需和我联系,但请注明来自猿教程iskeys.com
