0
다중 스레드 UDP 클라이언트 - 서버 사전을 구현 중입니다. 제대로 구현 한 것 같지만 제대로 테스트하는 방법을 모르겠습니다. 시간이 있다면 누구나 내 코드를 빠르게 볼 수 있습니까? 당신이 할 수다중 스레드 UDP 서버 테스트 (Java)
Server Started
Number of threads active: 1
Number of threads active: 2
Number of threads active: 3
Number of threads active: 4
Number of threads active: 5
Thread-0 just run.
Number of threads active: 5
Thread-1 just run.
Number of threads active: 5
Thread-3 just run.
Number of threads active: 5
:
java DictServer <port> <dictionary file name>
java DictClient localhost <port> <word to search>
이 서버의 출력 (클라이언트가 3 번 여기에 실행 된) :
이
나는 보통 내 프로그램을 실행하는 방법입니다 출력이 괜찮아 보입니다. "Worker Pool Model"이기 때문에 스레드 번호를 최대 (5)로 유지합니다. 그러나 UDP에서는 송수신 된 패킷 만 '활성 연결'이 없습니다. 클라이언트가 패킷을 받으면 스레드가 닫힙니다. 이것은 매우 빠르게 발생하므로 실제로 여러 클라이언트를 동시에 테스트 할 수는 없습니다. 어떤 제안?또한 setter를 사용하여 스레드 수를 업데이트합니다. 하지만 난 그것을 사용하여 호출
"DictServer.decNumThreads()"이 나쁜가요?
내 코드 :
서버 클래스 :
public class DictServer {
private static int threads = 0;
public static void main(String args[]) throws IOException {
// Connection Parameters
DatagramSocket socket = null;
int maxThreads = 5; // Max threads at any time
// Retrieve user input
int serverPort = Integer.parseInt(args[0]); // Convert string to int
String dictionaryFile = args[1];
try {
// Setup socket
socket = new DatagramSocket(serverPort);
System.out.println("Server Started");
while(true) {
if(threads < maxThreads) {
ServerThread server = new ServerThread(socket, dictionaryFile);
new Thread(server).start();
threads++;
System.out.println("Number of threads active: " + threads);
}
}
}
catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
finally {
if(socket != null)
socket.close();
}
}
// Setter for number of active threads
public static void decNumThreads() {
threads--;
}
}
스레딩 클래스 : 스레드를 생성
public class ServerThread implements Runnable {
private DatagramSocket socket = null;
private String dictionaryFile;
// Constructor
public ServerThread(DatagramSocket socket, String dictionaryFile) {
this.socket = socket;
this.dictionaryFile = dictionaryFile;
}
@Override
public void run() {
byte[] word = new byte[1000];
byte[] definition = new byte[1000];
try {
// Get client request
DatagramPacket request = new DatagramPacket(word, word.length);
socket.receive(request);
// Retrieve definition from dictionary file
if(word != null)
definition = getDefinition(new String(word), dictionaryFile);
// Put reply into packet, send packet to client
DatagramPacket reply = new DatagramPacket(definition, definition.length, request.getAddress(), request.getPort());
socket.send(reply);
}
catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
System.out.println(Thread.currentThread().getName() + " just run.");
DictServer.decNumThreads();
}
일부 코드를 작성했으며 시험해보기를 원하십니까? 클라이언트를 자동화하고 자체 테스트 시스템을 만드십시오. 문제가 발생하면 코드, 증상, 오류 메시지/예외 사항 및 디버깅을 위해 그 시점까지 수행 한 작업으로 콜백하십시오. –
위에서 언급했듯이, 나는 이미 클라이언트를 실행하고 올바른 결과를 받았습니다. 난 그냥 내 서버의 스레딩 기능을 테스트하는 방법을 몰라 그래서 여기에 몇 가지 통찰력을 물었다. 대답을 얻는 데 도움이 될 것이라고 생각하여 코드를 게시했습니다. 나는 나의 고객을 자동화하려고 노력할 것이다. – pakmon