자바 소켓 서버 구축 다 중 사용자 접근 응답
구체 적 으로 실현 하려 면 다음 코드 를 보십시오.
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:
 *      
 * */
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  ,                
    while(true){
      new Response(server.accept()).start();;
    }
  }catch(IOException e){
    e.printStackTrace();
  }finally{
    try{
      server.close();
    }catch(IOException e){
      e.printStackTrace();
    }
  }
}
/**
 * Description:
 *          ,       
 * 
 * @param msg         
 * @return           
 * */
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:
   *       
   * */
  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();
        //        ,       
        if(content.equalsIgnoreCase("quit")){
          break;
        }else{
          output.println(getMsg(content));
          output.flush();
        }
      }
    }catch(IOException e){
      e.printStackTrace();
    }finally{
      //    
      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{
      //     ,          。
      String msg = new ServerSocketDemo().getMsg("  ");
      Assert.assertEquals("gun!", msg);
    }catch(Exception e){
      e.printStackTrace();
    }
  }
}
2.apche JMeter 도 구 를 사용 하여 이 서버 에 대해 압력 테스트 를 실시 합 니 다.(1)아파 치 JMeter 를 열 고 테스트 계획 을 우 클릭->추가->Threads(Users)->Setup Thread Group
   (2)스 레 드 속성 설정(스 레 드 수,순환 순서 등)
 (3)우 클릭 으로 추가->simpler->HTTP 요청
  (4)속성 을 설정 하고 실행 을 클릭 하면 압력 테스트 를 할 수 있 습 니 다.
 이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.