Client Server Example in Java - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Client Server Example in Java

Description:

Client Server Example in Java – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 12
Provided by: bobba9
Category:
Tags: client | example | java | server | zap

less

Transcript and Presenter's Notes

Title: Client Server Example in Java


1
Client Server Example in Java
  • The next 11 slides highlight the steps needed to
    have one server wait for one client application
  • Five steps required on the server slide
  • Four steps required on the client slide
  • They also show example code from
  • AccountServer.java (adds the try catch)
  • AccountClient.java (adds the try catch)

2
Server Using Sockets
  • Five steps
  • 1. Create a ServerSocket
  • int port 4000 // Only port available in
    our labs!
  • ServerSocket socket null
  • socket new ServerSocket(port)
  • Establishes the port where the server waits for
    connections from clients

3
Server Using Sockets
  • 2. Wait for a connection from the client with a
    Socket
  • Socket connection socket.accept()
  • This waits for a connection
  • A Connection is the server's way to communicate
    with the client, which can be done using
    ObjectInputStream and ObjectOutputStream

4
Server Using Sockets
  • 3. Get two IO streams to allow communication
    with the client and the server
  • These two stream objects allow the server to
    write objects to and read objects from the client
  • ObjectOutputStream output
  • new ObjectOutputStream(connection.getOutput
    Stream())
  • ObjectInputStream input
  • new ObjectInputStream(connection.getInputS
    tream())
  • Now a client can read the servers output (with
    input) and write to the server's input (with
    output)
  • Assuming the client also has IO streams

5
Server Using Sockets
  • 4. Let the client and server communicate (silly
    example)
  • BankAccount theClientsAccount null
  • BankAccount theServersAccount new
    BankAccount("Greedy", 0.00)
  • while (true)
  • double amount ((Double) input.readObject()).d
    oubleValue()
  • if (amount lt 0.0)
  • break
  • theClientsAccount (BankAccount)
    input.readObject()
  • if (theClientsAccount.withdraw(amount))
  • theServersAccount.deposit(amount)
  • System.out.println(theClientsAccount.toString()
    " "
  • theServersAccount.toString()
    )
  • output.writeObject(theClientsAccount)
  • Take money from the client, but leave at least 100

6
Server Using Sockets
  • 5. Shut down the connection
  • connection.close()
  • Any object that is read from or written to a
    socket must implement serializable
  • String already does this
  • To BankAccount, add this
  • public class BankAccount implements
    ComparableltBankAccountgt, Serializable

7
The Client
  • Four Steps
  • 1. Create a socket to connect to the server
  • String IPAddress "localhost" // The
    machine you're on
  • int port 4000
  • Socket server new Socket(IPAddress, port)

8
The Client
  • 2. Get references to the Server's IO streams
  • ObjectOutputStream output new
  • ObjectOutputStream(server.getOutputStrea
    m())
  • ObjectInputStream input new
    ObjectInputStream(server.getInputStream())

9
The Client
  • 3. Do the processing
  • BankAccount myAccount new BankAccount("Sucker",
    5000.00)
  • while (true)
  • String amountAsString JOptionPane.showInputDi
    alog(null,
  • "You've won! Enter desired amount" "
    you have "

  • myAccount.getBalance())
  • double amount Double.parseDouble(amountAsStri
    ng)
  • // First write a double to the server
  • output.writeObject(new Double(amount))
  • if (amount gt 0)
  • // First write the BankAccount to the server
  • output.writeObject(myAccount)
  • // Read the BankAccount back from the server
    (with less money)
  • myAccount (BankAccount) input.readObject()
  • else
  • break
  • Writes an amount an an account to the server

10
The Client
  • 4. Close the connection
  • server.close()
  • This example wrote objects with ObjectOutStream
  • If you only need to pass strings around (as in
    BattleBoat perhaps), you can also readLine with a
    BufferedReader and write strings with PrintWriter
  • This is like reading text from a file and writing
    text to a file

11
Summary
  • Networking is made much easier with Javas
    extensive API that hides a lot of networking
    details
  • Networking is integrated with input and output
    both use streams.
  • Decorators make your life much easier, you can
    write and read serialized objects (also encrypt
    and/or zip)
  • You can read and write strings with
    BufferedReader and PrintWriter
Write a Comment
User Comments (0)
About PowerShow.com