简体   繁体   English

android平板电脑,路由器和2个设备之间的单向连接

[英]One-way connection between android tablet, router and 2 devices

I've got question, that I haven't found answer for yet. 我有一个问题,我还没有找到答案。 I have 2 devices with wifi each, that are sending special data. 我有2个带有wifi的设备,它们正在发送特殊数据。 I want to show this data at the same moment on a tablet. 我想同时在平板电脑上显示这些数据。 There is a router with network, both tablet and that devices are connected to this network. 有一个具有网络的路由器,平板电脑和设备都已连接到该网络。

How to solve this? 如何解决呢? Should I use serversocket? 我应该使用serversocket吗? I don't know if I explained it clear enought, if not, please ask. 我不知道我是否解释得足够清楚,否则请询问。 Thanks for any response. 感谢您的任何回复。

I have the same application running on the company I work. 我在我工作的公司上运行相同的应用程序。 The "device" is a micro-controller based device that is implemented the lwIP (lightweight IP protocol) and it's listening to the port 83 and every 500ms the tablet goes and read new fresh data and plot it in a graph. “设备”是一种基于微控制器的设备,已实现了lwIP(轻型IP协议),并且它正在监听端口83,平板电脑每经过500ms就会读取新的新鲜数据并将其绘制成图表。 Works like a charm. 奇迹般有效。

(in case you'll be plotting charts, I used the AChartEngine and you can check on my profile a question/answer on it with some useful info) (以防您要绘制图表,我使用了AChartEngine,您可以在我的个人资料上查看有关问题/答案的有用信息)

the code below is a simplified version of what I'm doing. 下面的代码是我正在做的简化版本。 The complete version includes SEVERAL try{ } catch() { } in case it catches an exception it try closing the socket and return null; 完整的版本包括几个 try{ } catch() { } ,以防万一它捕获到异常并尝试关闭套接字并返回null。

public static String SendMessage(String message, String ip, int port) {

    // Connect to host ==================================
    Socket socket = new Socket();
    socket.setSoTimeout(TIMEOUT);
    InetSocketAddress addr = new InetSocketAddress(ip, port);
    socket.connect(addr, TIMEOUT);

    // Send Message ======================================
    byte[] outputBuffer = message.getBytes();
    socket.getOutputStream().write(outputBuffer);

    // Zero the input buffer =============================
    for (int i = 0; i < inputBuffer.length; i++) {
        inputBuffer[i] = 0;
    }

    // Read the response ==================================
    int count = 0;
    do {
        count = socket.getInputStream().read(inputBuffer);
    } while (count != -1);

    // Close connection ====================================
    close(socket);

    // Return message ======================================
    return new String(inputBuffer).trim();
}

hope it helps, happy coding. 希望对您有所帮助,编码愉快。

1. Socket will be a good idea. 1.套接字将是一个好主意。

For Sending : 发送 :

Socket s = new Socket();
s.connect(new InetSocketAddress("IP_ADDR",PORT_NO);

OutputStream o = s.getOutputStream();
PrintWriter pw = new PrintWriter(o);
pw.write(msg);   // msg will be the data needed to send

For Receiving: 接收:

Socket s = new Socket();
s.connect(new InetSocketAddress("IP_ADDR",PORT_NO);

InputStream i = s.getInputStream();
InputStreamReader isr = new InputStreamReader(i);
BufferedReader br = new BufferedReader(isr);
String str = new String();

while((str=br.readLine())!=null){

   System.out.println(str);  // do whatever u want to do with str, the data read


 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM