Java使用socket实现两人聊天对话
import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; /** * User: HYY * Date: 13-10-29 * Time: 下午9:57 * To change this template use File | Settings | File Templates. */ public class Server { public static final int SERVER_PORK = 10000; public static void main(String[] args) throws IOException, InterruptedException { ServerSocket serverSocket = new ServerSocket(SERVER_PORK); System.out.println("正在监听 " + SERVER_PORK + " 端口!"); Socket socket; BufferedReader bufferedReader; while (true) { socket = serverSocket.accept(); System.out.println("一客户端连接。"); Thread writerThread = new Thread(new MyServerWriter(socket)); writerThread.start(); bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); try { String msg;//客户端发过来的信息 while ((msg = bufferedReader.readLine()) != null) { System.out.println("##客户端:" + msg); } } catch (IOException e) { try { if (!socket.isClosed()) { socket.close(); } } catch (IOException e1) { System.out.println("关闭socket出现错误"); } } System.out.println("提示:当前客户端已经断开连接,服务器正等待下一个客户端的连接。"); } } } class MyServerWriter implements Runnable { private Socket socket = null; private PrintWriter printWriter; private Scanner scanner; public MyServerWriter(Socket socket) throws IOException { this.socket = socket; scanner = new Scanner(System.in); printWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true); } @Override public void run() { String msg;//要发送的信息 try { while ((msg = scanner.nextLine()) != null) { if (msg.equals("88")) { if (!socket.isClosed()) { System.out.println("服务器手动与客户端断开"); socket.close(); } break; } printWriter.println(msg); } } catch (IOException io) { System.out.println("关闭socket出现问题"); } catch (Exception e) { System.out.println("异常关闭客户端(可能是直接关闭退出窗口)"); } } }
import java.io.*; import java.net.Socket; import java.util.Scanner; /** * Created with IntelliJ IDEA. * User: HYY * Date: 13-10-29 * Time: 下午10:31 * To change this template use File | Settings | File Templates. */ public class Client { public static void main(String[] args) throws IOException { Socket socket = new Socket("127.0.0.1", 10000); System.out.println("连接服务器成功!如果想断开连接请输入88(回车)"); new Thread(new MyClientWriter(socket)).start(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); try { String msg;//服务器发过来的信息 while ((msg = bufferedReader.readLine()) != null) { System.out.println("##服务器:" + msg); } } catch (IOException e) { System.out.println("警告:断开连接!"); try { if (!socket.isClosed()) { socket.close(); } } catch (IOException e1) { System.out.println("读取线程:关闭socket出现错误"); } } System.exit(1); } } class MyClientWriter implements Runnable{ private Socket socket = null; private PrintWriter printWriter; private Scanner scanner; public MyClientWriter(Socket socket) throws IOException { this.socket = socket; scanner = new Scanner(System.in); printWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true); } @Override public void run() { String msg;//要发送的信息 try { while ((msg = scanner.nextLine()) != null) { System.out.println("isClosed="+socket.isClosed()); if(socket.isClosed()) { break; } else { if(msg.equals("88")) { break; } } printWriter.println(msg); } } catch (Exception e) { // System.out.println("异常关闭客户端(可能是直接关闭退出窗口)"); } System.out.println("写线程准备死亡"); } }
本文出自 无忧之路 - 博客园