服务器端:
客户端:
示例代码:
服务器端:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class ChatServer {
/**
* 监听端口号
*/
private int port = 8081;
public ChatServer() {
}
public ChatServer(int port) {
this.port = port;
}
public void service() {
try {
System.out.println("服务器开始建立连接");
// 建立服务器连接
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("服务器开始监听");
// 开始监听
Socket socket = serverSocket.accept();
// 获取输出流,向客户端发送信息
OutputStream outputStream = socket.getOutputStream();
BufferedReader reader = null;
try {
// 获取输入流,读取客户端传过来的信息
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println("从客户端接收到一条消息,内容为:" + line);
}
// 发送消息给客户端
outputStream.write("我接收到了".getBytes(StandardCharsets.UTF_8));
} finally {
// 关闭流和连接
if(reader != null) {
reader.close();
}
if(outputStream != null) {
outputStream.close();
}
if(socket != null) {
socket.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ChatServer chatServer = new ChatServer();
chatServer.service();
}
}
客户端:
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class ChatClient {
private int port = 8080;
public ChatClient() {
}
public ChatClient(int port) {
this.port = port;
}
private void consumer() {
System.out.println("客户端开始发送数据");
try {
Socket client = new Socket("localhost", 8081);
OutputStream outputStream = client.getOutputStream();
try {
outputStream.write("这里是8080端口".getBytes(StandardCharsets.UTF_8));
} finally {
if(outputStream != null) {
outputStream.close();
}
if(client != null) {
client.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ChatClient client = new ChatClient();
client.consumer();
}
}
运行结果:
每一个客户端的请求都需要服务端开启一个线程处理。
package com.alone.study.chat.bio;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import static java.util.concurrent.Executors.newFixedThreadPool;
public class ChatServer {
/**
* 监听端口号
*/
private int port = 8081;
/**
* 处理socket的线程池
*/
private static final ExecutorService EXECUTOR_SERVICE = newFixedThreadPool(10);
private List<Socket> sockets = new ArrayList<>(10);
public ChatServer() {
}
public ChatServer(int port) {
this.port = port;
}
public void service() {
try {
System.out.println("服务器开始建立连接");
// 建立服务器连接
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("服务器开始监听");
// 开始监听
Socket socket = serverSocket.accept();
sockets.add(socket);
EXECUTOR_SERVICE.submit(new SocketThread(socket));
Thread.sleep(2000);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
} finally {
if(sockets != null) {
for (Socket socket : sockets) {
if(socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
public static void main(String[] args) {
ChatServer chatServer = new ChatServer();
chatServer.service();
}
private class SocketThread implements Runnable {
private Socket socket;
public SocketThread() {
}
public SocketThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
System.out.println("开始处理来自" + socket.getRemoteSocketAddress() + "的请求");
// 接收客户端的消息
readFromClient();
// 回复客户端说已收到
sendToClient();
}
private void sendToClient() {
OutputStream outputStream = null;
// 回复客户端
try {
outputStream = socket.getOutputStream();
// 发送消息给客户端
outputStream.write("我接收到了".getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭流和连接
if(outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void readFromClient() {
// 接收客户端的数据
BufferedReader reader = null;
try {
System.out.println("开始获取客户端接收的消息");
// 获取输入流,读取客户端传过来的信息
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println("从客户端接收到一条消息,内容为:" + line);
}
System.out.println("结束获取客户端接收的消息");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭流和连接
if(reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
package com.alone.study.chat.bio;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutorService;
import static java.util.concurrent.Executors.newFixedThreadPool;
public class ChatClient {
private int port = 8080;
private static final ExecutorService EXECUTOR_SERVICE = newFixedThreadPool(1);
public ChatClient() {
}
public ChatClient(int port) {
this.port = port;
}
private void consumer() {
System.out.println("客户端开始发送数据");
try {
Socket client = new Socket("localhost", 8081);
OutputStream outputStream = client.getOutputStream();
try {
outputStream.write("这里是8080端口".getBytes(StandardCharsets.UTF_8));
System.out.println("客户端结束发送数据");
} finally {
if(outputStream != null) {
outputStream.close();
}
if(client != null) {
client.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ChatClient client = new ChatClient();
client.consumer();
}
}
不太理解上面这个服务端为啥会报错。
to be continue…
1.
2.
3.
4.
5.
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- igat.cn 版权所有 赣ICP备2024042791号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务