Java 通过套接字进行客户端/服务器/客户端通信
我有3台设备通过蓝牙PAN网络连接Java 通过套接字进行客户端/服务器/客户端通信,java,android,sockets,bluetooth,mindstorms,Java,Android,Sockets,Bluetooth,Mindstorms,我有3台设备通过蓝牙PAN网络连接 设备1:服务器-在我的例子中,服务器是EV3乐高积木==a 机器人 设备2:远程-第二台设备(Android应用程序)应为 用于控制设备1的动作 设备3:前端-设备 第三台设备应显示所选操作(Android) 可能的通信方法是蓝牙和JAVA中的套接字连接。我已经可以从设备2控制设备1,但命令不会中继到设备3。这是我在服务器上使用的代码: 主要 重读线程 public class RelayThread extends Thread { protected
- 设备1:服务器-在我的例子中,服务器是EV3乐高积木==a 机器人李>
- 设备2:远程-第二台设备(Android应用程序)应为 用于控制设备1的动作
- 设备3:前端-设备 第三台设备应显示所选操作(Android)
public class RelayThread extends Thread {
protected Socket socket;
BufferedReader bufferedReader;
public RelayThread (Socket clientSocket) {
this.socket = clientSocket;
}
public void run() {
Singleton motors = Singleton.getInstance();
InputStream inp = null;
BufferedReader brinp = null;
DataOutputStream out = null;
try {
inp = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(inp, "UTF-8");
bufferedReader = new BufferedReader(isr);
out = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
return;
}
while (true) {
try {
String command= bufferedReader.readLine();
if ((command== null) || command.equalsIgnoreCase("QUIT")) {
socket.close();
return;
}
else {
// do ROBOT actions
/*
* SERVER ACTIONS
*/
// notify the other client of the delivered LINE
out.writeBytes(command);
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}
我现在正在使用TCP客户端作为我的设备3,但当我通过设备2发送命令时,它不会显示任何文本。我如何实现我的项目?我做错了什么?这是为您的服务器准备的。创建所有连接的列表
List<RelayThread> clients = new ArrayList<RelayThread>();
while (true) {
try {
socket = serverSocket.accept();
} catch (IOException e) {
System.out.println("I/O error: " + e);
}
// new thread for a client
RelayThread relay = new RelayThread(socket,this);
relay.start();
clients.add(relay);
}
以及RelayThread中的发送者消息
public void sendCommand(String command){
out.writeBytes((command+"\r\n").getBytes()); // I suggest you add a parser charachter, like \r\n. then client can understand message ends
}
您需要首先将消息传递到服务器,然后服务器将其发送到其他客户端。那么,您的服务器是否从device-2读取消息并将其发送到device-3?我在你的代码中没有看到它,这就是为什么我在中继线程中使用“out.writeBytes(command);”和“in-are-use-same-socket”。这意味着,他们使用相同的客户端。所以在您的情况下,如果设备2发送命令,设备2将再次获得命令。您需要在服务器的列表中保留中继线程,并对它们进行迭代,然后将来自任何客户端的消息传递给所有客户端。这将是您的解决方案。看看我的回答Hi Adem,我看不到你的答案,除了评论?非常感谢你的代码-我已经知道它的方向了。但是,我无法创建客户列表。行“RelayThread relay=new RelayThread(socket,this).start();”抛出以下错误“RelayThread relay=new RelayThread(socket,this).start();”我想我需要调整RelayThread的构造函数,或者?谢谢!到目前为止,我一直在用类“main”的静态main方法初始化服务器。。。然而,这似乎和“this”引用不符。我将尝试重构代码。我已经重构了我的代码并使其正常工作:)。我只需要弄清楚如何在我的客户机上保持连接打开。基本上,客户机列表填充得非常快(我从设备2传输的每个命令都是服务器中的另一个客户机)
public void sendCommand(String command, RelayThread source){
for (int i=0;i<clients.size();i++){
if (clients.get(i) != source) {
clients.get(i).sendCommand(command);
}
}
}
Main main;
public RelayThread (Socket clientSocket,Main main) {
this.socket = clientSocket;
this.main = main;
}
public void sendCommand(String command){
out.writeBytes((command+"\r\n").getBytes()); // I suggest you add a parser charachter, like \r\n. then client can understand message ends
}