DOC

Tutorial 2 Network Programming in Java

By Jean Nelson,2014-04-24 12:30
14 views 0
Tutorial 2 Network Programming in Java

Tutorial 2 Network Programming in Java

    Tutorial 2 Network Programming in Java

Objectives

?Java network programming introduction

    ?

    ?HTTP introduction

    ?

    ?Java URL class for establishing HTTP connection

     1

Tutorial 2 Network Programming in Java

    1 Java network programming introduction

Java provides java.net package to support network

    programming.

1.1 Client and Server

    ? A client obtains a service via sending a request to a

    server

    ? A client initiates a connection, retrieves data, responds

    user input. For example, web browser, chat program

    (ICQ)

    ? A server provides a set of services, such as web server,

    time server, file server, chat server.

    ? A server responds to connection, receives requests for

    data from client, and delivers it to client.

    ? The protocol between client and server is the

    communication rule, for example FTP, SMTP, and

    HTTP.

     2

Tutorial 2 Network Programming in Java

    1.2 Socket

? We use socket to establish the connection between client

    and server.

    ? Socket identifies a connection using host address and port

    number.

? A socket is a bi-directional communication channel

? Java differentiates client sockets from server sockets.

    e.g. for client

     Socket client=new Socket(“hostname”,portNumber);

     for server

     ServerSocket server=new SeverSocket(portNumber);

?

    Client

    port 13 Time

    Service

    Web port 80

    Service

    Server

     3

Tutorial 2 Network Programming in Java

    ? Well known ports for server

    80 web server

    21 Ftp server

    13 Time server

    23 Telnet

    25 Email(SMTP)

1.3 The connection between server and client using

    socket

Socket Operations at Client Side

    ? create a client socket:

     Socket (host, port)

     s = new Socket (“java.sun.com”, 13)

? get input / output data streams out of the socket:

     in = new DataInputStream(s.getInputStream ());

     out = new DataOutputStream( s.getOutputStream());

     out = new PrintStream( s.getOutputStream());

    ? read from input / write to output data streams:

     String str = in.readLine();

     out.println ( “Echo:” + str + “\r”);

    ? close the socket:

     s.close();

     4

Tutorial 2 Network Programming in Java

    Socket Operations at Server Side

A server is always waiting for being connected. It

    need not initiate a connection to a host. So a server

    socket need only specify its own port no.

    ? create a server socket:

     ServerSocket (port)

     ServerSocket s = new ServerSocket(8189);

    ? accept an incoming connection:

     Socket snew = s.accept (); ? get input / output data streams out of the socket for

    the incoming client:

    in = new DataInputStream(snew.getInputStream());

     out = new PrintStream(snew.getOutputStream()); ? close the socket for the incoming client:

     snew.close();

1.4 Example : A web Client

import java.net.*;

    import java.io.*;

class HttpClient {

     public static void main(String[] args ) {

     try {

    Tutorial for CS5286 Algorithms&Tech for Web Search 5

Tutorial 2 Network Programming in Java

     Socket s = new

    Socket("www.cs.cityu.edu.hk", 80);

     DataInputStream in = new DataInputStream(s.getInputStream());

     PrintStream out=new

    PrintStream(s.getOutputStream());

     out.println("GET / HTTP/1.0");

     out.println();

     String line;

     while ((line=in.readLine())!=null)

     System.out.println("> "+line);

     s.close();

     } catch (Exception e)

    { System.out.println(e);}

     }

    }

2 HTTP introduction

    HTTP( Hypertext Transfer Protocol) is an application layer protocol , just like FTP, SMTP, etc. The WWW is based on HTTP, for the communication rule between the browser and web server is HTTP.

    You can check following URL for detailed document about HTTP . http://www.w3.org/Protocols/

    Tutorial for CS5286 Algorithms&Tech for Web Search 6

Tutorial 2 Network Programming in Java

2.1 Overview

HTTP request

Format: Method URI HTTP Version

For example: GET / HTTP/1.0

There are three most important methods in HTTP 1.0 ,

    include GET, PUT and HEAD

? To request a web page, a browser sends a web server an

    HTTP GET message

? To send data to a web server, a browser can use an

    HTTP POST message

? To get information about document size , modification

    date, but not document itself , you can use HTTP HEAD

    request.

And there are some other HTTP methods added in HTTP

    1.1 , such as PUT, DELETE, OPTIONS, and TRACE.

Also client can send additional information about itself to

    web server, such as browser type, operation system.

HTTP response

    Tutorial for CS5286 Algorithms&Tech for Web Search 7

    Tutorial 2 Network Programming in Java

    After receiving client’s request, web server will

    respond. It includes web server HTTP version, HTTP

    server status code for client’s request, and some

    information about document, such as Content-Length,

    Content-Type, Last-Modified date.

    2. browser sends 3. server fetches GET URL to 1. user types or web page for URL server clicks URL

    4. server sends

    web page data to

    browser 5. browser

    displays web

    page*

    internet

    Tutorial for CS5286 Algorithms&Tech for Web Search 8

Tutorial 2 Network Programming in Java

    2.2 Example

import java.net.*;

    import java.io.*;

class HttpClient1 {

     public static void main(String[] args ) {

     try {

     Socket s = new Socket("www.cs.cityu.edu.hk", 80);

     DataInputStream in = new

    DataInputStream(s.getInputStream());

    PrintStream out=new PrintStream(s.getOutputStream());

     out.println("HEAD /index.html HTTP/1.0");

     out.println();

     String line;

     while ((line=in.readLine())!=null)

     System.out.println("> "+line);

     s.close();

     } catch (Exception e) { System.out.println(e);}

     }

    }

    Tutorial for CS5286 Algorithms&Tech for Web Search 9

Tutorial 2 Network Programming in Java

    2.3 URL: Uniform Resource Locator

    ? The URL standard defines a common way to refer to

    any web page, anywhere on the web

    ? format: protocol://web_server_name/page_name

     example: http://www.yahoo.com/index.html

    ? URLs are most commonly used in conjunction with

    the HTTP protocol to GET a URL, or POST data to a

    URL

    ? a URL can refer to many kinds of web page:

    plain text, formatted text (usually in HTML…),

    multimedia, database, ...

    2.4 HTML: HypeText Markup Language

Specific mark-up language used in the Web

Many kinds of markups

low-level appearance (font changes, lists)

    logical structure (title, headings)

    links to other documents

    embedded graphics

    meta-information (language)

    and much more...

    Tutorial for CS5286 Algorithms&Tech for Web Search 10

Report this document

For any questions or suggestions please email
cust-service@docsford.com