Java实战之基于TCP实现简单聊天程序
简介
TCP(Transmission Control Protocol)是一种可靠的传输协议,其保证了数据的可靠传输。而基于TCP的聊天程序则利用了TCP协议的可靠性进行双方的消息传输。本文将介绍如何通过Java实现基于TCP协议的简单聊天程序。
实现步骤
1. 创建服务器端
服务器端实现基于TCP的Socket通信,其实现步骤如下:
1. 创建ServerSocket对象并指定端口号
2. 调用ServerSocket对象的accept()方法监听客户端请求
3. 使用InputStream读取客户端发来的消息
4. 使用OutputStream向客户端发送响应消息
示例代码:
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(8080);
System.out.println("服务器已启动,等待客户端连接。。。");
while (true) {
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len = 0;
StringBuilder sb = new StringBuilder();
while ((len = inputStream.read(bytes)) != -1) {
sb.append(new String(bytes, 0, len, "UTF-8"));
}
System.out.println("客户端发送的消息是:" + sb.toString());
OutputStream outputStream = socket.getOutputStream();
outputStream.write("服务端已接收到消息".getBytes("UTF-8"));
outputStream.flush();
}
}
}
2. 创建客户端
客户端同样实现基于TCP的Socket通信,其实现步骤如下:
1. 创建Socket对象并指定服务器IP和端口号
2. 使用OutputStream向服务器发送消息
3. 使用InputStream读取服务器响应消息
示例代码:
public class Client {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("127.0.0.1", 8080);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("你好,服务器".getBytes("UTF-8"));
outputStream.flush();
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len = 0;
StringBuilder sb = new StringBuilder();
while ((len = inputStream.read(bytes)) != -1) {
sb.append(new String(bytes, 0, len, "UTF-8"));
}
System.out.println("服务端响应的消息是:" + sb.toString());
}
}
3. 简单聊天程序
聊天程序需要同时启动服务端和客户端。客户端向服务端发送消息后,服务端再将消息转发给所有连接上的客户端。实现步骤如下:
1. 创建ServerSocket对象并指定端口号
2. 调用ServerSocket对象的accept()方法监听客户端请求,并将连接上的客户端Socket保存到List中
3. 使用InputStream读取客户端发来的消息
4. 将消息发送给所有连接上的客户端
示例代码:
服务端代码
public class ChatServer {
private static List<Socket> socketList = new ArrayList<>();
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(8080);
System.out.println("服务器已启动,等待客户端连接。。。");
while (true) {
Socket socket = serverSocket.accept();
socketList.add(socket);
new Thread(new ServerThread(socket)).start();
}
}
static class ServerThread implements Runnable {
Socket socket;
public ServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len = 0;
StringBuilder sb = new StringBuilder();
while ((len = inputStream.read(bytes)) != -1) {
sb.append(new String(bytes, 0, len, "UTF-8"));
}
System.out.println("客户端发送的消息是:" + sb.toString());
for (Socket s : socketList) {
OutputStream outputStream = s.getOutputStream();
outputStream.write(sb.toString().getBytes("UTF-8"));
outputStream.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
客户端代码:
public class ChatClient {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("127.0.0.1", 8080);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("你好,服务器".getBytes("UTF-8"));
outputStream.flush();
while (true) {
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len = 0;
StringBuilder sb = new StringBuilder();
while ((len = inputStream.read(bytes)) != -1) {
sb.append(new String(bytes, 0, len, "UTF-8"));
}
System.out.println("收到服务器发送的消息:" + sb.toString());
}
}
}
总结
本文介绍了如何通过Java实现基于TCP的简单聊天程序。通过例子我们可以学习到TCP协议的特点以及通过Java如何实现Socket通信。同时,聊天程序的实现也涉及到多线程的使用,可以让我们更好地学习多线程的使用方法。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java实战之基于TCP实现简单聊天程序 - Python技术站