JAVA Tutorial - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

JAVA Tutorial

Description:

http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html ... java.net.ServerSocket ... Sends the names of clients to chat with to the server. ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 26
Provided by: afran
Category:
Tags: java | chat | download | java | sun | tutorial

less

Transcript and Presenter's Notes

Title: JAVA Tutorial


1
JAVA Tutorial for CSE 5344
Rohita Mohan Feb 9, 2007 Department of Computer
Science and EngineeringThe University of Texas
at Arlington
The materials are modified from Wei Zhang
Shufeng Zhous ppt slide
2
Outline
  • Getting Started
  • Java Socket Programming
  • Java Multithreads Programming
  • Program Assignment 1

3
Getting Started
  • JDK 5.0 download
  • http//java.sun.com/j2se/1.5.0/download.jsp
  • Update the PATH variable
  • Test
  • javac helloWorld.java // ? helloWorld.class
  • java helloWorld
  • Reference
  • http//java.sun.com/docs/books/tutorial/getStarte
    d/cupojava/win32.html

4
Socket Programming
  • What is Socket
  • A socket is a communication endpoint of a two-way
    communication link an object through which an
    application sends or receives packets of data
    across a network.
  • The purpose is to abstract away the underlining
    network.

5
What is Socket?
  • A server has a socket that is bound to a
    specific port number and waits for a connection
    request from a client.
  • A client makes a connection request based on the
    host name of the server and the port number.

6
What is Socket? (contd)
  • Upon acceptance, the server gets a new socket
    bound to the port.
  • If the connection is accepted, a socket is
    successfully created and the client can use the
    socket to communicate with the server.

7
Sequence of Operations for a TCP Socket
Communication
  • Server
  • Create a server socket
  • Start listening on a port
  • Create a new socket
  • Accept connection
  • Sending Receiving
  • Client
  • Create the socket
  • Seek a connection
  • Sending Receiving

8
Socket Programming--TCP
  • Client side
  • java.net.Socket
  • Specifies the remote hostname and port number
  • Socket (String host, int port)
  • Socket (InetAddress address, int port)
  • Server side
  • java.net.ServerSocket
  • A server socket waits for accept connections
    to come in over the network.
  • SeverSocket (int port, int count)
  • Accept a connection
  • Socket accept() // waits for a connection
    request,
  • // returns a new socket object

9
Socket Methods
  • InetAddress getInetAddress()
  • // Returns the IP address of the remote host
  • int getPort()
  • // Returns the port number of the remote host
  • InetAddress getLocalAddress()
  • // Return the local address to which the socket
    is connected
  • int getLocalPort()
  • // return local port number
  • void close()
  • //close the socket

10
How do I open a socket?
  • Open a socket like this Socket
    MyClient MyClient new Socket("Machine name",
    PortNumber)
  • It is a good idea to handle exceptions. (From
    now on, all our code will handle exceptions!) The
    above can be written as
  • Socket MyClient try            MyClient
    new Socket("Machine name", PortNumber) catch
    (IOException e)      System.out.println(e)
  • Programming a server ServerSocket
    MyService try    MyServerice new
    ServerSocket(PortNumber)                  catc
    h (IOException e)            System.out.println
    (e)         

11
Socket I/O Stream
  • Abstract classes
  • InputStream
  • OutputStream
  • Methods for creation of input and output streams
  • InputStream getInputStream()
  • // receive data from the network
  • OutputStream getOutputStream()
  • // sends data to the network
  • DataInputStream is a subclass of
    FilterInputStream, which is a subclass of
    InputStream. It has methods such as read,
    readChar, readInt, readDouble, and readLine.

12
How do I create an input stream?
  • On the client side, you can use the
    DataInputStream class to create an input stream
    to receive response from the server
    DataInputStream input try    input new
    DataInputStream(MyClient.getInputStream())
    catch (IOException e)    System.out.println(
    e)
  • On the server side, you can use DataInputStream
    to receive input from the client
    DataInputStream input try       input
    new DataInputStream(serviceSocket.getInputStream
    ()) catch (IOException e)   
    System.out.println(e)

13
Client Side Example
  • import java.io.
  • import java.net.
  • public class EchoClient
  • public static void main(String args) throws
    IOException Socket echoSocket null
  • PrintWriter out null
  • BufferedReader in null
  • try
  • echoSocket new Socket("taranis", 7)
  • out new PrintWriter(echoSocket.getOutputStream
    (), true)
  • in new BufferedReader(new InputStreamReader(
    echoSocket.getInputStream()))
  • catch (UnknownHostException e)
  • System.err.println("Don't know about host
    taranis.") System.exit(1)
  • catch (IOException e)
  • System.err.println("Couldn't get I/O for "
    "the connection to taranis.")
  • System.exit(1)

14
Example (Cont.)
  • BufferedReader stdIn new BufferedReader( new
    InputStreamReader(System.in))
  • String userInput while ((userInput
    stdIn.readLine()) ! null)
  • out.println(userInput)
  • System.out.println("echo " in.readLine())
  • out.close()
  • in.close()
  • stdIn.close()
  • echoSocket.close()

15
Basic Steps for a Client Program
  • Open a socket.
  • Open an input stream and output stream to the
    socket.
  • Read from and write to the stream according to
    the servers protocol.
  • Close the streams.
  • Close the sockets.

16
Server Side Example
  • import java.net.
  • class SimpleTCPServer
  • public static void main(String a) throws
    Exception
  • ServerSocket acceptor new ServerSocket(7)
  • System.out.println("On port "
    acceptor.getLocalPort())
  • while (true)
  • Socket client acceptor.accept()
  • // Do stuff here...
  • client.close()

17
Java Multithreads
Figure. Life Cycle of a Thread
  • A thread is a single sequential flow of control
    within a program.

18
Multithreads (Cont.)
  • Create a thread
  • Thread()
  • Start a thread
  • Void start()
  • Void run()
  • In Java, you can either extends Thread Class or
    implements Runnable Interface. You can (and
    should) add your code by overriding run() method.

19
Multithread example
  • class PrimeThread extends Thread
  • long minPrime
  • PrimeThread(long minPrime)
  • this.minPrime minPrime
  • public void run()
  • // compute primes larger than minPrime  . . .
  • PrimeThread p new PrimeThread(143)
  • p.start()

20
Chat ProgramProgram 1

21
Program 1(Cont.)

Chat Client GUI
22
Program 1 (Cont. )
  • Server
  • Creates a Jabber Thread that uses UDP socket to
    receive messages from clients
  • Main thread waits for client connections on
    TCP Socket
  • Each client accepted on separate TCP socket
  • Sends UDP port number and list of online clients.
  • Main Thread goes back to waiting for new clients
  • Jabber Thread reads messages from clients from
    UDP socket and sends them to appropriate clients
    over their respective TCP sockets

23
Program 1 (Cont. )
  • Client
  • Creates a TCP connection socket with the server.
  • Receives the list of online clients from the
    server.
  • Sends the names of clients to chat with to the
    server.
  • Provides a GUI displaying the online clients,
    messages received from clients.
  • Sends the message typed in by the user over UDP
    to the server according to the protocol defined.
  • Continuously displays TCP socket messages from
    the server.
  • Continuously updates online clients in the GUI.
  • Terminates if and when the server terminates.

24
Reference
  • Socket programming
  • http//java.sun.com/docs/books/tutorial/networkin
    g/
  • Multithreads Programming
  • http//java.sun.com/docs/books/tutorial/essential
    /threads/

25
TA Office Hours
  • Tuesday Thursday
  • 230 PM 300 PM
  • EOB-W 117
  • rmohan_at_cse.uta.edu
Write a Comment
User Comments (0)
About PowerShow.com