package socket;import java.io.*;import java.net.Socket;public class ClientThread extends Thread{private Socket socket;public ClientThread() {try {Socket socket = new Socket("localhost", 8088);this.socket = socket;} catch (IOException e) {e.printStackTrace();}}public ClientThread(Socket socket) {this.socket = socket;}@Overridepublic void run() {// 待发送的消息OutputStream outputStream = null;try {outputStream = socket.getOutputStream();PrintWriter printWriter = new PrintWriter(outputStream);printWriter.write(String.format("我是客户端方[%s],我要我的幸运数字", this.getName()));printWriter.flush();// 断开连接socket.shutdownOutput();// 从服务器接收的信息InputStream is = socket.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is));String info = null;while ((info = br.readLine()) != null) {System.out.println("我是客户端,接收到服务器返回的信息:" + info);}br.close();is.close();outputStream.close();socket.close();} catch (IOException e) {e.printStackTrace();}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。