1 Star 1 Fork 0

李明松/algorithm

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ClientThread.java 1.42 KB
一键复制 编辑 原始数据 按行查看 历史
李明松 提交于 2022-01-09 23:33 +08:00 . 密码学期末考试题
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;
}
@Override
public 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();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/msli121/algorithm.git
git@gitee.com:msli121/algorithm.git
msli121
algorithm
algorithm
master

搜索帮助