Advanced Java free demo( from India)low price-cost less.. - PowerPoint PPT Presentation

About This Presentation
Title:

Advanced Java free demo( from India)low price-cost less..

Description:

RVH Technologies is a Brand of Online trainings… Honest,Dedication,Hard work..Is the secret of success for our Institute…. Believe us ,Join Us..We will make You Experts…. We are concentrating mainly in Online Trainings.... All the courses are conducted in the latest versions. We will Provide the online training based on the User Requirement (This May be Full fledged Couse,Some Modules of the course based on the User Need) Please Request for a FREE DEMO,Check the Out the standards, Then Choose the best Training Center. We are 100% sure ,you will reach to us after the demo class……… For Further Queries Please contact us on 91 8790137293 Email:info@rvhtech.com Web:www.rvhtech.com Exclusive Offer: If you come up with one more referral,You will get the discount of 20%, If it is two referrals ..you will get discount of 30% And more than that you will get 40% discount. – PowerPoint PPT presentation

Number of Views:34

less

Transcript and Presenter's Notes

Title: Advanced Java free demo( from India)low price-cost less..


1
(No Transcript)
2
(No Transcript)
3
Advanced Java Introducing From RVH Technologies
4
Networking
  • Using the networking capabilities provided in the
    Java environment is quite easy
  • We will see how to use Sockets

5
What Is a Socket?
  • A socket is one endpoint of a two-way
    communication link between two programs
    running on the network.
  • A socket is bound to a port number so that the
    TCP layer can identify the application that data
    is destined to be sent.

6
How do Sockets work?
  • A server runs on a specific computer and has a
    socket that is bound to a specific port number.
  • Client knows the hostname and port of server and
    tries to make a connection request

7
Connection established
  • If the server accepts the connection it gets a
    new socket bound to a different port.
  • It needs a new socket (and consequently a
    different port number) so that it can continue to
    listen to the original socket

8
How does Java support Sockets
  • The java.net package provides a class, Socket,
    that implements one side of a two-way connection
    between your Java program and another program on
    the network
  • It also includes the ServerSocket class, which
    implements a socket that servers can use to
    listen for and accept connections to client

9
Establish the Socket connection

Output
Port
Host
try echoSocket new Socket(avatar ",
7777) out new PrintWriter(echoSocket.getOutp
utStream(), true) in new
BufferedReader(new
InputStreamReader(echoSocket.getInputStream()))
catch
Input
10
Need to Catch Exceptions
catch (UnknownHostException e)
System.err.println("Don't know about host
avatar.") System.exit(1) catch
(IOException e) System.err.println("Couldn
't get I/O for "
"the connection to avatar.")
System.exit(1)
11
Simple Socket Example
Set up a mechanism to read from standard input
BufferedReader stdIn new
BufferedReader(
new InputStreamReader(System.in))
String userInput
while ((userInput stdIn.readLine()) ! null)
out.println(userInput)
System.out.println("echo "
in.readLine())
Read from standard input
Write to Server
Output whats read back from Server
12
Close up Shop on Client side
out.close( ) in.close( ) stdIn.close(
) echoSocket.close( )
13
Server
  • A server must open a SeverSocket
  • ServerSocket server new ServerSocket( 7777 )
  • Call accept on that socket creating a new socket
  • Socket socket server.accept()
  • Socket acts as socket from client

14
If a socket is a pipe
  • We could conceptualize this like so

Ports
Client
Server
The things flowing through the Plumbing
The Socket Plumbing
15
Objects flow through the Pipe
  • Let first address the case where we want to have
    objects flowing over the pipe
  • Must have at least the following mechanisms for
  • Objects to be written by the server
  • Objects to be read by the client

16
The new protocol Client
public class Client Socket socket new
Socket( "127.0.0.1", 9999 ) //
ObjectInputStream input new
ObjectInputStream(socket.getInputStream() )
// read using serialization NewProtocol
protocol (NewProtocol)(input.readObject() )
System.out.println(Protocol protocol)
socket.close()
17
The new protocol Server
class ThreadedSocket extends Thread // here is
where all the real work is done. private Socket
socket ThreadedSocket( Socket socket )
this.socket socket //
ObjectOutputStream output new
ObjectOutputStream(socket.getOutputStream() )
output.writeObject( protocol )
18
File example
  • For example to write an object that can be read
    by the example in ObjectInputStream

FileOutputStream ostream new FileOutputStream(f
oo.bar") ObjectOutputStream p new
ObjectOutputStream(ostream) p.writeInt(12345) p.
writeObject("Today") p.writeObject(new
Date()) p.flush() ostream.close()
19
The Needed Java Framework
  • Only objects that support the java.io.Serializable
    interface can be written to streams.
  • The class of each serializable object is encoded
    including the class name and signature of the
    class, the values of the object's fields and
    arrays, and the closure of any other objects
    referenced from the initial objects
  • This relates to introspection/reflection which we
    will discuss shortly

20
More about the Framework
  • The default deserialization mechanism for objects
    restores the contents of each field to the value
    and type it had when it was written.
  • Marshalling of Objects (Serialize)
  • Un marshaling of Object (Serialize)

21
Retrieving Class Objects
  • You can retrieve a Class object in several ways
  • Class c foo.getClass() // for some object
    named foo
  • Bar b new Bar()
  • Class c b.getClass()
  • Class s c.getSuperclass()

22
Getting the Class Name
  • Every class in the Java programming language has
    a name. When you declare a class, the name
    immediately follows the class keyword
  • At runtime, you can determine the name of a Class
    object by invoking the getName method. The String
    returned by getName is the fully-qualified name
    of the class.
  • A good home study question Given an instance
    prints the names of the classes its inheritance
    hierarchy from least specific to most specific
    excluding Object

23
An Example
import java.lang.reflect. import java.awt.
class SampleName public static
void main(String args) Button
b new Button() printName(b)
static void
printName(Object o) Class c
o.getClass() String s
c.getName() System.out.println(s
)
Need Reflection Package To Do this
24
(No Transcript)
25
(No Transcript)
26
(No Transcript)
27
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com