相关文章
springboot编写netty-socketio实例:https://blog.csdn.net/zhangjian8641/article/details/108628974
什么是Socket
Socket(套接字),就是对网络中不同主机上的应用进程之间进行双向通信的端点的抽象。
套接字Socket=(IP地址:端口号)
Socket是以 TCP/IP 协议进行通讯
图片来源:https://www.runoob.com/w3cnote/android-tutorial-socket1.html
从上面的模型可以看出,Socket是一个长连接,可以在连接结束前进行多次通讯。
实现Socket
下面用代码实现一下上面的图。
Server服务器
package com.example;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
public class Server extends Thread {
private ServerSocket server = null;
private Socket socket = null;
/**
* 实例化Socket服务
*/
private Server(int port) {
try {
server = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 接收Socket
*/
@Override
public void run() {
try {
System.out.println(getDate() + " 等待客户端连接...");
socket = server.accept();
new SendMessThread().start();
System.out.println(getDate() + " 客户端 (" + socket.getInetAddress().getHostAddress() + ") 连接成功...");
InputStream in = socket.getInputStream();
int len;
byte[] buf = new byte[1024];
while ((len = in.read(buf)) != -1) {
System.out.println(getDate() + " 客户端: (" + socket.getInetAddress().getHostAddress() + ")说:"
+ new String(buf, 0, len, StandardCharsets.UTF_8));
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static String getDate() {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
/**
* 发送Socket
*/
class SendMessThread extends Thread {
@Override
public void run() {
try {
if (socket != null) {
Scanner scanner = new Scanner(System.in);
OutputStream out = socket.getOutputStream();
String in = "";
do {
in = scanner.next();
out.write(("" + in).getBytes(StandardCharsets.UTF_8));
out.flush();
} while (!"----结束----".equals(in));
scanner.close();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Server server = new Server(1234);
server.start();
}
}
Client客户端
package com.example;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
public class Client extends Thread {
private Socket socket = null;
/**
* 实例化Socket会话
*/
private Client(String host, int port) {
try {
socket = new Socket(host, port);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 接收Socket
*/
@Override
public void run() {
new sendMessThread().start();
try {
InputStream in = socket.getInputStream();
int len;
byte[] buf = new byte[1024];
while ((len = in.read(buf)) != -1) {
System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " 服务器说: "
+ new String(buf, 0, len, StandardCharsets.UTF_8));
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 发送Socket
*/
class sendMessThread extends Thread {
@Override
public void run() {
try {
Scanner scanner = new Scanner(System.in);
OutputStream os = socket.getOutputStream();
String in = "";
do {
in = scanner.next();
os.write((""+in).getBytes());
os.flush();
} while (!"----结束----".equals(in));
scanner.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Client client = new Client("127.0.0.1", 1234);
client.start();
}
}
运行结果
先运行Server服务器,再运行Client客户端