一个简单的Socket通信Demo
服务器端Demo:
Server.java(服务器端运行主程序,直接运行):
package cn.wjs; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class Server { private static final int port = 9999; private static final int backlog = 100; private static final String bindAddr = "127.0.0.1"; public static void main(String[] args) throws Exception { System.out.println("正在启动Socket服务器"); ServerSocket server = new ServerSocket(9998, 100, InetAddress.getByName("127.0.0.1")); System.out.println("启动完成\n正在监听客户端连接"); while (true) { Socket socket = server.accept(); System.out.println("产生了一个新的会话,来自:" + socket.getInetAddress() + "\n=================================="); new ServerMessageReceiver(socket).start(); new ServerMessageSender(socket).start(); System.out.println("服务器端已启动"); } } }
ServerMessageReceiver.java:
package cn.wjs; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.Socket; public class ServerMessageReceiver extends Thread { private Socket socket; public ServerMessageReceiver(Socket socket) { this.socket = socket; } public void run() { try { BufferedReader reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), "UTF-8")); String content = null; while (true) { if (this.socket.isClosed()) { System.out.println("Socket已经关闭"); reader.close(); this.socket.close(); break; } content = reader.readLine(); if (content.equals("bye")) { System.out.println("对方请求关闭连接,无法继续进行聊天"); reader.close(); this.socket.close(); break; } System.out.println("【对方】" + content + "\n"); } reader.close(); this.socket.close(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
ServerMessageSender.java:
package cn.wjs; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.net.Socket; public class ServerMessageSender extends Thread { private Socket socket; public ServerMessageSender(Socket socket) { this.socket = socket; } public void run() { try { BufferedWriter outwriter = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream(), "UTF-8")); BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); String msg = null; while (true) { msg = inputReader.readLine(); if (msg.toLowerCase().equals("exit")) { System.exit(0); } if (this.socket.isClosed()) { System.out.println("Socket已经关闭"); outwriter.close(); inputReader.close(); this.socket.close(); break; } outwriter.write(msg); outwriter.newLine(); outwriter.flush(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
ClientDemo:
Client.java(客户端主程序,直接运行):
package cn.wjs; import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class Client { private static final int port = 9999; private static final String bindAddr = "127.0.0.1"; public static void main(String[] args) { System.out.println("正在连接Socket服务器"); try { Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9998); System.out.println("已连接\n================"); new ClientMessageSender(socket).start(); new ClientMessageReceiver(socket).start(); System.out.println("已启动"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
ClientMessageReceiver.java:
package cn.wjs; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.Socket; public class ClientMessageReceiver extends Thread { private Socket socket; public ClientMessageReceiver(Socket socket) { this.socket = socket; } public void run() { try { BufferedReader reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), "UTF-8")); String content = null; while (true) { if (this.socket.isClosed()) { System.out.println("Socket已经关闭"); reader.close(); this.socket.close(); break; } content = reader.readLine(); if (content.equals("bye")) { System.out.println("对方请求关闭连接,无法继续进行聊天"); reader.close(); this.socket.close(); break; } System.out.println("【对方】" + content + "\n"); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
ClientMessageSender.java:
package cn.wjs; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.net.Socket; public class ClientMessageSender extends Thread { private Socket socket; public ClientMessageSender(Socket socket) { this.socket = socket; } public void run() { try { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream(), "UTF-8")); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String msg = null; while (true) { msg = reader.readLine(); if (msg.toLowerCase().equals("exit")) { System.exit(0); } if (this.socket.isClosed()) { System.out.println("Socket已经关闭"); writer.close(); this.socket.close(); break; } writer.write(msg); writer.newLine(); writer.flush(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
通过elicpse导出jar包直接运行,服务器端导出jar设置main.class为Server.java,客户端导出jar设置main.class为Client.java
------
这是传统的TCP/BIO模式.如果多个客户端向服务器发送请求,通常采用连接池的方式来维护,这和数据库的连接池一个道理。当然,线程池中socket的数量有限,同时使用socket请求有可能造成客户端出现无限的等待,可以通过socket.setSoTimeout设置响应超时时间