我计划使用 J2ME 使用蓝牙 API 实现一个服务器。我希望多个客户端能够同时连接到它,但我在 NET 上找不到太多。
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();
}
如何修改这些代码以使其作为多客户端服务器工作?
最佳答案
这是一个标准问题。当 StreamConnection connection = streamConnNotifier.acceptAndOpen();返回你必须产生一个使用连接的线程。主线程然后重新进入接受并等待下一个连接。
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 方法中,您可以获取流并进行通信。
关于java-me - 如何在 j2me 中使用蓝牙 API 制作多客户端服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5721133/