2010-01-13 3 views
0

이것은 서버 응용 프로그램에 속한 내 Main 클래스입니다! 하지만 클라이언트 응용 프로그램을 실행하지 않으면이 문장이 콘솔에 기록됩니다. 왜 도와 주시겠습니까? 감사합니다.네트워크 문제! (멀티 스레드 클라이언트/서버)

내 주요 클래스 : 콘솔에서

public class Main { 

static Socket client = null; 
static ServerSocket server = null; 

// We can have 10 clients' connections 
static ClientThread t[] = new ClientThread[10]; 

public static void main(String args[]) { 
    System.out.println("Server is starting..."); 
    System.out.println("Server is listening..."); 
    try { 
     server = new ServerSocket(5050); 
     System.out.println("Client Connected..."); 


     while (true) { 

      client = server.accept(); 
      for (int i = 0; i <= 9; i++) { 
       if (t[i] == null) { 
        (t[i] = new ClientThread(client, t)).start(); 
        break; 
       } 
      } 
     } 
    } catch (IOException e) { 
     System.out.println(e); 
    } 
} 
} 

// This client thread opens the input and the output streams for a particular client, 
// ask the client's name, informs all the clients currently connected to the 
// server about the fact that a new client has joined the chat room, 
// and as long as it receive data, echos that data back to all other clients. 
// When the client leaves the chat room this thread informs also all the 
// clients about that and terminates. 

class ClientThread extends Thread { 

DataInputStream is = null; 
PrintStream os = null; 
Socket clientSocket = null; 
ClientThread t[]; 

public ClientThread(Socket clientSocket, ClientThread[] t) { 
    this.clientSocket = clientSocket; 
    this.t = t; 
} 

@Override 
public void run() { 
    String line; 
    String name; 
    try { 
     is = new DataInputStream(clientSocket.getInputStream()); 
     os = new PrintStream(clientSocket.getOutputStream()); 
     os.println("Enter your name."); 
     name = is.readLine(); 
     os.println("Hello " + name + " to our chat room.\nTo leave enter /quit in a new line"); 
     for (int i = 0; i <= 9; i++) { 
      if (t[i] != null && t[i] != this) { 
       t[i].os.println("*** A new user " + name + " entered the chat room !!! ***"); 
      } 
     } 
     while (true) { 
      line = is.readLine(); 
      if (line.startsWith("/quit")) { 
       break; 
      } 
      for (int i = 0; i <= 9; i++) { 
       if (t[i] != null) { 
        t[i].os.println("<" + name + "> " + line); 
       } 
      } 
     } 
     for (int i = 0; i <= 9; i++) { 
      if (t[i] != null && t[i] != this) { 
       t[i].os.println("*** The user " + name + " is leaving the chat room !!! ***"); 
      } 
     } 

     os.println("*** Bye " + name + " ***"); 

     // Clean up: 
     // Set to null the current thread variable such that other client could 
     // be accepted by the server 

     for (int i = 0; i <= 9; i++) { 
      if (t[i] == this) { 
       t[i] = null; 
      } 
     } 

     // close the output stream 
     // close the input stream 
     // close the socket 

     is.close(); 
     os.close(); 
     clientSocket.close(); 
    } catch (IOException e) { 
     System.out.println(e); 
    } 
}} 

:

init: 
deps-jar: 
compile-single: 
run-single: 
Server is starting... 
Server is listening... 
Client Connected... 

답변

1

당신이 소켓을 만들고 연결을 수락하기 전에 클라이언트가 연결되어 있는지 선언

 server = new ServerSocket(5050); 
     System.out.println("Client Connected..."); 

을 인쇄해야 함

클라이언트가이 라인 이후

접속된다

client = server.accept(); 

클라이언트 실제로

을 연결하는 블록까지