networking with javan-1 outline client-server example steps required on the server side steps...

11
Networking with Java N-1 Outline Client-Server Example Steps required on the server side Steps required on the client side

Upload: dustin-dawson

Post on 18-Jan-2018

219 views

Category:

Documents


0 download

DESCRIPTION

Networking with JavaN-3 Server Using Sockets Wait for a connection from the client with a Socket Socket client = socket.accept(); Socket client = socket.accept(); This waits for a connection No further statements are executed in the server until a client connects (shown later) client is the server's way to communicate with the client using ObjectInputStream and ObjectOutputStream

TRANSCRIPT

Page 1: Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side

Networking with Java N-1

Outline• Client-Server Example

Steps required on the server sideSteps required on the client side

Page 2: Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side

Networking with Java N-2

Server Using Sockets

Create a ServerSocket // 4000 is the only port available in our labs!// 4000 is the only port available in our labs! ServerSocket socket = new ServerSocket(4000);ServerSocket socket = new ServerSocket(4000);

• Establishes the port where the server waits for connections from clients

Page 3: Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side

Networking with Java N-3

Server Using Sockets

Wait for a connection from the client with a Socket Socket client = socket.accept();Socket client = socket.accept();

• This waits for a connection • No further statements are executed in the server until

a client connects (shown later)• client is the server's way to communicate with the

client using ObjectInputStream and ObjectOutputStream

Page 4: Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side

Networking with Java N-4

Writing using SocketsGet two IO streams to allow communication with the client and the server

Server can write objects to the client and read them ObjectOutputStream outputToClient = ObjectOutputStream outputToClient = new ObjectOutputStream(client.getOutputStream());new ObjectOutputStream(client.getOutputStream()); ObjectInputStream inputFromClient =ObjectInputStream inputFromClient =

new ObjectInputStream(client.getInputStream()); new ObjectInputStream(client.getInputStream()); Later, a client will be able to read this server's output when it write an object to the client with outputToClient.writeObject(new MyClass());outputToClient.writeObject(new MyClass());

and the client can write to this server that reads withMyClass c = (MyClass)inputFromClient.readObject();MyClass c = (MyClass)inputFromClient.readObject();

Assuming the client also has IO streams that is …

Page 5: Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side

Networking with Java N-5

The ClientIn a separate program, create a socket to connect to the

server

String IPAddress = "localhost";String IPAddress = "localhost"; int port = 4000;int port = 4000; Socket server = new Socket(IPAddress, port);Socket server = new Socket(IPAddress, port);

Page 6: Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side

Networking with Java N-6

The Client

Get references to the Server's IO streams of the server with which this client can now read from and write to

ObjectOutputStream outputToServer = new ObjectOutputStream outputToServer = new ObjectOutputStream(server.getOutputStream());ObjectOutputStream(server.getOutputStream());

ObjectInputStream inputFromServer = new ObjectInputStream inputFromServer = new ObjectInputStream(server.getInputStream());ObjectInputStream(server.getInputStream());

Page 7: Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side

Do some Input

MyClass c = (MyClass) inputFromServer.readObject();MyClass c = (MyClass) inputFromServer.readObject();System.out.println(c.toString());System.out.println(c.toString());

Object read and written objects must be Serializable

public class MyClass implements Serializable {public class MyClass implements Serializable { public String toString() {public String toString() { return "This could be any of your types.";return "This could be any of your types."; }}}}

Networking with Java N-7

Page 8: Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side

Networking with Java N-8

Use Object IO streams

• This example has one simple readObject with a writeObject from the other program

• The next example code could replace that simple read and write with loops

• This allows communication until some event occurs to terminate the looping (like a BattleBoat win or loss)

• The next example shows how a server can present the illusion of taking money form a client

Page 9: Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side

Networking with Java N-9

Server with a loop replace simple write with loopBankAccount theClientsAccount = null;BankAccount theClientsAccount = null;// theClientsAccount will be read from client after connection to this server// theClientsAccount will be read from client after connection to this serverBankAccount theClientsAccount = null; BankAccount theClientsAccount = null; // This server's account will deposit the money withdrawn from the clients// This server's account will deposit the money withdrawn from the clients// account and then writes back the modified clients account.// account and then writes back the modified clients account.BankAccount theServersAccount = new BankAccount("Greedy", 0.00);BankAccount theServersAccount = new BankAccount("Greedy", 0.00); // Continue as long as the client sends a positive amount (as a double)// Continue as long as the client sends a positive amount (as a double)while (true) {while (true) { double amount = ((Double) inputFromClient.readObject()).doubleValue();double amount = ((Double) inputFromClient.readObject()).doubleValue(); if (amount <= 0.0)if (amount <= 0.0) break;break; // read the client's account// read the client's account theClientsAccount = (BankAccount) inputFromClient.readObject();theClientsAccount = (BankAccount) inputFromClient.readObject(); // "Take" the money (at least present the illusion)// "Take" the money (at least present the illusion) theClientsAccount.withdraw(amount);theClientsAccount.withdraw(amount); theServersAccount.deposit(amount);theServersAccount.deposit(amount); // and run. Send back the modified object// and run. Send back the modified object outputToClient.writeObject(theClientsAccount);outputToClient.writeObject(theClientsAccount);}}

Page 10: Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side

Networking with Java N-10

Client with a Loop replace simple read with loop // The clients account// The clients account BankAccount myAccount = new BankAccount("Sucker", 5000.00);BankAccount myAccount = new BankAccount("Sucker", 5000.00); // Loop as long as the client wishes to give away money.// Loop as long as the client wishes to give away money. // (Okay, there really is no money being moved, it's just an illusion)// (Okay, there really is no money being moved, it's just an illusion) while (true) {while (true) { // Try to get a positive amount from the client using this program// Try to get a positive amount from the client using this program String amountAsString = JOptionPane.showInputDialog(null,String amountAsString = JOptionPane.showInputDialog(null, "You've won! Enter desired amount" + " you have ""You've won! Enter desired amount" + " you have " + myAccount.getBalance());+ myAccount.getBalance()); double amount = Double.parseDouble(amountAsString);double amount = Double.parseDouble(amountAsString);

// Write the amount this client thinks is winning// Write the amount this client thinks is winning outputToServer.writeObject(new Double(amount));outputToServer.writeObject(new Double(amount)); // Since the server is reading, the write is necessary to prevent// Since the server is reading, the write is necessary to prevent // an end of file exception when this client shuts down.// an end of file exception when this client shuts down. // Terminate this program when the client determines a positive// Terminate this program when the client determines a positive // input to the dialog box results in a decreasing balance for the client// input to the dialog box results in a decreasing balance for the client if (amount <= 0)if (amount <= 0) break;break; // And the account from which the server will "withdraw" money// And the account from which the server will "withdraw" money outputToServer.writeObject(myAccount);outputToServer.writeObject(myAccount); // Get a new version of this client's account back from the server // Get a new version of this client's account back from the server myAccount = (BankAccount) inputFromServer.readObject();myAccount = (BankAccount) inputFromServer.readObject(); }} }}

Page 11: Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side

Networking with Java N-11

Summary• Networking is made much easier with Java’s extensive

API that hides a lot of networking details• Networking is integrated with input and output: both

use streams• You can read and write strings with BufferedReader

and PrintWriter, but Use ObjectOutputStream and ObjectInputStream to read and write any object the implements Serializable

• You can use loops in the client and server to communicate for a while

• If you read from one program, write from the other