通过wifi为Android设备的Udp和Tcp通信 [英] Udp and Tcp communication for android devices over wifi

查看:505
本文介绍了通过wifi为Android设备的Udp和Tcp通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

一个房间里有十个或更多的Android设备。其中一人开始向其他人广播他的名字和IP地址。

其他设备接收它并向第一个设备发送其名称和IP地址。第一个设备逐个接收名称和IP地址,并向它们发送一个字符串。这种情况可能是循环的。

我经常搜索并找到TCP和UDP协议。但UDP不可靠,速度快,对广播有用,TCP可靠,速度慢,对单播有用。

现在我决定用UDP做第一步(由其中一个设备广播),之后检测IP地址使TCP通信。

这是正确的工作吗?我可以这样做吗?

谢谢你

Hello,
There are ten android devices or more in a room. One of them begins to broadcast his name and IP address to other.
Other devices receive it and send to first device their names and their IP addresses. The first device receives names and IP addresses one by one and sends them a string. This scenario may be loop.
I search a lot and find TCP and UDP protocols for doing it. But UDP is unreliable, speedier, useful for broadcast, and TCP is reliable, slower, useful for unicast.
Now I decide to do first step(broadcast by one of the devices) with UDP, and after detecting IP addresses make TCP communication.
Is that a correct work? Can I do it?
Thanks

推荐答案

你在使用TCP(或UDP,这是一种无连接协议,类似于TCP)而感到困惑和网络拓扑,在你的情况下响铃也许。



在这种情况下,我会建议你不要写这样的东西。相反,我建议您为TCP客户端创建服务器并让它与网络通信。通过您的本地网络,从路由器。该服务器将侦听来自客户端的每条消息,客户端将通过网络在服务器上共享其数据。然后,您可以为每个客户端生成响应。



第一步是熟悉用Java联网 [ ^ ]。 Android使用Java作为官方编程语言,因此用Java编写的程序在Android中可以公平运行,并且根据Android标准进行一些更改。



在Android中,这些关于网络的资源可以帮助您开始通过WiFi和其他媒介进行数据共享。 http://developer.android.com/training/building-connectivity.html [ ^ ]
You are confusing yourself with TCP (or UDP which is a connectionless protocol, similar to TCP) and network topology, ring in your case maybe.

I would, in this case, suggest that you do not write such a thing. Instead, I would recommend that you create a server for TCP clients and let it communicate with the network. Through your local network, from the router. That server would listen to every message from the clients, client would share their data on the server through network. Then you can generate a response for each of the client.

First step is to get familiar with networking in Java[^]. Android uses Java as official programming language, thus a program written in Java would work fair in Android with a few changes as per Android standards.

In Android, these resources about networking can help you getting started for communicating through WiFi and other mediums for data sharing. http://developer.android.com/training/building-connectivity.html[^]


这篇关于通过wifi为Android设备的Udp和Tcp通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆