2

I am trying to set up a multiplayer hangman android application using two emulators. I am trying to have one emulator act as the server and the other emulator act as a client. I am having trouble creating a server on one emulator and understanding how to grab information that would be pushed onto the server. I am very new to android and network programming.

I have read the android documentation, so I know that networking for emulators is possible. I am just having trouble following the instructions given. https://developer.android.com/studio/run/emulator-networking

public class CreateServer extends AppCompatActivity {

    private Socket socket;
    private ServerSocket server;
    private DataInputStream streamIn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_server);
        new makeAServer().execute();

    }

    public class makeAServer extends AsyncTask<Void, Void, Void> {
        TextView content = findViewById(R.id.TextFromClient);



        @Override
        protected Void doInBackground(Void... voids) {
            try {
                server = new ServerSocket(5151);
                socket = server.accept();

                streamIn = new DataInputStream(new BufferedInputStream (socket.getInputStream()));
                while (true) {
                    String line = streamIn.readUTF();
                    content.setText(line);
                }
            } catch (IOException ioe) {
                Log.d("Message 10:", "IOException");
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
        }
    }


}

I tried to adapt a tutorial for making a server, but I tell through the logs that this code is not executed properly.

TaylorM
  • 21
  • 2
  • So you are following this: https://developer.android.com/studio/run/emulator-networking#connecting ? You should also specify what is your environment (PC/Mac/Linux) and if you were successful in connecting to each emulator's console: https://developer.android.com/studio/run/emulator-networking#consoleredir – Morrison Chang Apr 20 '19 at 05:04
  • I am using a PC environment. Since I posted this question, I have learned a lot more about network programming. I can connect my host device to the emulator through telnet. However, I am still not seeing any results in the log. I know that I need to open a server using an async function due to network restrictions on the UI thread, but I am confused on how the async is working because I can't see the logs. – TaylorM Apr 20 '19 at 21:38
  • Check if the following answers your question: https://stackoverflow.com/questions/4278037/communication-between-two-android-emulators – EpicPandaForce Sep 29 '21 at 21:11

0 Answers0