客户端/服务器/客户端通过套接字通信

XS

我有3个通过蓝牙PAN网络连接的设备。

  • 设备1:服务器-在我的情况下,服务器是EV3乐高积木==机器人!
  • 设备2:远程-第二个设备(Android APP)应用于控制设备1的操作
  • 设备3:前端-第三台设备应显示所选操作(Android)

可能的通信方法是JAVA中的蓝牙和套接字连接。我已经可以从DEVICE 2控制DEVICE 1-但命令不会中继到DEVICE3。这是我用于服务器的代码:

主要的

        try {
        serverSocket = new ServerSocket( 1111 );
    } catch (IOException e) {
        e.printStackTrace();

    }
    while (true) {
        try {
            socket = serverSocket.accept();
        } catch (IOException e) {
            System.out.println("I/O error: " + e);
        }
        // new thread for a client
        new RelayThread(socket).start();
    }

RelayThread螺纹

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-Client作为我的DEVICE 3,但是当我通过DEVICE 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); 
   }

和向其他客户端发送消息的方法

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);
     }
  }
}

并且,RelayThread的构造函数保留Main

Main main;
public RelayThread (Socket clientSocket,Main main) {
    this.socket = clientSocket;
    this.main = main;
}

并且,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
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Java套接字。服务器-客户端通信

C#中的异步套接字服务器,通过套接字服务器进行客户端到客户端的通信

Java TCP 客户端/服务器套接字

客户端js上的套接字服务器?

套接字Java客户端-Python服务器

服务器/客户端套接字连接

套接字编程-简单的客户端/服务器

Java客户端/服务器套接字问题

UDP 客户端/服务器程序,通过不同 IP 和端口上的 2 个套接字进行通信

客户端-服务器套接字通信TCP / IP中的对象问题

TCP 套接字客户端和 protobuf 可以与 gRPC 服务器通信吗?

客户端服务器与套接字C的基本通信

在C ++中使用TCP套接字进行远程客户端和服务器通信

我想在客户端使用网络套接字进行服务器通信。

Java中通过套接字进行客户端服务器身份验证

通过套接字将Java客户端中的int写入c服务器

如何使用 Java 客户端和 Python 服务器通过套接字创建 IPC?

通过Java中的套接字将文件从客户端传输到服务器

通过袜子代理的 SSL 套接字 - 客户端 <---> 服务器数据是否加密?

服务器无法通过C中的套接字向客户端发送消息

Python 3:通过套接字发送文件。(客户端-服务器程序)

通过套接字连接将图像从客户端发送到服务器

通过ActionScript服务器和Java客户端之间的套接字发送对象

通过python中的TCP套接字在客户端-服务器之间发送文件?

通过套接字输出流从 Swift 客户端向 Java 服务器发送消息

Java套接字服务器-客户端;卡在服务器端

C ++套接字客户端/服务器服务器不响应客户端

通过中央服务器连接通过不同线程连接的两个套接字客户端作为客户端服务器对

带套接字的TCP客户端/服务器,服务器向客户端发送文件,客户端挂起,Python