java搭建一個Socket服務(wù)器響應(yīng)多用戶訪問
當(dāng)我們搭建了一個Socket服務(wù)端,是需要去響應(yīng)多用戶的訪問的。此時,我們就要使用多線程,為每個訪問的用戶建立一個線程來響應(yīng)該用戶的訪問。
具體實現(xiàn),看如下代碼:
package com.sun.socket; import Java.io.IOException; import java.NET.*; import java.io.*; import java.util.*; /** * Description: * 搭建一個Socket服務(wù)器響應(yīng)多用戶訪問 * @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服務(wù)器響應(yīng)多個用戶訪問 * */ public void test1(){ init(); ServerSocket server = null; try{ //以指定端口搭建一個Socket服務(wù)端 server = new ServerSocket(12000); //等待客戶端Socket實例,并創(chuàng)建一個線程去響應(yī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)容,返回相應(yīng)的內(nèi)容 * * @param msg 客戶端輸入的內(nèi)容 * @return 返回服務(wù)端回復(fù)的內(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: * 響應(yīng)用戶 * @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ù)用戶端的輸入,做出相應(yīng)的反應(yīng) 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 。
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工具對該服務(wù)端進行壓力測試
(1)打開Apache JMeter,右鍵測試計劃->添加->Threads(Users)->Setup Thread Group
(2)設(shè)置線程屬性(線程數(shù),循環(huán)次等)
(3)右鍵添加->simpler->HTTP請求
(4)設(shè)置屬性,點擊運行就可以進行壓力測試了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaWeb響應(yīng)下載功能實例代碼(包含工具類)
- java中添加按鈕并添加響應(yīng)事件的方法(推薦)
- javaweb如何實現(xiàn)請求和響應(yīng)
- Java中HttpServletResponse響應(yīng)中文出現(xiàn)亂碼問題
- Java Web請求與響應(yīng)實例詳解
- java常見事件響應(yīng)方法實例匯總
- javasciprt下jquery函數(shù)$.post執(zhí)行無響應(yīng)的解決方法
- JAVA發(fā)送HTTP請求,返回HTTP響應(yīng)內(nèi)容,應(yīng)用及實例代碼
- JavaWeb Refresh響應(yīng)頭代碼實例詳解
相關(guān)文章
Java?Stream函數(shù)式編程管道流結(jié)果處理
這篇文章主要為大家介紹了Java?Stream函數(shù)式編程管道流結(jié)果處理的示例過程解析需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-03-03使用springBoot中的info等級通過druid打印sql
這篇文章主要介紹了使用springBoot中的info等級通過druid打印sql,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Java SMM框架關(guān)聯(lián)關(guān)系映射示例講解
SSM框架是spring MVC ,spring和mybatis框架的整合,是標準的MVC模式,將整個系統(tǒng)劃分為表現(xiàn)層,controller層,service層,DAO層四層,使用spring MVC負責(zé)請求的轉(zhuǎn)發(fā)和視圖管理,spring實現(xiàn)業(yè)務(wù)對象管理,mybatis作為數(shù)據(jù)對象的持久化引擎2022-08-08feign post參數(shù)對象不加@RequestBody的使用說明
這篇文章主要介紹了feign post參數(shù)對象不加@RequestBody的使用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10在springboot中攔截器Filter中注入bean失敗問題及解決
這篇文章主要介紹了在springboot中攔截器Filter中注入bean失敗問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05