Presentation is loading. Please wait.

Presentation is loading. Please wait.


Similar presentations



2 BASICS OF ANDROID SOCKET PROGRAMMING A socket is a common interface for performing network communication. Underneath the hood, Androids HTTP client library is using sockets to send and receive data. Android Sockets are the same as Java Sockets.

3 JAVA SOCKET PROGRAMMING The package provides support for sockets programming (and more). Classes defined in this package are: InetAddress Socket ServerSocket DatagramSocket DatagramPacket

4 InetAddress CLASS Static methods you can use to create new InetAddress objects. getByName(String host) getAllByName(String host) getLocalHost() Example Use of the class: Code: try { InetAddress a = InetAddress.getByName(hostname); System.out.println(hostname + ":" a.getHostAddress()); } catch (UnknownHostException e) { System.out.println("No address found for " + hostname); } Output:

5 Socket CLASS A Socket class defines active TCP sockets only client sockets socket returned by accept(); Constructor and Methods Constructor: Creates a stream socket and connects it to the specified port number on the named host. Following are different constructors: Socket(InetAddress server, int port); Socket(InetAddress server, int port,InetAddress local, int localport); Socket(String hostname, int port); close() :Closes the Socket connection and releases all associated resources. Thus a Client Socket is created as follows: Socket socclient=new Socket(server_ip_address,server_port_number);

6 SocketServer CLASS A ServerSocket class defines a Server Socket. Passive sockets are supported by this class. Constructor and Methods: Constructor: Creates a server socket and binds it to the specified local port number, with the specified backlog. The following are different constructors: ServerSocket(int port); ServerSocket(int port, int backlog); ServerSocket(int port, int backlog, InetAddress bindAddr); Socket Accept(): Listens for a connection to be made to this socket and accepts it. This method blocks until a connection is made. This is a method of the SocketServer Class however the object instance returned by this method is of the Socket class. close(): Closes the Socket connection and releases all associated resources. A Server Socket can be created as follows: ServerSocket server_sock = new ServerSocket(server_port_number);

7 TCP Socket Client Server Communication

8 UDP Sockets UDP is Connectionless and unreliable service. There isnt an initial handshaking phase. Transmitted data may be received out of order, or lost. Socket Programming with UDP No need for a welcoming socket. No streams are attached to the sockets. The sending hosts creates packets by attaching the IP destination address and port number to each batch of bytes and the receiving process must unravel to received packet to obtain the packets information bytes. DatagramSocket class is used to create a UDP socket. DatagramSocket(); DatagramSocket(int port); DatagramSocket(int port, InetAddress a); DatagramPacket class needed to specify the payload. DatagramPacket( byte[] buf, int len, InetAddress a, int port);

9 UDP Socket Client Server Communication

10 Example UDP Client import*; import*; class UDPClient { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(; DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("hostname"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine(); sendData = sentence.getBytes();

11 Example UDP Client DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } }

12 Example UDP Server import*; import*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); String sentence = new String(receivePacket.getData());

13 Example UDP Server InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); }

14 Socket I/O Socket I/O is based on the Java I/O support in the package Once a Socket is created data can be transmitted using Inputstreams and Outputstreams. InputStream and OutputStream are abstract classes.

15 InputStream An InputStream is a stream of incoming byte data. An InputStream can be obtained from a Socket by using the getInputStream() method. In order to read from a stream, you must create a byte buffer to read in data.Each call to read on an InputStream fills your buffer with data and returns the number of bytes read. Example use of InputStream: BufferedReader in=new BufferedReader(new InputStreamReader(socclient.getInputStream())); String data=in.readLine();

16 OutputStream An OutputStream is a stream of outgoing byte data An OutputStream can be obtained from a Socket by using the getOutputStream() method. You can write data to a stream by passing in a byte buffer of data. You should use the flush() method if you want to make sure that the data you have written has been output to disk or sent to the other end of the socket. Eg: PrintWriter out = new PrintWriter(socclient.getOutputStream(), true);

17 SCENARIO Consider a Java Socket Server and a Mobile Application Client. Consider the Mobile Application to run an Android OS. Let us look at some very simple examples to create an Android Application that works as a socket client. Example 1: Consider A simple Socket Server which receives a text files name with path from the Socket client and replies saying weather the file exists in the given path or not. If the file exists, it retrieves the contents of the text file. Example 2: Consider A simple Socket server which receives a string and replies with the count of the number of occurrences of the letter s in the string. The Complete Code of these examples is available as part of the module.

18 EXAMPLE I JAVA SOCKET SERVER Implement a Java Socket Server irrespective of the android nature of the client. The Following code snippet creats a ServerSocket and calls the accept() blocking call to wait for connection. public class SockServer { public static void main(String[] args) throws Exception { System.out.println( The File Retrieval server is running."); int clientNumber = 0; ServerSocket listener = new ServerSocket(9898); Create Socket try { while (true) { new Finder(listener.accept(), clientNumber++).start(); Wait for Client Connection } and create a new } finally { thread for each client listener.close(); }..

19 EXAMPLE I JAVA SOCKET SERVER The following code snippet defines the Finder Class that stores the information about Each Socket Client connection... private static class Finder extends Thread {Extends Thread Class private Socket socket; private int clientNumber; public Finder(Socket socket, int clientNumber) { this.socket = socket; this.clientNumber = clientNumber; log("New connection with client# " + clientNumber + " at " + socket); }..

20 public void run() { try { BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); while (true) { String input = in.readLine(); if (input == null || input.equals(".")) { break; } CheckforFile cf = new CheckforFile(); int exists = cf.fileExists(input); Input Stream Output Stream Get file path from Socket Instantiate CheckforFile object whos fileExists() function returns 1 if file exists and 0 otherwise EXAMPLE I JAVA SOCKET SERVER

21 File dir = new File(input) if(exists==1) { System.out.println("The file exists"); BufferedReader br = new BufferedReader(new FileReader(dir)); try { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } String everything = sb.toString(); out.println("Hello, client #" + clientNumber + ". The file "+input+" exists"+"The contents of the file are:"+everything); } finally { br.close(); } else { out.println("The file "+input+" does not exist"); System.out.println("The file does not exist"); } Send data to Client which specifies if the file exits, the contents of the file. Send data to client specifying that the file does not exist. EXAMPLE I JAVA SOCKET SERVER

22 } } catch (IOException e) { log("Error handling client# " + clientNumber + ": " + e); } finally { try { socket.close(); } catch (IOException e) { log("Couldn't close a socket, what's going on?"); } log("Connection with client# " + clientNumber + " closed"); } private void log(String message) { System.out.println(message); } Handle Exceptions Close Socket Handle Exceptions

23 EXAMPLE I ANDROID SOCKET CLIENT The Android Client is a simple Java Client with additional handlers for updating the User Interface. In Android we use a PrintWriter to output data to the socket server because it writes characters as opposed to a PrintStream which writes bytes. Use a Handler to communicate with the UI thread. The Android User Interface is updated using a UI thread and hence the Socket thread which is a Background thread requires a Handler object to update the UI. AsyncTask can be used instead of Threads as they require lesser maintenance. The basic example given however uses Threads for simplicity.

24 EXAMPLE I ANDROID SOCKET CLIENT Create an Android Application with a Blank Activity. If you are new to Android Programming refer to the Modules on Basic Android Programming. Layout: Create a Simple Layout with a TextField reading Text File Path, an EditText field that takes the path input and a Button Find. Add a ScrollView to the Layout by adding the following code to activity_main.xml under -> res -> layout.

25 EXAMPLE I ANDROID SOCKET CLIENT Add a button on click Method inside the Main Activity Class of the Application. public void searchFile(View v) { this.editText= (EditText)findViewById(; Get the View by ID and get the final String value = editText.getText().toString(); contents of the field displayMessage("Searching for file: "+value);this method updates the UI tclient=new Thread(new Runnable(){ public void run() {create a thread try { displayMessage("Socket being Created.. "); socclient=new Socket(B); create socket PrintWriter out = new PrintWriter(socclient.getOutputStream(), true); Output Stream if(value!=null) { out.println(value);send the filepath to the server

26 EXAMPLE I ANDROID SOCKET CLIENT Message servermsg=Message.obtain(); BufferedReader in=new BufferedReader(new InputStreamReader(socclient.getInputStream())); InputStream String data=in.readLine();Read response from server servermsg.obj="Server Response: "+data; handler.sendMessage(servermsg); Use the handler to update UI in.close(); }close input and output streams. out.close(); } catch (UnknownHostException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace();handle exceptions } }); tclient.start(); }

27 EXAMPLE I ANDROID SOCKET CLIENT public void displayMessage(String msg) the string parameter as a textview item {into the scrollview of your application this.scrollView = (ScrollView) findViewById(; this.linearLayout = (LinearLayout) findViewById(; TextView tx = new TextView(this); tx.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); tx.setText(msg); this.linearLayout.addView(tx); } Handler handler=new Handler() { public void handleMessage(Message msg)override the handMessage method { displayMessage(msg.obj.toString()); } };

28 EXAMPLE I Output This is a Screenshot of the Application running in the Android Device with the response from the server containing the contents of the text file.

29 EXAMPLE II Output Screenshot of the Application running in the Android Device with the response from the server containing the number of occurrences of the letter s in the entered string.



Similar presentations

Ads by Google