2011-04-19 3 views
3

J2ME를 사용하는 Bluetooth API를 사용하여 서버를 구현할 계획입니다. 여러 클라이언트가 동시에 연결할 수 있기를 원하지만 NET에서 그다지 많은 것을 찾을 수 없었습니다.j2me에서 블루투스 API를 사용하는 멀티 클라이언트 서버를 만드는 방법은 무엇입니까?

UUID uuid = new UUID("1101", false); 
    String SurveyAnswer=""; 
    //Create the service url 
    String connectionString = "btspp://localhost:" + uuid + ";name=xyz"; 
    //open server url 
    StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier) Connector.open(connectionString); 
    //Wait for client connection 
    System.out.println("\nServer Started. Waiting for clients to connect..."); 
    while(true){ 
    StreamConnection connection = streamConnNotifier.acceptAndOpen();  
    } 

어떻게 이러한 코드를 수정하여 다중 클라이언트 서버로 사용할 수 있습니까?

+0

코드의 오류는 무엇입니까? – funkybro

+0

한 번에 하나의 피어 만 연결할 수 있습니다. 피어가 서비스에 연결하려고하고 다른 피어의 요청이 처리 될 때 전자는 요청이 처리 될 때까지 대기해야합니다. –

답변

0

표준 문제입니다. StreamConnection 연결 = streamConnNotifier.acceptAndOpen(); 연결을 사용하는 Thread를 생성해야한다. 메인 쓰레드는 accept에 다시 들어가서 다음 연결을 기다린다.

UUID uuid = new UUID("1101", false);  String SurveyAnswer="";  
//Create the service url  
String connectionString = "btspp://localhost:" + uuid + ";name=xyz"; 
//open server url 
StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier) 
    Connector.open(connectionString); 
//Wait for client connection 
System.out.println("\nServer Started. Waiting for clients to connect..."); 
while(true){ 
    StreamConnection connection = streamConnNotifier.acceptAndOpen();  
    System.out.println("Client connected starting communication"); 
    new CommunicationThread(connection).start(); 
} 

CommunicationThreads run 메소드에서 스트림을 가져 와서 통신 할 수 있습니다.

+0

이 코드는 start() 메소드를 찾을 수 없다는 것을 보여줍니다. –

+0

CommunicationThread가 Thread를 확장합니까? start()는 스레드에 속한다. –

관련 문제