socket programming with mobile socket client

30
SOCKET PROGRAMMING WITH MOBILE SOCKET CLIENT DEARTMENT OF COMPUTER SCIENCE IOWA STATE UNIVERSITY

Upload: sherry

Post on 24-Feb-2016

76 views

Category:

Documents


0 download

DESCRIPTION

SOCKET PROGRAMMING WITH MOBILE SOCKET CLIENT. DEARTMENT OF COMPUTER SCIENCE IOWA STATE UNIVERSITY. A socket is a common interface for performing network communication. Underneath the hood, Android’s HTTP client library is using sockets to send and receive data. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

SOCKET PROGRAMMING WITH MOBILE SOCKET CLIENT

DEARTMENT OF COMPUTER SCIENCEIOWA STATE UNIVERSITY

Page 2: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

BASICS OF ANDROID SOCKET PROGRAMMING

A socket is a common interface for performing network communication. Underneath the hood, Android’s HTTP client library is using sockets to send and receive data.

Android Sockets are the same as Java Sockets.

Page 3: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

JAVA SOCKET PROGRAMMING

The package java.net provides support for sockets programming (and more).

Classes defined in this package are: InetAddressSocketServerSocketDatagramSocketDatagramPacket

Page 4: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

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: www.yahoo.com:209.131.36.158

Page 5: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

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);

Page 6: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

SocketServer CLASSA 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);

Page 7: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

TCP Socket Client Server Communication

Page 8: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

UDP Sockets

UDP is Connectionless and unreliable service. There isn’t 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 packet’s 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);

Page 9: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

UDP Socket Client Server Communication

Page 10: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

ExampleUDP Client

import java.io.*; import java.net.*;

  class UDPClient {     public static void main(String args[]) throws Exception     {         BufferedReader inFromUser =         new BufferedReader(new InputStreamReader(System.in));         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();

Page 11: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

ExampleUDP 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();       } }

Page 12: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

ExampleUDP Server

import java.io.*; import java.net.*;

  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());

Page 13: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

ExampleUDP 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);

      } }}

Page 14: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

Socket I/O

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

Page 15: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

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();

Page 16: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

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);

Page 17: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

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 file’s 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.

Page 18: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

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 Sockettry {while (true) {new Finder(listener.accept(), clientNumber++).start(); Wait for Client Connection

} and create a new } finally { thread for each clientlistener.close();}

}..

Page 19: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

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); }..

Page 20: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

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 who’s fileExists() function returns 1 if file exists and 0 otherwise

EXAMPLE I JAVA SOCKET SERVER

Page 21: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

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

Page 22: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

EXAMPLE I JAVA SOCKET SERVER

} } 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 SocketHandle Exceptions

Page 23: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

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.

Page 24: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

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 <Project_folder> -> res -> layout.

<ScrollView android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="200dp"> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> </LinearLayout></ScrollView>

Page 25: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

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(R.id.editText1); Get the View by ID and get the final String value = editText.getText().toString(); contents of the fielddisplayMessage("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 socketPrintWriter out = new PrintWriter(socclient.getOutputStream(), true); Output Streamif(value!=null){out.println(value); send the filepath to the server

Page 26: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

EXAMPLE IANDROID SOCKET CLIENT

Message servermsg=Message.obtain();BufferedReader in=new BufferedReader(new InputStreamReader(socclient.getInputStream())); InputStreamString data=in.readLine(); Read response from serverservermsg.obj="Server Response: "+data;handler.sendMessage(servermsg); Use the handler to update UIin.close();} close input and output streams.out.close();} catch (UnknownHostException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace(); handle exceptions}}});

tclient.start();}

Page 27: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

EXAMPLE IANDROID SOCKET CLIENT

public void displayMessage(String msg) the string parameter as a textview item{ into the scrollview of your application

this.scrollView = (ScrollView) findViewById(R.id.scrollView1);this.linearLayout = (LinearLayout) findViewById(R.id.linearLayout2);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());}};

Page 28: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

EXAMPLE IOutput

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.

Page 29: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

EXAMPLE IIOutput

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.

Page 30: SOCKET PROGRAMMING  WITH MOBILE SOCKET CLIENT

THANK YOU!