java Socket 学习

Posted lizhen1412

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Socket 学习相关的知识,希望对你有一定的参考价值。

socket 服务端编写之BIO网络学习

技术分享图片
 1 package com.example.demo.bio;
 2 
 3 import java.io.IOException;
 4 import java.net.ServerSocket;
 5 import java.net.Socket;
 6 
 7 /**
 8  * @Author: Lambert
 9  * @Date: 2019-01-07 00:37
10  * @Description:
11  */
12 public class Bioserver {
13     private static final int PORT = 8080;
14 
15     public static void main(String[] args) throws IOException {
16 
17         ServerSocket serverSocket = null;
18         try {
19             serverSocket = new ServerSocket(PORT);
20             System.out.println("the time server is start in port" + PORT);
21 
22             Socket socket = null;
23             while (true) {
24                 //阻塞项目
25                 socket = serverSocket.accept();
26                 new Thread(new TimeServerHandler(socket)).start();
27             }
28         } catch (
29                 Exception e) {
30             e.printStackTrace();
31         } finally {
32             {
33                 if (serverSocket != null) {
34                     System.out.println("the time server close");
35                     serverSocket.close();
36                 }
37 
38             }
39         }
40 
41     }
42 }
View Code
技术分享图片
 1 package com.example.demo.bio;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.InputStreamReader;
 5 import java.io.PrintWriter;
 6 import java.net.Socket;
 7 import java.util.Date;
 8 
 9 /**
10  * @Author: Lambert
11  * @Date: 2019-01-07 00:56
12  * @Description:
13  */
14 public class TimeServerHandler implements Runnable {
15 
16     private Socket socket;
17 
18     /**
19      * 构造函数
20      *
21      * @param socket
22      */
23     public TimeServerHandler(Socket socket) {
24         this.socket = socket;
25     }
26 
27     @Override
28     public void run() {
29         BufferedReader in = null;
30         PrintWriter out = null;
31         try {
32             //获得输入流
33             in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
34             out = new PrintWriter(this.socket.getOutputStream(), true);
35 
36             String body = null;
37             while ((body = in.readLine()) != null && body.length() != 0) {
38                 System.out.println("the time Server receive msg:" + body);
39                 out.println(new Date().toString());
40                 //暂时不需要
41 //                out.flush();
42             }
43 
44         } catch (Exception e) {
45             e.printStackTrace();
46         } finally {
47             if (in != null) {
48                 try {
49                     in.close();
50                 } catch (Exception e) {
51                     e.printStackTrace();
52                 }
53             }
54             if (out != null) {
55                 try {
56                     out.close();
57                 } catch (Exception e) {
58                     e.printStackTrace();
59                 }
60             }
61             if (socket != null) {
62                 try {
63                     this.socket.close();
64                 } catch (Exception e) {
65                     e.printStackTrace();
66                 }
67             }
68         }
69 
70 
71     }
72 
73 }
View Code

技术分享图片

 

以上是关于java Socket 学习的主要内容,如果未能解决你的问题,请参考以下文章

Java IO学习笔记四:Socket基础

[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段

Java实现简单的socket通信

学习JAVA Socket中 启动了serverSocket 再启动socket 为啥serversocket 自动关闭了

Socket网络编程进阶与实战

Socket网络编程进阶与实战