2012-03-03 2 views
1

최근에는 서버 - 클라이언트 연결을 시도하고 있습니다. 문제없이 1 대 1 연결을 만들 수 있었지만 이제는 여러 클라이언트를 허용하는 서버를 만들려고합니다. 그리고 서버가 연결을 수신 할 수없는 문제가 발생합니다. 연결을 위해 대기Java; 여러 클라이언트 연결을 허용하는 서버를 만들려고합니다.

년 - 메인 루프 : 핸들

public class ChatMultiServer { 

public static void main(String []args){ 

    int socknum = 124; 
    ServerSocket serverSocket; 
    Socket clientSocket; 

    while(true){ 

    ////opens socket 
    try{ 
     System.out.println("Opening port..."); 
     new ServerSocket(124).close(); 
     serverSocket = new ServerSocket(socknum); 
    }catch(Exception e){System.out.println("Error 101 = failed to bind to port "+socknum+"."); break;} 
    //////accepts connection 
    try{ 
     System.out.println("Waiting for connections..."); 
     clientSocket = serverSocket.accept(); 
    }catch(Exception e){System.out.println("Error 102 = failed to accept port "+socknum+"."); break;} 
    ///// 
    try{ 
     System.out.println("Initializing thread..."); 
     new Thread(new CMSThread(clientSocket)); 
    }catch(Exception e){System.out.println("Error 103 = failed to create thread."); break;} 
    try{ 
     serverSocket.close(); 
    }catch(Exception e){System.out.println("Error 105 = failed to close socket.");} 
    } 
} 

}

년 - 스레드 하나의 내가 여기에 자신 분명하지만 만들어 내 코드인지 잘 모르겠어요 ... 설립 연결 :

public class CMSThread extends Thread{ 
Socket socket; 
BufferedReader in; 
PrintWriter out; 
String username; 
char EOF = (char)0x00; 
public CMSThread(Socket s){ 
    socket = s; 
    run(); 
} 
public void run(){ 
    try{ 
    System.out.println("Setting up streams..."); 
    in = (new BufferedReader(new InputStreamReader(socket.getInputStream()))); 
    out = new PrintWriter(socket.getOutputStream()); 
    }catch(Exception e){System.out.println("Error 204 = failed to get streams");} 
    try{ 
     out.print("Welcome! you can quit at any tyme by writing EXIT.\nLet me introduce myself, I'm 'testprogram 1', but that doesn't really matter since you'll do the talking.\nWhat's your name?"+EOF); 
     out.flush(); 
     username = in.readLine(); 
     out.print("<b>"+username+"</b>, that's a nice name.\nWell, i'll shut up now. Have fun talking to yourself while whoever is running the server observes your conversation.\n"+EOF); 
     out.flush();  
    }catch(Exception e){System.out.println("Are you effin kidding me!? -.- whatever... Error 666 = failed to chat.");} 
} 

내 문제는 다시 한 번 말하지만 서버가 클라이언트와 연결되면 (GUI 작성이 쉽기 때문에 클라이언트 용 액션 스크립트를 사용하고 있습니다) 스레드가있을 때까지 대기합니다. 루프를 다시 시작하기 위해 실행을 마쳤습니다. 스레드가 채팅을 처리하는 것과 동시에 루프를 만들려고합니다.

나는 루프를위한 스레드와 연결을 처리하기위한 스레드를 만들어야 할 필요가 있다고 생각했지만, 어떻게해야하는지에 대해서는 확신 할 수 없다. 제 가설은 다소 옳았습니다. 그렇다면 대답에 대한 지침이 좋을 것입니다.

PS : 내 코드가 조금 지저분한 경우, 또는이 바보 같은 질문 경우, 나는 한 동안 자바 프로그램을 제작하지 않은 경우 나는

답변

2

... 미안 해요 당신은 실제로 시작되지 않습니다 새로운 글타래 - 그냥 run()에 직접 전화하고 있습니다. 지금까지 볼 수 있듯이 이것은 각 CMSThread 개체를 만드는 주 스레드에서 run()을 실행한다는 것을 의미합니다.

스레드를 시작하려면 thread.start()으로 전화해야합니다.

또한 왜 다른 스레드에서 CMSThread를 래핑하는지 확신 할 수 없습니다. CMSThread는 스레드를 확장하므로 자체적으로 시작할 수 있습니다. 래퍼 스레드가 시작되고 있지 않습니다.

그래서 당신이 필요합니다

 new CMSThread(clientSocket).start(); 

하고 CMSThread

+0

Alrigh의 생성자에서 run() 호출을 제거, 내가 그 앞의 코드에서이고, 다른 스레드 일의 포장 미안해 내가 변경 및 명백하게 나는 그 부분을 바꾸는 것을 잊었다. start()를 호출하려고하면 "start"메서드가 정의되지 않았 음을 알려주고 빈 start() 메서드를 만들려고했지만 이전과 같은 방식으로 작동합니다. -i는 run() 호출을 제거했습니다. – Ian

+0

'Thread.start()'를 의미합니다. 'CMSThread'가 다른 java.lang.Thread 클래스를 확장한다고 가정하고, 다른 Thread 클래스가 아니라면,'start()'메소드를 상속받습니다. [javadoc] (http://docs.oracle.com/javase/6) /docs/api/java/lang/Thread.html#start%28%29). and Java [threads tutorial] (http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html) – DNA

+0

아, 그래, 나는 이제 모든 것을 이해했다고 생각해. 그리고 그것은 작동합니다. 정말 고마워, 정말 도움이 : D 조 – Ian

관련 문제