- 网络编程的知识结构图
- socket编程
网络编程,主要是指基于TCP的网络通信编程,使用Socket类实现,也称为socket编程
socket编程模型中有服务器端和客户端,服务器端使用ServerSocket创建,一般有固定的IP地址和端口号,方便向外界提供服务。客户端可以有多个,并且使用Socket主动连接服务器。连接后,服务器端也创建一个Socket对象表示这次连接
编程步骤:
服务器端:
1 创建服务器对象ServerSocket
2 等待客户端的连接请求,收到请求后即返回表示这次连接的Socket对象
3 开启新的线程专门处理这个连接
4 获得连接的输入输出流,并按照一定的规则进行数据交换
5 关闭连接(关闭连接时会自动关闭IO流)
客户端:
1 创建Socket对象,即向服务器申请连接
2 获得连接的输入输出流,并按照一定的规则进行数据交换
3 最后关闭连接(关闭连接时会自动关闭IO流)
平时编程时一般都是基于应用层协议,比如HTTP,直接进行socket编程的并不多。
/*
* 把从客户端读取到的一行数据的字符进行翻转,然后发送给客户端
* 当读取到over时,连接断开
*/
public class Server2 {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(10002);
while (true) {
Socket socket = server.accept();
MyThread myThread = new MyThread(socket);
myThread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class MyThread extends Thread {
private Socket socket;
public MyThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
if ("over".equalsIgnoreCase(line)) {
break;
}
//字符翻转的操作
char[] chs = line.toCharArray();
for (int i = 0; i < chs.length / 2; i++) {
char ch = chs[i];
chs[i] = chs[chs.length - 1 - i];
chs[chs.length - 1 - i] = ch;
}
bufferedWriter.write(chs);
bufferedWriter.newLine();
bufferedWriter.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class Client2 {
public static void main(String[] args) {
Socket socket = null;
Scanner scanner = null;
try {
socket = new Socket("localhost", 10002);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
scanner = new Scanner(System.in);
String line = null;
while ((line = scanner.nextLine()) != null) {
bufferedWriter.write(line);
bufferedWriter.newLine();
bufferedWriter.flush();
line = bufferedReader.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (scanner != null) {
scanner.close();
}
}
}
}
- UDP网络编程
传输层协议除了TCP,还有UDP(User Datagram Protocol),即用户数据报协议。UDP是无连接的,而且不保证数据的完整、有序,但传输效率非常高,适合视频、音频等对数据可靠性要求不太高的场景
UDP编程的时候没有典型的服务器——客户端结构,而且通信的两端不建立连接,可以直接把数据封装成数据包发送到另一端,而且每一端都可以发送、接收数据包
DatagramSocket 表示通信的一端,可以发送、接收数据包
DatagramPacket 数据包,理论上一个数据包可包含的数据量最多为65535字节
public class Send1 {
public static void main(String[] args) throws IOException {
DatagramSocket sendSocket = new DatagramSocket(10003);
byte[] data = "hello".getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 10004);
sendSocket.send(packet);
sendSocket.close();
}
}
public class Receive1 {
public static void main(String[] args) throws IOException {
DatagramSocket receiveSocket = new DatagramSocket(10004);
byte[] buff = new byte[1024];
DatagramPacket packet = new DatagramPacket(buff, buff.length);
receiveSocket.receive(packet);
int len = packet.getLength();
System.out.println(new String(buff, 0, len));
receiveSocket.close();
}
}