import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;import java.net.UnknownHostException;public class TestClientsThread {/** * @param args */public static void main(String[] args) {try {//===========建立Socket连接===========Socket soc = new Socket("192.168.1.184", 25896);// 读取2号的final BufferedReader readers = new BufferedReader(new InputStreamReader(soc.getInputStream()));// 键盘写入final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));// 把自己写的输出到2号final PrintWriter writer = new PrintWriter(soc.getOutputStream());new Thread(new Runnable() {public void run() {while (true) {try {String str = readers.readLine();// 读取2号if(str != null){System.out.println("2号 :" + str);}} catch (IOException e) {e.printStackTrace();}}}}).start();System.out.println("连接成功");String s = reader.readLine();// 键盘写入while (!s.equals("bye")) {writer.println(s);// 写入服务端writer.flush();System.out.println("1号:" + s);s = reader.readLine();}writer.close();readers.close();reader.close();soc.close();} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
Socket 和ServerSocket的连接 .单一的聊天,随便输入输出
最新推荐文章于 2023-04-02 00:36:38 发布