1、主机IP地址获取
步骤一:获取主机InetAddress
步骤二:获取主机IP地址
InetAddress address = null;
try {
address = InetAddress.getByName("www.baidu.com");
}
catch (UnknownHostException e) {
System.exit(2);
}
System.out.println("HostName" + "\t\t"
+ "IPAddress");
System.out.println(address.getHostName() + "\t\t"
+ address.getHostAddress());
System.exit(0);
输出结果
HostName IPAddress
www.baidu.com 180.101.50.242
2、查看端口使用情况
public static void main(String[] args) {
Socket Skt;
String host = "localhost";
if (args.length > 0) {
host = args[0];
}
for (int i = 0; i < 1024; i++) {
try {
System.out.println("查看 "+ i);
Skt = new Socket(host, i);
System.out.println("端口 " + i + " 已被使用");
}
catch (UnknownHostException e) {
System.out.println("Exception occured"+ e);
break;
}
catch (IOException e) {
}
}
}
输出结果
……
查看 17
查看 18
查看 19
查看 20
查看 21
端口 21 已被使用
查看 22
查看 23
查看 24
……
3、获取本机ip地址及主机名
public static void main(String[] args) throws IOException, InterruptedException {
InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
System.out.println("主机IP地址:"+inetAddress.getHostAddress());
System.out.println("主机名称:"+inetAddress.getHostName());
}
输出结果
主机IP地址:180.101.50.242
主机名称:www.baidu.com
4、获取文件大小
步骤一:获取URL对象 new URL(“资源路径”);
步骤二:连接URL链接 URLConnection conn = url.openConnection();
步骤三:获取文件大小 conn.getContentLength();
URL url = new URL("资源路径"); //获取URL对象
URLConnection conn = url.openConnection(); //打开URL资源器
int size = conn.getContentLength(); //文件大小
5、多线程Socket
public class dictoryImpl implements Runnable {
Socket clientSocket;
public dictoryImpl(Socket clientSocket) {
this.clientSocket = clientSocket;
}
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(1234);
System.out.println("listening");
while(true){
Socket socket = serverSocket.accept();
System.out.println("connectionLocalPort:"+socket.getLocalPort());
System.out.println("connectionCurrentPort:"+socket.getPort());
new Thread(new dictoryImpl(socket)).start();
}
}
@Override
public void run() {
try {
PrintStream printStream = new PrintStream(clientSocket.getOutputStream());
for (int i = 0; i < 100; i++) {
printStream.println(i + " bottles of beer on the wall.");
}
printStream.close();
clientSocket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
class client{
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost",1234);
socket.close();
}
}
输出结果
listening
connectionLocalPort:1234
connectionCurrentPort:3799
6、查看主机指定文件的最后修改时间
public static void main(String[] args) throws IOException {
URL url = new URL("资源路径");
URLConnection uc = url.openConnection();
uc.setUseCaches(false);
long timestamp = uc.getLastModified();
System.out.println("java.bmp 文件最后修改时间 :"+timestamp);
}
7、使用Socket连接到指定主机
public static void main(String[] args) throws IOException {
InetAddress inetAddress;
Socket socket = new Socket("www.baidu.com",80);
inetAddress = socket.getInetAddress();
System.out.println("连接到 "+inetAddress);
socket.close();
}
输出结果
连接到 www.baidu.com/180.101.50.188
8、网页抓取
public static void main(String[] args) throws IOException {
URL url = new URL("https://blog.csdn.net/Zain_horse/article/details/129048338");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = bufferedReader.readLine()) != null){
System.out.println(line);
}
bufferedReader.close();
}
输出结果
9、获取响应头日期
public static void main(String[] args) throws IOException {
URL url = new URL("https://blog.csdn.net/Zain_horse/article/details/129048338");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
long date = httpURLConnection.getDate();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
if (date == 0){
System.out.println("无法获取Header");
}else {
System.out.println("Date:"+simpleDateFormat.format(date));
}
}
输出结果
Date:2023-02-22 22:08:26 下午
10、获取相应头信息
public static void main(String[] args) throws IOException {
URL url = new URL("https://blog.csdn.net/Zain_horse/article/details/129048338");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
Map<String, List<String>> listMap = httpURLConnection.getHeaderFields();
Set<String> keySet = listMap.keySet();
for (String key:keySet) {
String val = httpURLConnection.getHeaderField(key);
System.out.println(key+":"+val);
}
}
输出结果
Keep-Alive:timeout=20
Transfer-Encoding:chunked
null:HTTP/1.1 200 OK
Strict-Transport-Security:max-age= 31536000
Server:openresty
Connection:keep-alive
Set-Cookie:dc_session_id=10_1677075301490.865484; Expires=Thu, 01 Jan 2025 00:00:00 GMT; Path=/; Domain=.csdn.net;
Vary:Accept-Encoding
Content-Language:en-US
Date:Wed, 22 Feb 2023 14:15:01 GMT
Content-Type:text/html;charset=utf-8
11、解析URL
public static void main(String[] args) throws IOException {
URL url = new URL("https://blog.csdn.net/Zain_horse/article/details/129048338");
System.out.println(url);
System.out.println(url.getProtocol()+"://"+url.getHost()+url.getPort()+url.getPath());
}
输出结果
https://blog.csdn.net/Zain_horse/article/details/129048338
https://blog.csdn.net-1/Zain_horse/article/details/129048338
12、C/S Socekt之间通信
服务器端建立过程:
- 服务器建立通信ServerSocket
- 服务器建立Socket接收客户端连接
- 建立IO输入流读取客户端发送的数据
- 建立IO输出流向客户端发送数据消息
ServerSocket 服务器端
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8888);
System.out.println("启动服务器....");
Socket s = ss.accept();
System.out.println("客户端:"+s.getInetAddress().getLocalHost()+"已连接到服务器");
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//读取客户端发送来的消息
String mess = br.readLine();
System.out.println("客户端:"+mess);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw.write(mess+"\n");
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端Client建立过程:
- 创建Socket通信,设置通信服务器的IP和Port
- 建立IO输出流向服务器发送数据消息
- 建立IO输入流读取服务器发送来的数据消息
ClientSoceket 客户端
/*
author by w3cschool.cn
Main.java
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1",8888);
//构建IO
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
//向服务器端发送一条消息
bw.write("测试客户端和服务器通信,服务器接收到消息返回到客户端\n");
bw.flush();
//读取服务器返回的消息
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String mess = br.readLine();
System.out.println("服务器:"+mess);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务器端结果:
启动服务器....
客户端结果:
服务器:测试客户端和服务器通信,服务器接收到消息返回到客户端