标签:ack try img socket 间隔 -- http end stack
@Test
public void server() {
try {
// 创建一个服务端socket,监听客户端的socket请求
ServerSocket server = new ServerSocket(11122);
while (true) {
// 收到客户端的请求后,产生一个socket
Socket ss = server.accept();
// 进行通信
InputStream is = ss.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
while( br.ready()){//判断是否含有下一行
System.out.println(new Date() + " 客户端发送: " + br.readLine());
}
OutputStream os = ss.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
Thread.sleep(1000);
pw.println("你好,我已经收到信息");//含有自动flush 等价于write之后,flush然后发送出去
Thread.sleep(10000);
pw.println("你好2,我已经收到信息");
System.out.println("---------------");
pw.close();
os.close();
br.close();
is.close();
ss.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void client() {
try {
Socket socket = new Socket("127.0.0.1", 11122);
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
pw.println("hi 我是客户端");
pw.println("hi end");
while(true){
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
while( br.ready()){
System.out.println(new Date() + " 服务端说: " + br.readLine());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
服务端发送两次消息,间隔1s
标签:ack try img socket 间隔 -- http end stack
原文地址:https://www.cnblogs.com/PG-five/p/10180740.html