如何在Android中获取服务器(套接字)的IP地址? [英] How to get IP Address of Server (Socket) in Android?

查看:419
本文介绍了如何在Android中获取服务器(套接字)的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个连接两个android设备的简单聊天程序,它们可以发送简单消息 我在端口(例如1234)上使用Socket运行服务器

I'm creating a simple chat program that connects two android devices and they can send simple message I run the server with Socket on a port (1234 for example)

问题出在客户端,我不知道服务器的IP地址. (我不想手动输入) 有没有办法找到在特定端口上运行的服务器?或者我可以在可以将其提供给客户端的某些特定静态IP上运行服务器?

The problem is from the client i do not know the server IP Address. (and i dont want to enter it manually) is there a way to find a server that is running on a specific port?or can i run the server on some specific static IP that i can give it to clients?

是否没有其他方法可以与可在Android 2.2+上运行的android设备通信(不想使用wifi Direct)?

if not is there another way to communicate with android devices that works on Android 2.2+(don't want to use wifi direct) ?

预先感谢

推荐答案

您可以从服务器广播一条udp消息,并在消息中指定您的IP.让客户端接收广播,并使用该味精作为IP地址连接到套接字. (PS:广播接收者必须b被允许接收广播).您完成了!

You can broadcast a udp message from the server specifying your ip in the message. Let the client receive the broadcast and use that msg as the ip address to connect to the socket. (PS : The broadcast receiver must b allowed to receive broadcasts). And you are done!

这篇关于如何在Android中获取服务器(套接字)的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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