服务端代码
/**
* <p>项目名称:Socket双向通信</p>
* <p>模块名称:Socket服务端</p>
* 1、创建ServerSocket对象并绑定监听端口
* 2、调用serverSocket.accept(),作用:阻塞侦听客户端请求,并返回一个Socket对象
* 3、获取输入流InputStream,并转换为字符流InputStreamReader
* 4、创建BufferReader进行按行读取,用StringBuffer接收
* 5、创建输出流OutputStream,用PrintWriter进行回复消息写入
* 6、关闭所有的流和socket对象
*
* @ClassName:SocketServer
* @Create:1/7/20 10:33 AM
* @Version:1.0
*/
public class SocketServer {
public static void main(String[] args) {
InputStream inputStream = null;
OutputStream outputStream = null;
PrintWriter printWriter = null;
InputStreamReader inputStreamReader = null;
try {
ServerSocket serverSocket = new ServerSocket(Constant4Socket.PORT);
System.out.println("服务端已启动......");
while (true) {
// 侦听客户端连接并返回一个socket对象
Socket socket = serverSocket.accept();
// 获取输入流进行读取客户端消息
inputStream = socket.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuffer buffer = new StringBuffer();
String tempStr =</