码迷,mamicode.com
首页 > 编程语言 > 详细

java Socket 学习

时间:2019-01-07 10:35:55      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:网络   des   util   demo   输入   sock   receive   pac   while   

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 学习

标签:网络   des   util   demo   输入   sock   receive   pac   while   

原文地址:https://www.cnblogs.com/lizhen1412/p/10231333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!