目录
InetAddress类
相关方法
- 获取本机InetAddress对象 getLocalHost
- 根据指定主机名/域名获取ip地址对象 getByName
- 获取InetAddress对象的主机名 getHostName
- 获取InetAddress对象的地址 getHostAddress
//获取计算机的主机名和IP地址相关API
public class API_ {
public static void main(String[] args) throws UnknownHostException {
// 获取本机InetAddress对象 getLocalHost
InetAddress localHost = InetAddress.getLocalHost();
System.out.println("本机的InetAddress对象为"+localHost);
// 根据指定主机名/域名获取ip地址对象 getByName
InetAddress byName = InetAddress.getByName("DESKTOP-4P4RENO");
System.out.println("本机的IP地址为"+byName);
InetAddress byName1 = InetAddress.getByName("www.baidu.com");
System.out.println("百度的IP地址为"+byName1);
// 获取InetAddress对象的主机名 getHostName
String hostName = localHost.getHostName();
System.out.println("本机的主机名为"+hostName);
// 获取InetAddress对象的地址 getHostAddress
String hostAddress = localHost.getHostAddress();
System.out.println("本机的IP地址为"+hostAddress);
}
}
运行结果:
本机的InetAddress对象为DESKTOP-4P4RENO/192.168.1.108
本机的IP地址为DESKTOP-4P4RENO/192.168.1.108
百度的IP地址为www.baidu.com/110.242.68.3
本机的主机名为DESKTOP-4P4RENO
本机的IP地址为192.168.1.108
Socket(插口)
- 套接字(socket)开发网络安全应用程序被广泛采用,以至于成为事实上的标准
- 通信的两端都要有Socket,是两台机器间通信的端点
- 网络通信其实就时Socket间的通信
- Socket允许程序把网络连接当成一个流,数据在两个Socket间通过IO传输
- 一般主动发起通信的应用程序属客户端,等待通信请求的为服务端
TCP 网络通信编程
基本介绍
- 基于用户端——服务端的网络通信
- 底层使用的时TCP/IP协议
- 应用场景举例:客户端发送数据,服务端接受并显示控制台
- 基于Socket的TCP编程
@应用案例1 (使用字节流)
-
编写一个服务器端和一个客户端
-
服务器端在9999端口监听
-
客户端连接到服务器端,发送"hello,server",然后退出
- 服务器端接收到客户端发送的信息,输出,并退出
思路分析:
先看代码:(注释详细)
服务端代码:
//服务端
public class SocketTCP01Server {
public static void main(String[] args) throws IOException {
// 1.在本机的9999端口监听,等待连接。
// 要求在本机内没有其他服务监听9999端口
// ServerSocket()方法传入一个端口
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端,在9999端口监听,等待连接.....");
// 2.当没有客户端连接9999端口时,程序会阻塞,等待连接
// 如果有客户端连接,则会返回Socket对象,程序继续
Socket socket = serverSocket.accept();
System.out.println("服务端 socket= " + socket.getClass());
// 3.通过socket.getInputStream()读取,客户端写入到数据通道的数据,然后显示
InputStream inputStream = socket.getInputStream();
// 4.IO读取
byte[] bytes = new byte[1024];
int readlen = 0;
while((readlen = inputStream.read(bytes)) != -1){
System.out.println(new String(bytes,0,readlen));//根据读取到的实际长度显示内容
}
// 5.关闭流和Socket
inputStream.close();
socket.close();
serverSocket.close();
}
}
客户端代码:
//客户端,发送hello,server给服务端
public class SocketTCP01Client {
public static void main(String[] args) throws IOException {
// 1.连接服务端(ip,端口)
// 获取本机InetAddress对象 getLocalHost
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println("客户端Socket 返回"+socket.getClass());
// 2.连接上后,生成socket,通过socket.getOututStream(),得到和socket对象关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
// 3.通过输出流,写入数据到数据通道
outputStream.write("hello,server".getBytes());//getBytes将字符串改为字节序列
// 5.关闭流和Socket
socket.close();
outputStream.close();
System.out.println("客户端退出");
}
}
运行结果:
1. 客户端没运行,服务端运行了的窗口(当客户端没有连接9999端口时):
注意:此时程序处于堵塞状态,等待连接
2. 当客户端和服务端都运行时,客户端的窗口:(此时客户端连接了9999端口)
3.当客户端和服务端都运行,服务端的窗口:
@应用案例2(使用字节流)
-
编写一个服务器端和一个客户端
-
服务器端在9999端口监听
-
客户端连接到服务器端,发送"hello,server",然后退出
- 服务器端接收到客户端发送的信息,输出,并发送“hello,client",再退出
该题在上一个案例修改即可(注意需要设置结束标记)
//设置结束标记 socket.shutdownOutput();
服务端代码:
public class SocketTCP02Server {
public static void main(String[] args) throws IOException {
// 1.在本机的9999端口监听,等待连接。
// 要求在本机内没有其他服务监听9999端口
// ServerSocket()方法传入一个端口
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端,在9999端口监听,等待连接.....");
// 2.当没有客户端连接9999端口时,程序会阻塞,等待连接
// 如果有客户端连接,则会返回Socket对象,程序继续
Socket socket = serverSocket.accept();
System.out.println("服务端 socket= " + socket.getClass());
// 3.通过socket.getInputStream()读取,客户端写入到数据通道的数据,然后显示
InputStream inputStream = socket.getInputStream();
// 4.IO读取
byte[] bytes = new byte[1024];
int readlen = 0;
while((readlen = inputStream.read(bytes)) != -1){
System.out.println(new String(bytes,0,readlen));//根据读取到的实际长度显示内容
}
// 写入
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello,Client".getBytes());
//设置结束标记
socket.shutdownOutput();
// 5.关闭流和Socket
outputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
客户端代码:
public class SocketTCP02Client {
public static void main(String[] args) throws IOException {
// 1.连接服务端(ip,端口)
// 获取本机InetAddress对象 getLocalHost
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println("客户端Socket 返回"+socket.getClass());
// 2.连接上后,生成socket,通过socket.getOututStream(),得到和socket对象关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
// 3.通过输出流,写入数据到数据通道
outputStream.write("hello,server".getBytes());//getBytes将字符串改为字节序列
socket.shutdownOutput();
// 4.IO读取
byte[] buf = new byte[1024];
int readlen = 0;
InputStream inputStream = socket.getInputStream();
while((readlen = inputStream.read(buf))!= -1){
System.out.println(new java.lang.String(buf,0,readlen));
}
// 5.关闭流和Socket
inputStream.close();
socket.close();
outputStream.close();
System.out.println("客户端退出");
}
}
运行结果:
@应用案例3 (使用字符流)
-
编写一个服务器端和一个客户端
-
服务器端在9999端口监听
-
客户端连接到服务器端,发送"hello,server",然后退出
- 服务器端接收到客户端发送的信息,输出,并发送“hello,client",再退出
和前面两例差不多,只是字节流和字符流的区别
注意:
在使用字符输入流时:
bufferedWriter.newLine();//插入一个换行符,表示回复内容的结束
bufferedWriter.flush();//注意需要手动的flush
客户端代码:
public class SocketTCP03Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
//写入数据到数据通道
OutputStream outputStream = socket.getOutputStream();
//转换流OutputStreamWriter 字符流BufferedWrite
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello,Server");
bufferedWriter.newLine();//插入一个换行符,表示回复内容的结束
bufferedWriter.flush();//注意需要手动的flush
//读取服务端回复的消息
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String s = bufferedReader.readLine();
System.out.println(s);
//关闭流
bufferedReader.close();
bufferedWriter.close();//关闭最外层流即可
socket.close();
}
}
服务端代码:
public class SocketTCP03Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端等待连接");
Socket socket = serverSocket.accept();
//读取客户端写入到数据通道的数据
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String s = bufferedReader.readLine();
System.out.println(s);
//回复信息
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello,Client");
bufferedWriter.newLine();//插入一个换行符,表示回复内容的结束
bufferedWriter.flush();//注意需要手动的flush
//关闭流
bufferedReader.close();
bufferedWriter.close();
socket.close();
serverSocket.close();
}
}
运行结果: