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.