java socket 多线程通讯
1.目录结构
2.Server.java 代码
package de.bvb.server; import java.net.ServerSocket; import java.net.Socket; import de.bvb.util.IOUtils; import de.bvb.util.ReaderThread; import de.bvb.util.WriterThread; /** * 参考: http://www.cnblogs.com/jpwz/p/5705160.html */ public class Server { public static void main(String[] args) { Server server = new Server(); server.init(); } private void init() { ServerSocket serverSocket = null; Socket socket = null; try { serverSocket = new ServerSocket(9999); System.out.println("服务已启动..."); while (true) { socket = serverSocket.accept(); System.out.println(socket.getInetAddress().getHostAddress() + "@" + socket.getPort() + " 已连接"); // 分别启动读写线程 new ReaderThread(socket).start(); new WriterThread(socket).start(); } } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeCloseable(serverSocket); } } }
3.client.java 代码
package de.bvb.client; import java.net.Socket; import de.bvb.util.IOUtils; import de.bvb.util.ReaderThread; import de.bvb.util.WriterThread; public class Client { public static void main(String[] args) { Client client = new Client(); client.init(); } private void init() { try { Socket socket = new Socket("127.0.0.1", 9999); System.out.println(socket.getInetAddress().getHostAddress() + "@" + socket.getLocalPort() + " 已连接上服务器"); new ReaderThread(socket).start(); new WriterThread(socket).start(); } catch (Exception e) { e.printStackTrace(); } } }
4.util下的代码
package de.bvb.util; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.Socket; public class ReaderThread extends Thread { private Socket socket; public ReaderThread(Socket socket) { this.socket = socket; } @Override public void run() { BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String content; while (null != (content = reader.readLine())) { System.out.println(content); } } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeCloseable(reader, socket); } } }
package de.bvb.util; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; public class WriterThread extends Thread { private Socket socket; public WriterThread(Socket socket) { this.socket = socket; } @Override public void run() { BufferedWriter writer = null; BufferedReader reader = null; try { String name = socket.getInetAddress().getHostAddress() + "@" + socket.getLocalPort(); reader = new BufferedReader(new InputStreamReader(System.in)); writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); String content; while (null != (content = reader.readLine())) { System.out.println(name + "说: " + content); writer.write(name + "说: " + content + "\n"); writer.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeCloseable(reader, writer, socket); } } }
package de.bvb.util; import java.io.Closeable; import java.io.IOException; public class IOUtils { public static void closeCloseable(Closeable... closeable) { try { if (closeable != null && closeable.length > 0) { for (Closeable c : closeable) { if (c != null) { c.close(); } } } } catch (IOException e) { throw new RuntimeException(e); } } }