Android网络搜索

时间:2016-03-12 15:25:23

标签: android sockets wifi

我正在创建一个应用程序,需要人们通过WiFi连接到彼此才能让他们一起玩。

但是,我不想通过IP地址搜索某个人,我想搜索当前主持游戏的所有人,并更新我可以点击进行连接的人员列表。

服务器

public class server extends AppCompatActivity {
int numPlayers=0;
int count = 0;
TextView infoip, msg;
String message = "";
ServerSocket serverSocket;
private String name="server";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_server);
    Bundle b = this.getIntent().getExtras();
    numPlayers = b.getInt("numPlayers");
    //name = b.getString("name");

    infoip = (TextView) findViewById(R.id.serverText);
    msg = (TextView) findViewById(R.id.serverPlayerCount);

    infoip.setText("Hosting...");

    Thread socketServerThread = new Thread(new SocketServerThread());
    socketServerThread.start();
    ButtClickListener();
}
@Override
protected void onDestroy() {
    if (serverSocket != null) {
        try {
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }
 }
private void ButtClickListener() {
    Button cancel = (Button) findViewById(R.id.severQuit);
    cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}

private class SocketServerThread extends Thread 

{
static final int SocketServerPORT = 8080;

@Override
public void run() {
    try {
        serverSocket = new ServerSocket(SocketServerPORT);
        server.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
            }
        });

        while (true) {
            Socket socket = serverSocket.accept();
            count++;
            message += "#" + count + " from " + socket.getInetAddress()
                    + ":" + socket.getPort() + "\n";

            server.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    msg.setText(message);
                }
            });

            SocketServerReplyThread socketServerReplyThread = new SocketServerReplyThread(
                    socket, count);
            socketServerReplyThread.run();

        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

private class SocketServerReplyThread extends Thread {

private Socket hostThreadSocket;

SocketServerReplyThread(Socket socket, int c) {
    hostThreadSocket = socket;
    count = c;
}

@Override
public void run() {
    OutputStream outputStream;
    String msgReply = "You are the #" + count+" person out of "+count+"/"+numPlayers;

    try {
        outputStream = hostThreadSocket.getOutputStream();
        PrintStream printStream = new PrintStream(outputStream);
        printStream.print(msgReply);
        printStream.close();

        message += msgReply + "\n";

        server.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                msg.setText(message);
            }
        });

    } catch (IOException e) {
        e.printStackTrace();
        message += "Error " + e.toString() + "\n";
    }

    server.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            msg.setText(message);
        }
    });
}

}

   private String getIpAddress() {
        String ip = "";
        try {
            Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
                    .getNetworkInterfaces();
            while (enumNetworkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = enumNetworkInterfaces
                        .nextElement();
            Enumeration<InetAddress> enumInetAddress = networkInterface
                    .getInetAddresses();
            while (enumInetAddress.hasMoreElements()) {
                InetAddress inetAddress = enumInetAddress.nextElement();

                if (inetAddress.isSiteLocalAddress()) {
                    ip += "SiteLocalAddress: "
                            + inetAddress.getHostAddress() + "\n";
                }

            }

        }

    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        ip += "Something Wrong! " + e.toString() + "\n";
    }

    return ip;
}
}

客户端

public class ClientThread implements Runnable {

    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
            Socket socket = new Socket(serverAddr,
                    8080);
            connected = true;
            while (connected) {
                try {
                } catch (Exception e) {
                }
            }
            socket.close();
        } catch (Exception e) {
            connected = false;
        }
    }
}

当我通过点击找到服务器时,我将能够通过名称连接到服务器来实现上述代码,但我不知道如何搜索服务器。

我如何修改客户端,以便在运行时搜索所有可能的服务器并返回服务器名称列表?

感谢。

1 个答案:

答案 0 :(得分:0)

我认为这是网络服务发现的一个很好的用例http://developer.android.com/training/connect-devices-wirelessly/nsd.html