Android Introduction Hello Socket Programming TCP and UDP

  • Slides: 6
Download presentation
Android Introduction Hello Socket Programming TCP and UDP 1

Android Introduction Hello Socket Programming TCP and UDP 1

Goal Convert the Java Socket Programming Project to Android Text input from the user

Goal Convert the Java Socket Programming Project to Android Text input from the user – sent to the same server(s) as before Answer from the server(s) Sends the input from the user to the UPD Server Sends the input from the user to the TCP Server 2

Layout Create a simple layout that will include the required elements: Edit. Text. View

Layout Create a simple layout that will include the required elements: Edit. Text. View Buttons Register one “On. Clicked. Listener()” for each of the buttons Test functionality (make a toast for each listener) 3

Socket Programming Define most variables as members of the main Activity Class Rename: Datagram.

Socket Programming Define most variables as members of the main Activity Class Rename: Datagram. Socket client. Socket -> Datagram. Socket udp. Client. Socket client. Socket -> Socket tcp. Socket; Adapt the code from pure Java to Android (especially the input and output strings) by filling in the two On. Click. Listeners 4

Handling Exceptions Many network operations throw exceptions (as they can fail). In the original

Handling Exceptions Many network operations throw exceptions (as they can fail). In the original code they were just thrown out of main – in Android we have to handle them! Try{ Operations that may fail and throw an exception; }catch (Exception e) { Log. e(TAG, "Caught UDP Exception: “ + e. get. Message()); Toast. make. Text(Hello. Nets. this, "UDP Error"+ e. get. Message()), Toast. LENGTH_LONG). show(); } 5

Running it First give the application INTERNET permissions (like in the Web. View) Implement

Running it First give the application INTERNET permissions (like in the Web. View) Implement the UDP and TCP functionality separately Run and debug. 6