Java 网络编程
一、IP地址
1. 什么是IP地址
2. IP地址的组成
IP地址= 网络地址 + 主机地址
网络地址:标识计算机或网络设备所在的网段
主机地址:标识特定主机或网络设备
3. IP地址的配置和检测
1)查看本机的IP地址:ipconfig。
2)测试网络是否通畅:ping 目标IP地址。
二、Socket
1. 简介
Socket的底层机制复杂,Java平台提供了一些简单的API,可以更简单有效的使用Socket开发而无需了解底层机制。
2. 发送数据
public class Client {
public static void main(String[] args) {
// 创建Socket对象
try(// 通信链路的客户端点创建成功
Socket socket = new Socket("localhost", 8088);
// 获取输出流将登录信息发送给服务器端
OutputStream os = socket.getOutputStream();
// 获取输入流接收服务器端发送的响应信息
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String str = "用户名:tom,密码:123456";
byte[] bytes = str.getBytes();
// 输出信息
os.write(bytes);
System.out.println("向服务器发送请求成功!");
// 关闭输出流
socket.shutdownOutput();
// 获取输入流接收服务器端发送的响应信息
String str2;
while ((str2 = br.readLine()) != null) {
System.out.println("这里是客户端,接收到的服务器发送来的响应信息为:");
System.out.println(str2);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. 接收数据
public class Server {
public static void main(String[] args) {
try(// 创建ServerSocket
ServerSocket ss = new ServerSocket(8088);
// 侦听客户端的请求
Socket soc = ss.accept();
// 获取输入流接收客户端的请求信息
InputStream is = soc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
// 获取输出流向客户端发送响应信息
OutputStream os = soc.getOutputStream()) {
String str;
while ((str = br.readLine()) != null) {
System.out.println("这里是服务器端,接收到的客户端信息为:");
System.out.println(str);
}
// 获取输出流向客户端发送响应信息
Thread.sleep(2000);
String str2 = "用户名和密码正确,登录成功";
byte[] bytes = str2.getBytes();
os.write(bytes);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
三、InetAddress、DatagramPacket、DatagramSocket
1. 移动端(发送端)
public class Send {
/*
* 示例06:升级示例05,发送方发送咨询问题,接收方回应咨询。
*
* 发送方实现步骤如下:
* (1)获取本地主机的InetAddress对象。
* (2)创建DatagramPacket对象,封装要发送的信息。
* (3)利用DatagramSocket对象将DatagramPacket对象数据发送出去。
*/
public static void main(String[] args) {
DatagramSocket ds = null;
InetAddress ia = null;
String mess = "你好,我想咨询一个问题。";
System.out.println("我说:" + mess);
try {
// 获取本地主机地址
ia = InetAddress.getByName("localhost");
// 创建DatagramPacket对象,封装数据
DatagramPacket dp = new DatagramPacket(mess.getBytes(),
mess.getBytes().length, ia, 8800);
// 创建DatagramSocket对象,向服务器发送数据
ds = new DatagramSocket();
ds.send(dp);
byte[] buf = new byte[1024];
DatagramPacket dpre = new DatagramPacket(buf, buf.length);
ds.receive(dpre);
// 显示接收到的信息
String reply = new String(dpre.getData(), 0, dpre.getLength());
System.out.println(dpre.getAddress().getHostAddress() + "说:"
+ reply);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
ds.close();
}
}
}
2. 服务器(接收端)
public class Receive {
public static void main(String[] args) {
/*
* 示例06:升级示例05,发送方发送咨询问题,接收方回应咨询。
*
* 接收方实现步骤如下:
* (1)创建DatagramPacket对象,准备接收封装的数据。
* (2)创建DatagramSocket对象,接收数据保存于DatagramPacket对象中。
* (3)利用DatagramPacket对象处理数据。
*/
DatagramSocket ds = null;
DatagramPacket dp = null;
DatagramPacket dpto = null;
// 创建DatagramPacket对象,用来准备接收数据
byte[] buf = new byte[1024];
dp = new DatagramPacket(buf, 1024);
try {
// 创建DatagramSocket对象,接收数据
ds = new DatagramSocket(8800);
ds.receive(dp);
// 显示接收到的信息
String mess = new String(dp.getData(), 0, dp.getLength());
System.out.println(dp.getAddress().getHostAddress() + "说:" + mess);
String reply = "你好,我在,请咨询!";
// 显示与本地对话框
System.out.println("我 说:" + reply);
// 创建DatagramPacket对象,封装数据
SocketAddress sa = dp.getSocketAddress();
dpto = new DatagramPacket(reply.getBytes(),
reply.getBytes().length, sa);
ds.send(dpto);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
ds.close();
}
}
}
s().length, sa);
ds.send(dpto);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
ds.close();
}
}
}