• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to keep sending data over the same socket(same port on server)?

I'm coding an app where the user can input a text message and send it over UDP. My MainActivity consists of a page where the user inputs the ip address of the server and the port. As soon as the user presses on the connect button, it will create a socket and send an empty packet. Also when the user presses the connect button, the SecondActivity will start. The SecondActivity consists of a EditText input field, where text can be inserted and sent. I want the messages sent over the same UDP socket created in the MainActivity. I got a separate class for the UDP client. How can I code this?


ps I can upload my code on request
 
Well the quick solution is to declare your Socket as 'public static' in your MainActivity class. It can then be accessed from any other class, including your UDP client.

Code:
class MainActivity {
  public static Socket socket;
  ...
  // Open the socket
  ...
}

Code:
class childActivity {
  ...
  // Use the socket
  MainActivity.socket.getOutputStream();
  ...
}
 
Last edited by a moderator:
Back
Top Bottom