1 Android Introduction Hello Threads. 2 Goal Create an application that uses a background thread as a UDP server to receive messages from the UDP client.

Slides:



Advertisements
Similar presentations
Programming with Android: Network Operations
Advertisements

Socket UDP H. Fauconnier 1-1 M2-Internet Java. UDP H. Fauconnier M2-Internet Java 2.
Bluetooth. Wireless networking technology Limited broadcast range (< 10 meters) Example uses include Connecting headsets to phones, keyboard/mouse to.
Services. Application component No user interface Two main uses Performing background processing Supporting remote method execution.
SOCKET PROGRAMMING WITH MOBILE SOCKET CLIENT DEARTMENT OF COMPUTER SCIENCE IOWA STATE UNIVERSITY.
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
WiFi in Android.
Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
@2011 Mihail L. Sichitiu1 Android Introduction Hello Socket Programming TCP and UDP.
Jan Java Networking UDP Yangjun Chen Dept. Business Computing University of Winnipeg.
2: Application Layer 1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive.
Internetworking (Contd) Chapter 4. Figure 3.26 ATM protocol layers.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
1 Network Layers Application Transport Network Data-Link Physical bits.
2: Application Layer 1 Socket Programming Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose, Keith Ross Addison-Wesley,
19-Aug-15 About the Chat program. 2 Constraints You can't have two programs (or two copies of the same program) listen to the same port on the same machine.
2: Application Layer 1 Socket Programming TCP and UDP.
Practical Session 11 Multi Client-Server Java NIO.
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
Winter 2002Suprakash Datta1 Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
-1- Georgia State UniversitySensorweb Research Laboratory CSC4220/6220 Computer Networks Dr. WenZhan Song Associate Professor, Computer Science.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Socket Programming Lee, Sooyong
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Network Programming and Sockets CPSC 363 Computer Networks Ellen Walker Hiram College (Includes figures from Computer Networking by Kurose & Ross, © Addison.
Threads and Services. Background Processes One of the key differences between Android and iPhone is the ability to run things in the background on Android.
Socket Programming Tutorial. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
Java Socket programming. Socket programming with TCP.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
2: Application Layer1 Socket programming Socket API Explicitly created, used, released by apps Client/server paradigm Two types of transport service via.
@2010 Mihail L. Sichitiu1 Android Introduction Hello Threads.
Practical Session 11 Multi Client-Server Java NIO.
1 CSCD 330 Network Programming Spring 2014 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 7 Application.
1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of.
1 Netprog 2002 TCP/IP UDP/IP in Java Based on Java Network Programming and Distributed Computing.
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 11 Omar Meqdadi Department of Computer Science and Software Engineering University.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
Network Programming Communication between processes Many approaches:
Object-Orientated Analysis, Design and Programming
Echo Networking COMP
Block 14 Group Communication (Multicast)
Network Programming Introduction
Socket programming with TCP
Hello Socket Programming TCP and UDP
Android Introduction Hello Threads.
Chapter 2: outline 2.1 principles of network applications
Socket programming - Java
CSCD 330 Network Programming
Software Engineering for Internet Applications
Socket Programming.
CSCD 330 Network Programming
Socket Programming 2: Application Layer.
CPSC 441 UDP Socket Programming
Chapter 2: Application layer
Multiplexing/Demux.
Socket Programming with UDP
Based on Java Network Programming and Distributed Computing
Presentation transcript:

1 Android Introduction Hello Threads

2 Goal Create an application that uses a background thread as a UDP server to receive messages from the UDP client Text sent from UDPClient

3 Layout Text sent from UDPClient TextView that changes from Starting Server to Server Started (optional) TextView that shows the text sent from UDPClient TextView that shows the IP address of the server (needed for UDPClient) Clear Button – clears the Last Message (and shows that interface is still responsive)

4 Application Structure Main Activity ServerThreadUDPClient OnCreate( ) Create the thread Start the thread OnDestroy( ) close the socket Constructor Open the Socket If successful – Server Started Find and display the IP Address run( ) while (socketStillOpen){ receive packet display Message send reply } Read line from input Send line to Server Receive line from Server Display line OnClickListener( ) Clear the Last Message Message Handler HelloThreadsUDPClient

5 Main Activity (HelloThreads) ClassMembers TextView isRunning,myIPAddressField,lastMessage; Buttonclear; ServerThread myThread; Handler OnCreate( ) Get handles (findViewById) to all GUI elements Create ServerThread: myThread=new ServerThread(getApplicationContext(),mHandler) Start the Thread: myThread.start(); Register the OnClickListener for the Clear Button OnDestroy( ) myThread.closeSocket(); OnClickListener( ) Clear the Last Message Handler Definition on Next page

The Handler Definition private final Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case PACKET_CAME: String incomingMessage = (String) msg.obj; lastMessage.setText(incomingMessage); break; case IS_RUNNING: String socketStatus = (String) msg.obj; isRunning.setText(socketStatus); break; case IP_ADDRESS: InetAddress myIPAddress = (InetAddress) msg.obj; myIPAddressField.setText("Server IP Address:"+myIPAddress.toString()); break; } }; 6

7 ServerThread public class ServerThread extends Thread Class Members Handler mHandler; // link to the Message Handler Context mContext; // link to application context DatagramSocket serverSocket; // the UDP socket well receive at public ServerThread(Context currentContext,Handler handler){ mContext = currentContext; mHandler = handler; Open the socket; if successful mHandler.obtainMessage(HelloThreads2.IS_RUNNING, "Server Started").sendToTarget(); InetAddress myIP = getMyWiFiIPAddress(); mHandler.obtainMessage(HelloThreads2.IP_ADDRESS, myIP).sendToTarget(); public void closeSocket() serverSocket.close(); public void run() Allows the socket to be closed (call from OnDestroy()) On Next Page On Next Next Page

8 Getting the IP Address: getMyWiFiIPAddress( ); WifiManager mWifi = (WifiManager) (mContext.getSystemService(Context.WIFI_SERVICE)); WifiInfo info = mWifi.getConnectionInfo(); DhcpInfo dhcp = mWifi.getDhcpInfo(); int myIntegerIPAddress = dhcp.ipAddress; byte[] quads = new byte[4]; for (int k = 0; k < 4; k++) quads[k] = (byte) ((myIntegerIPAddress>> k * 8) & 0xFF); try{ InetAddress myIPAddress = InetAddress.getByAddress(quads); return myIPAddress; }catch(Exception e){ if(D) Log.e(TAG,"Cannot Get My Own IP Address"); return null }

9 ServerThread.run( ) boolean socketOK=true; // True as long as we don't get socket errors while(socketOK) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); try{ serverSocket.receive(receivePacket); /***..... Same as the UDP server ****/ mHandler.obtainMessage(HelloThreads2.PACKET_CAME,sentence). sendToTarget(); } catch (Exception e){ socketOK = false; }// try }// while(socketOK) To the Message Handler in the main Thread Blocks Handles multiple requests as long as the socket is OK

Android Manifest To be able to use the Internet (open sockets) and to read our own IP address (from the WiFi Manager): 10