Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out

Socket

VIEWS: 10 PAGES: 14

									       Lecture 3


Network programming with java
Networkig java.net
Computers running on the Internet communicate to each other
using Transmission Control Protocol (TCP)
or the User Datagram Protocol (UDP)




You are programming at the application layer.
You don't need to concern yourself with the TCP and UDP layers.
You use the classes in the java.net package.
Server/client program
   Server- a program who performs tasks to
    its clients
   Client- a program who connent to a
    server and ask to get ”thinks” done...
   We use: URL, URLConnection, Socket,
    ServerSocket , RMI
URL
   It is a reference (an address)
    to a resource on the Internet
   Think of a URL as the name
    of a file on the World Wide
    Web, a file on some
    machine on the network
   The protocol identifier
    indicates the name of the
    protocol to be used to fetch
    the resource
   Ex: http, ftp,smtp,
    others
Create URL objects by using
java.net.URL
   Use URL to connect to remote site from applet
    or application
try {
URL url = new URL("http", "hostname", 80, "index.html“ );

or
 url = new URL("http://hostname:80/index.html");
}
catch (MalformedURLException e) { }
Parsing URL’s
   getProtocol
     •   Returns the protocol identifier component of the URL.
   getAuthority
     •   Returns the authority component of the URL.
   getHost
     •   Returns the host name component of the URL.
   getPort
     •   Returns the port number component of the URL. The getPort method returns an integer that
         is the port number. If the port is not set, getPort returns -1.
   getPath
     •   Returns the path component of this URL.
   getQuery
     •   Returns the query component of this URL.
   getFile
     •   Returns the filename component of the URL. The getFile method returns the same as
         getPath, plus the concatenation of the value of getQuery, if any.
   getRef
     •   Returns the reference component of the URL.
Reading files
import java.net.*; import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception
{
   URL url = new URL("http://www.yahoo.com/");
    BufferedReader in = new BufferedReader( new
   InputStreamReader( url.openStream()));
   String inputLine;
   while ((inputLine = in.readLine()) != null) // read as ordinary file
   System.out.println(inputLine);
   in.close();
}
}
Do more by connecting to URLs
URL yahoo = new URL("http://www.yahoo.com/");
  URLConnection yc = yahoo.openConnection();

-   Read from: getInputStream()
-   Write to some: getOutputStream());
-   Set some capabilities to the connection
Socket server/client
   A socket is a software that
    establishes bidirectional
    communication between a
    server program and one or
    more client programs.
   The socket associates the
    server program with a
    specific hardware port on the
    machine.
    Any client program in the
    network with a socket
    associated with that same
    port can communicate with
    the server program.
In practice
//In the server program
ServerSocket server = new ServerSocket(5678);
Socket client = server.accept();

----------------------------------------------------------------------

//In the client program
Socket server = new Socket("localhost" ,5678);
Client/Server

The problems arise because you have a single server
  trying to serve many clients at once.

//In the server program
ServerSocket server = new ServerSocket(5678);
Socket client = server.accept();
handle the client in a separate thread
RMI
Remote method invocation allows
applications to call object
methods located remotely.
A typical client program obtains
a remote reference to one or
more remote objects on a
server and then invokes
methods on them
Such an application is
sometimes referred to as a
distributed object application
Writing an RMI Server


   The server accepts tasks from clients, runs the
    tasks, and returns any results.
   The server code consists of an interface and a class.
   The interface defines the methods that can be
    invoked from the client.
   The interface defines the client's view of the remote
    object. The class provides the implementation.
   Client must know the service the server
    provide ( interface)
   Clients must find the object on the
    server, create the rmiregistry
   Client/ server must pass arguments,
    create the stub and selekton

								
To top