import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; /** * 发送给对方消息的线程类 */ class ClientSend extends Thread{ private Socket socket; public ClientSend(Socket socket){ this.socket = socket; } @Override public void run() { this.sendMsg(); } /** * 发送给对方消息的方法 */ private void sendMsg(){ Scanner scanner = null; PrintWriter pw = null; try { //创建scanner对象 scanner = new Scanner(System.in); //创建向对方发送消息的流对象 pw = new PrintWriter(this.socket.getOutputStream()); while (true){ String msg = scanner.nextLine(); pw.println(msg); pw.flush(); } }catch (Exception e){ e.printStackTrace(); }finally { if (pw != null){ pw.close(); } if (scanner != null){ scanner.close(); } if (this.socket != null){ try { this.socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } } /** * 接收对方消息的线程类 */ class ClientReceive extends Thread{ private Socket socket; public ClientReceive(Socket socket){ this.socket = socket; } @Override public void run() { this.receiveMsg(); } /** * 接收对方消息的方法 */ private void receiveMsg(){ BufferedReader br = null; try { //创建接受信息的流对象 br = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); while (true){ String msg = br.readLine(); System.out.println("对方说: "+msg); } }catch (Exception e){ e.printStackTrace(); }finally { if (br != null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } } public class ChatSocketClient { public static void main(String[] args) { try { Socket socket = new Socket("192.168.1.41",8888); System.out.println("连接成功"); new ClientSend(socket).start(); new ClientReceive(socket).start(); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; /** * 发送给对方信息的线程类 */ class ServerSend extends Thread{ private Socket socket; public ServerSend(Socket socket){ this.socket = socket; } @Override public void run() { this.sendMsg(); } /** * 发送给对方消息的方法 */ private void sendMsg(){ Scanner scanner = null; PrintWriter pw = null; try { //创建scanner对象 scanner = new Scanner(System.in); //创建向对方发送消息的流对象 pw = new PrintWriter(this.socket.getOutputStream()); while (true){ String msg = scanner.nextLine(); pw.println(msg); pw.flush(); } }catch (Exception e){ e.printStackTrace(); }finally { if (pw != null){ pw.close(); } if (scanner != null){ scanner.close(); } if (this.socket != null){ try { this.socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } } /** * 接收对方消息的线程类 */ class ServerReceive extends Thread{ private Socket socket; public ServerReceive(Socket socket){ this.socket = socket; } @Override public void run() { this.receiveMsg(); } /** * 接收对方消息的方法 */ private void receiveMsg(){ BufferedReader br = null; try { //创建接受信息的流对象 br = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); while (true){ String msg = br.readLine(); System.out.println("对方说: "+msg); } }catch (Exception e){ e.printStackTrace(); }finally { if (br != null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } } public class ChatSocketServer { public static void main(String[] args) { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(8888); System.out.println("服务端已启动,等待连接"); Socket socket = new Socket(); serverSocket.accept(); System.out.println("连接成功"); new ServerSend(socket).start(); new ServerReceive(socket).start(); }catch (Exception e){ e.printStackTrace(); }finally { if (serverSocket != null){ try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } } |
![]() |
![]() |
老师,这啥情况,不是连接成功了吗?