概述
public static void main(String[] args) {
SpringApplication.run(dwdp.class, args);
try {
ServerSocket serverSocket = new ServerSocket(9300);
System.out.println("启动服务器....");
Socket socket = serverSocket.accept();
System.out.println("客户端:"+socket.getInetAddress().getHostAddress()+"已连接到服务器");
BufferedReader br = new BufferedReader((new InputStreamReader(socket.getInputStream())));
CountDownLatch countDownLatch = new CountDownLatch(1);
ThreadUtil.execute(()->{
try{
String msg;
while (true){
msg= br.readLine();
if(StrUtil.isNotEmpty(msg)){
System.out.println("客户端:"+msg);
}
}
}catch (Exception e){
e.printStackTrace();
}
});
ThreadUtil.execute(()->{
try{
while (true){
Scanner scanner = new Scanner(System.in);
String msg =scanner.nextLine();
if(StrUtil.isNotEmpty(msg)){
if(msg=="exit"){
countDownLatch.countDown();
}else{
BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));
bw.write(msg+"n");
bw.flush();
}
}
}
}catch (Exception e){
}
});
countDownLatch.await();
} catch (Exception e) {
e.printStackTrace();
}
}
最后
以上就是粗心流沙为你收集整理的springBoot编写tcp服务端的全部内容,希望文章能够帮你解决springBoot编写tcp服务端所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复