Title: Advanced Java free demo( from India)low price-cost less..
1(No Transcript)
2(No Transcript)
3Advanced Java Introducing From RVH Technologies
4Networking
- Using the networking capabilities provided in the
Java environment is quite easy - We will see how to use Sockets
5What 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.
6How 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
7Connection 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
8How 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
9Establish 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
10Need 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)
11Simple 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
12Close up Shop on Client side
out.close( ) in.close( ) stdIn.close(
) echoSocket.close( )
13Server
- 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
14If a socket is a pipe
- We could conceptualize this like so
Ports
Client
Server
The things flowing through the Plumbing
The Socket Plumbing
15Objects 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
16The 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()
17The 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 )
18File 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()
19The 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
20More 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)
21Retrieving 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()
22Getting 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
23An 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)