一、网络
概念:两台或多台设备通过一定物理设备连接起来构成了网络。 根据网络的覆盖范围,可分为:局域网、城域网、广域网(万维网)。
二、网络通信
概念:两台设备之间通过网络实现数据传输。 java.net包下提供一系列的类或接口→完成网络通信 InetAddress类: 1、获取本机信息【主机名/IP】; 2、通过域名,获取远程服务器的IP。
三、IP地址
概念:唯一标识主机。查看ip地址:ipconfig IPv4:4个字节表示(十进制32位)→ IPv6:16个字节表示(十六进制128位) IPv6是互联网工程任务组设计的用于替代IPv4的下一代IP协议,极大扩充网络地址资源。
四、域名
概念:IP地址的映射,涉及HTTP协议。 解决记忆IP的困难
五、端口号
概念:用于标识计算机上某个特定的网络程序。 端口范围:0~65535(2个字节表示端口) 在网络开发中,不要使用0~1024的端口号,名花有主
六、网络协议(TCP/IP协议)
概念:协议就是数据的组织形式。 把要发送的数据,准确无误的送到对方区
七、Socket
1、通信的两端都要有Socket,是两台机器间通信的端点; 2、网络通信其实就是Socket间的通信; 3、Socket允许程序把网络连接当成一个流,数据在两个Socket间通过IO传输; 5、一般主动发起通信的应用程序属于客户端,等待通信请求的为服务端。
八、TCP字节流编程
- 编写一个服务器端,和一个客户端;
- 服务器端在9999端口监听;
- 客户端连接到服务端,发送 “ hello ” ,然后退出;
- 服务器端接收到客户端发送的信息,输出并退出。
补充:自行编写 → 服务端回送消息给客户端
package demo;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("等待连接客户端...");
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
byte[ ] buf = new byte[1024];
int readlen = 0;
while ((readlen = inputStream.read(buf)) != -1) {
System.out.println(new String(buf, 0, readlen));
}
inputStream.close();
socket.close();
serverSocket.close();
}
}
package demo;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocketClient {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello".getBytes());
outputStream.close();
socket.close();
System.out.println("客户端退出!!!");
}
}
细节: 1、ServerSocket可以通过accept() 返回多个Socket对象(多个客户端连接,多并发); 2、若服务器端没读到数据,则程序会阻塞; 3、上述程序会阻塞,并不会停止,Socket对象不知道对方是否输完,会一直等待对方输入数据 → 解决办法:在输出流后设置结束标记,即socket.shutdownOutput();
九、TCP字符流编程
package demo;
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.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocketClient {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello,server");
bufferedWriter.newLine();
bufferedWriter.flush();
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String s = bufferedReader.readLine();
System.out.println(s);
bufferedWriter.close();
bufferedReader.close();
socket.close();
System.out.println("客户端退出!!!");
}
}
package demo;
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.ServerSocket;
import java.net.Socket;
public class SocketServer {
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();
bufferedWriter.close();
bufferedReader.close();
socket.close();
serverSocket.close();
System.out.println("服务端退出!!!");
}
}
十、TCP传输图片
- 编写一个服务端,和一个客户端;
- 服务器端在8888端口监听;
- 客户端连接到服务端,发送一张图片;
- 服务端接受到客户端发送的图片,保存到src下,发送“收到图片”,再退出;
- 客户端接收到服务端发送的“收到图片”,再退出;
- 需要使用工具包StreamUtils.java。
package demo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws Exception {
Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
String filePath = "D:\\桌面存储\\1.png";
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
byte[] bytes = StreamUtils.streamToByteArray(bis);
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
bos.write(bytes);
socket.shutdownOutput();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String s = br.readLine();
System.out.println(s);
bos.close();
bis.close();
socket.close();
}
}
package demo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(8888);
Socket socket = serverSocket.accept();
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
byte[] bytes = StreamUtils.streamToByteArray(bis);
String destPath = "src\\demo\\666.png";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath));
bos.write(bytes);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write("收到图片!!!");
bw.newLine();
bw.flush();
bw.close();
bos.close();
bis.close();
socket.close();
serverSocket.close();
}
}
① netstat
DOS | 说明 |
---|
netstat -an | 查看当前网络情况:端口监听、网络连接 | netstat -anb | 还可显示哪个程序在监听 |
其中若要分页显示,则:netstat -an | more、netstat -an | more
② TCP网络通讯的秘密
当客户端连接到服务端后,实际上客户端也是通过一个端口和服务端进行通讯的,这个端口是TCP/IP来分配的,是不确定随机的。
③ UDP网络编程
- 类DatagramSocket和DatagramPacket实现了基于UDP协议网络程序;
- DatagramSocket对象,发送和接收数据报,系统不保证数据报一定能够送到目的地,也不确定什么时候可以抵达;
- DatagramPacket对象,封装UDP数据报,包含发送端的IP、端口号,接收端的IP、端口号;
- UDP数据报都给出了完整的地址信息,因此无需建立发送方和接收方的连接。
package demo;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPSend {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(9998);
byte[ ] data = "hello,明天吃火锅".getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getByName("10.212.130.221"), 9999);
socket.send(packet);
byte[ ] buf = new byte[1024];
DatagramPacket packet1 = new DatagramPacket(buf, buf.length);
socket.receive(packet1);
int length = packet1.getLength();
byte[ ] data1 = packet1.getData();
String s = new String(data1, 0, length);
System.out.println(s);
socket.close();
System.out.println("A端结束!!!");
}
}
package demo;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPReceiver {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(9999);
byte[ ] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
int length = packet.getLength();
byte[ ] data = packet.getData();
String s = new String(data, 0, length);
System.out.println(s);
byte[ ] data1 = "好的,明天见".getBytes();
DatagramPacket packet1 = new DatagramPacket(data1, data1.length, InetAddress.getByName("10.212.130.221"), 9998);
socket.send(packet1);
socket.close();
System.out.println("B端结束!!!");
}
}
|