Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

Networking and Web

Services

Chapter 5
Contents

➔ Networking basics
➔ Determining network status
➔ Communicating with a server socket
➔ Working with web services
1-Networking basics

● Nodes – Each addressed device in a network is known as a node. It can be a PC,


Mainframe, any device.
● Protocols – are predefined and agreed up on set of rules for communication.
Protocols are layered. E.g TCP/IP
● IP – is in charge of the addressing system and delivering packets
● TCP vs UDP – TCP is reliable. UDP is fire and forget.
● Client and Servers : requester and responder.
● Port – Represents particular area of PC memory (0 -65535)
Checking the network status

● Use ConnectivityManager class to determine whether network connectivity exists


and to get notifications of network changes.
Checking the network status

● The short example shows that you can get a handle to the ConnectivityManager
through the context’s getSystemService() method by passing the
CONNECTIVITY_SERVICE constant.
● When you have the manager, you can obtain network information via the
NetworkInfo object.
● The toString() method of the NetworkInfo object returns the output.
Checking the network status

● We can use the isAvailable() or isConnected() method (which returns a boolean


value), or you’ll directly query the NetworkInfo.State using the getState() method.
● NetworkInfo.State is an enum that defines the state of the connection.
● The possible values are CONNECTED, CONNECTING, DISCONNECTED, and
DISCONNECTING.
Communicating with a server socket

● A server socket is a stream that you can read or write raw bytes to, at a specified IP
address and port.
● You can deal with data and not worry about media types, packet sizes, and so on.
Description of the code

● The above java code extends Thread and implements the run method
● Uses ServerSocket to listen for a client connection on a specific port
● Uses a BufferedReader’s readLine() to get input from client, if the input is EXIT it
terminates the Server, otherwise, it sends the input back to the client using
BufferedWriter’s write()
● Run the code separately as a java code. (use cmd )
The Client Side
The Client code description

● Creates a client side Socket with the IP address and Port number of the server
Socket cl=new Socket(ip,port )
● Establishes BufferedReader & BufferedWriter to read data from server & write data
to server.
BufferedWriter wr=new BufferedWriter(new OutputStreamWriter
( cl.getOutputStream() ) );
● Finally, writes EXIT message to server, to end the socket connection
wr.write(“EXIT”,0,5);
Working with web services

● Web services are XML-based information exchange systems that use the
Internet for direct application-to-application interaction.
● They’re a means to call a remote method or operation that’s not tied to a specific
platform/vendor and get a result.
● POX, REST, and SOAP are the most common web services
● POX basically exposes chunks of XML over the wire, usually over HTTP.
● REST uses the concept of resources to define data and then manipulates them with
different HTTP methods using a URL-style
● SOAP is the most formal of them all, imposing strict rules about types of data,
transport mechanisms, and security.
The End

You might also like