简体   繁体   English

更改活动时如何保持与服务器的TCP连接?

[英]How do you maintain TCP connection with a server when changing activity?

I am kind of desperate. 我有点绝望。 I am working on an application that is supposed to visualize pollution data. 我正在开发一个应该可视化污染数据的应用程序。 The application connects to the sensor via WiFi direct, the sensor is the group host. 应用程序通过WiFi直接连接到传感器,传感器是组主机。 Then I am supposed to connect to port 5001 and retrieve data every 2 minutes. 然后,我应该连接到端口5001并每2分钟检索一次数据。 This data is supposed to be sent to a google maps activity where it will be visualized in an infowindow. 该数据应发送到Google Maps活动,然后在信息窗口中将其可视化。

At this moment I have only been able to create a TCP connection that closes down immediately, I followed this Really simple TCP client . 目前,我只能创建一个立即关闭的TCP连接,我遵循了这个真正简单的TCP客户端

So I need to establish a TCP connection that does not close and also runs when I am in other activities. 因此,我需要建立一个不会关闭的TCP连接,并且在我从事其他活动时也可以运行。 I do not need to send any messages, only retrieve data. 我不需要发送任何消息,只需检索数据。

I looked into this How to keep the android client connected to the server even on activity changes and send data to server? 我研究了如何在活动发生变化时如何保持android客户端连接到服务器并将数据发送到服务器? but did not understand how I am to implement that code for my case. 但不了解如何为我的案例实现该代码。

How do you do this? 你怎么做到这一点? Appreciate any help. 感谢任何帮助。

Your android App is a Client, every time you need to get data you can ask it from the Server. 您的android应用是一个客户端,每当您需要获取数据时,都可以从服务器询问它。

If Server working in Blocking mode you can send a request to the Server and Server sent Response through the opened connection 如果服务器在阻止模式下工作,则可以通过打开的连接向服务器发送请求,并且服务器发送响应

Here is some examples of Client/Server connection with Socket : 这是使用Socket进行客户端/服务器连接的一些示例:

https://examples.javacodegeeks.com/android/core/socket-core/android-socket-example/ https://examples.javacodegeeks.com/android/core/socket-core/android-socket-example/

https://github.com/codepath/android_guides/wiki/Sending-and-Receiving-Data-with-Sockets https://github.com/codepath/android_guides/wiki/Sending-and-Receiving-Data-with-Sockets

You can find more examples 您可以找到更多示例

At this moment I have only been able to create a TCP connection that closes down immediately 目前,我只能创建一个立即关闭的TCP连接

It relies on your Server design, Client app will send the request to the Server and if Server send back the response you can get it, but if the Server have no Response to sent your connection will be closed 它取决于您的服务器设计,客户端应用程序会将请求发送到服务器,如果服务器将响应发送回,则可以获取请求,但是如果服务器没有响应,则连接将关闭

So I need to establish a TCP connection that does not close and also runs when I am in other activities. 因此,我需要建立一个不会关闭的TCP连接,并且在我从事其他活动时也可以运行。 I do not need to send any messages, only retrieve data 我不需要发送任何消息,只需检索数据

See the links above, there is no need to create a connection that does not close, you should send your request and get the response from Server every time you need 请参见上面的链接,无需创建不会关闭的连接,您应在每次需要时发送请求并从服务器获取响应

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

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