當我們搭建了一個Socket服務端,是需要去響應多用戶的訪問的。此時,我們就要使用多線程,為每個訪問的用戶建立一個線程來響應該用戶的訪問。
具體實現(xiàn),看如下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
package com.sun.socket; import Java.io.IOException; import java.NET.*; import java.io.*; import java.util.*; /** * Description: * 搭建一個Socket服務器響應多用戶訪問 * @author Lee * */ public class ServerSocketDemo { ArrayList MSG = new ArrayList<>(); ArrayList RES = new ArrayList<>(); /** * Description: * 初始化數(shù)據(jù) * */ public void init(){ MSG.add( "hellow" ); RES.add( "hi" ); } /** * Description: * 搭建一個Socket服務器響應多個用戶訪問 * */ public void test1(){ init(); ServerSocket server = null ; try { //以指定端口搭建一個Socket服務端 server = new ServerSocket( 12000 ); //等待客戶端Socket實例,并創(chuàng)建一個線程去響應該客戶單實例 while ( true ){ new Response(server.accept()).start();; } } catch (IOException e){ e.printStackTrace(); } finally { try { server.close(); } catch (IOException e){ e.printStackTrace(); } } } /** * Description: * 根據(jù)用戶輸入的內(nèi)容,返回相應的內(nèi)容 * * @param msg 客戶端輸入的內(nèi)容 * @return 返回服務端回復的內(nèi)容 * */ public String getMsg(String msg){ String res = "Are you kidding me?Please speak English." ; for ( int i= 1 ;i<MSG.size();i++){ if (msg.contains(MSG.get(i))){ res = RES.get(i); } } return res; } public static void main(String[] args) { // TODO Auto-generated method stub new ServerSocketDemo().test1(); } /** * Description: * 響應用戶 * @author Lee * */ class Response extends Thread{ Socket client; /** * Description: * 默認構(gòu)造器 * */ public Response(){} /** * Description: * 初始化Socket * */ public Response(Socket client){ this .client = client; } @Override public void run(){ Scanner input = null ; PrintWriter output = null ; try { //獲取用戶端的輸入和輸出流 input = new Scanner(client.getInputStream()); output = new PrintWriter(client.getOutputStream()); output.println( "歡迎訪問!" ); output.flush(); //等待客戶端的輸入 String content = null ; while (input.hasNext()){ content = input.nextLine(); //根據(jù)用戶端的輸入,做出相應的反應 if (content.equalsIgnoreCase( "quit" )){ break ; } else { output.println(getMsg(content)); output.flush(); } } } catch (IOException e){ e.printStackTrace(); } finally { //關(guān)閉資源 input.close(); output.close(); } } } } |
1、我們可以寫一個小小測試工具類,來測試一下public String getMsg(String msg)方法。
對該類右鍵,選擇new新建一個JUnit Test Case 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.sun.socket; import org.junit.Assert; import org.junit.Test; public class ServerSocketDemoTest { @Test public void testGetMsg() { try { //調(diào)用該方法,并與其目標值進行對比。 String msg = new ServerSocketDemo().getMsg( "在嗎" ); Assert.assertEquals( "gun!" , msg); } catch (Exception e){ e.printStackTrace(); } } } |
2、使用apche JMeter工具對該服務端進行壓力測試
(1)打開Apache JMeter,右鍵測試計劃->添加->Threads(Users)->Setup Thread Group
(2)設置線程屬性(線程數(shù),循環(huán)次等)
(3)右鍵添加->simpler->HTTP請求
(4)設置屬性,點擊運行就可以進行壓力測試了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/qq1131410679/article/details/55051195