
- 前言
- 一、概述
- 二、要素
- 2.1、IP和端口号
- 2.2、通信协议
- 2.3、TCP
- 2.4、UDP
- 2.5、UPD案例分析
- 案例一:在线聊天
- 总结
前言
JavaSE网络编程学习笔记
一、概述计算机网络: 是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络 *** 作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
二、要素 2.1、IP和端口号
IP地址: InetAddress
- 可以确定一台主机
- ip地址的分类
- IPv4/IPv6
- IPv4:32位,0~255,越42亿个
- IPv6:128位,8个无符号整数表示
- 公网/私网
- 公网(互联网)
- 私网(局域网)
- IPv4/IPv6
域名: 标记IP
例如:www.baidu.com
端口号:
- 计算机上每个进程都会有一个端口,用来区分不同的软件
- 范围:0~65535
- 公有端口:0~1023
- 程序注册端口:2014~49151
- 动态、私有:49152~65535
TCP/IP协议
- TCP:类比打电话
- 连接,稳定
- 三次握手,四次挥手
- 传输完成后需要释放链接,效率低
- UDP:类比发短信
- 不连接,不稳定
- 只顾传输,不会进行交互
2.3、TCP
这里创建一个服务端和客户端来演示TCP交互过程
客户端:
public class Client {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
//1、需要知道服务器的ip和端口号
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
int port = 9090;
//2、创建连接
socket = new Socket(inetAddress, port);
//3、发送消息,IO流
os = socket.getOutputStream();
os.write("你好,服务器".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务端:
public class Server {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1、设置一个地址让客户端访问
serverSocket = new ServerSocket(9090);
//2、等待接受客户端发送的信息
socket = serverSocket.accept();
//3、读取客户端发送的消息
is = socket.getInputStream();
//4、管道流
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.4、UDP
不需要连接,只需要知道目标地址
服务端
public class Server {
public static void main(String[] args) {
try {
//1、开放端口
DatagramSocket socket = new DatagramSocket(9090);
//2、接受数据包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
//3、关闭连接
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端
public class Client {
public static void main(String[] args) {
try {
//1、建立Socket
DatagramSocket socket = new DatagramSocket();
//2、建立一个包
String msg = "你好,服务器";
InetAddress inetAddress = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, inetAddress, port);
//3、发送包
socket.send(packet);
//4、关闭流
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.5、UPD案例分析
案例一:在线聊天
服务端
public class Server {
public static void main(String[] args) {
try {
//1、开放端口
DatagramSocket socket = new DatagramSocket(9090);
while (true) {
//2、接受数据包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
//将接受的到信息转换成字符串
String msg = new String(packet.getData(),0,packet.getLength());
//输出到控制台
System.out.println(new String(packet.getData(),0,packet.getLength()));
//当用户输入88时结束通信
if ("88".equals(msg)) {
break;
}
}
//3、关闭连接
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端
public class Client {
public static void main(String[] args) {
try {
//1、建立Socket
DatagramSocket socket = new DatagramSocket();
//客户端输入
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
//获取用户输入的字符串
String msg = reader.readLine();
//建立包
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length,new InetSocketAddress("localhost",9090));
//3、发送包
socket.send(packet);
//用户输入88结束聊天
if ("88".equals(msg)) {
break;
}
}
//4、关闭流
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
总结
以上
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)