如何在Android上查看本地服务器?

时间:2017-05-05 04:52:33

标签: android sockets networking server

我对Android开发领域相对较新,遇到了一个我无法在网上找到任何问题的问题。我想知道查看本地服务器列表的最佳方式是什么。

方案: 在我的网络上托管了2个“派对”。我想在列表中查看这些“派对”的标题,然后选择一个连接到。

我知道首先必须建立连接才能在主机和客户端之间发送和接收数据,但我希望潜在的客户端能够从主机接收连接信息以便连接。 / p>

1 个答案:

答案 0 :(得分:0)

一种可能的方法是使服务器在特定端口上广播UDP数据包并侦听另一个端口上的传入TCP连接。客户端将侦听特定端口并更新服务器列表。在选择服务器时,客户端应与其他端口上的服务器建立TCP连接。

示例UDP服务侦听器

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import org.apache.http.util.ExceptionUtils;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;


/*
 * Linux command to send UDP:
 * #socat - UDP-DATAGRAM:192.168.1.255:11111,broadcast,sp=11111
 */
public class UDPListenerService extends Service {
static String UDP_BROADCAST = "UDPBroadcast";

//Boolean shouldListenForUDPBroadcast = false;
DatagramSocket socket;

private void listenAndWaitAndThrowIntent(InetAddress broadcastIP, Integer port) throws Exception {
    byte[] recvBuf = new byte[15000];
    if (socket == null || socket.isClosed()) {
        socket = new DatagramSocket(port, broadcastIP);
        socket.setBroadcast(true);
    }
    //socket.setSoTimeout(1000);
    DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
    Log.e("UDP", "Waiting for UDP broadcast");
    socket.receive(packet);

    String senderIP = packet.getAddress().getHostAddress();
    String message = new String(packet.getData()).trim();

    Log.e("UDP", "Got UDB broadcast from " + senderIP + ", message: " + message);

    broadcastIntent(senderIP, message);
    socket.close();
}

private void broadcastIntent(String senderIP, String message) {
    Intent intent = new Intent(UDPListenerService.UDP_BROADCAST);
    intent.putExtra("sender", senderIP);
    intent.putExtra("message", message);
    sendBroadcast(intent);
}

Thread UDPBroadcastThread;

void startListenForUDPBroadcast() {
    UDPBroadcastThread = new Thread(new Runnable() {
        public void run() {
            try {
                InetAddress broadcastIP = InetAddress.getByName("172.16.238.255"); //172.16.238.42 //192.168.1.255
                Integer port = 11111;
                while (shouldRestartSocketListen) {
                    listenAndWaitAndThrowIntent(broadcastIP, port);
                }
                //if (!shouldListenForUDPBroadcast) throw new ThreadDeath();
            } catch (Exception e) {
                Log.i("UDP", "no longer listening for UDP broadcasts cause of error " + e.getMessage());
            }
        }
    });
    UDPBroadcastThread.start();
}

private Boolean shouldRestartSocketListen=true;

void stopListen() {
    shouldRestartSocketListen = false;
    socket.close();
}

@Override
public void onCreate() {

};

@Override
public void onDestroy() {
    stopListen();
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    shouldRestartSocketListen = true;
    startListenForUDPBroadcast();
    Log.i("UDP", "Service started");
    return START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

Code gist