Download presentation
Presentation is loading. Please wait.
Published byDustin Dawson Modified over 8 years ago
1
Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side
2
Networking with JavaN-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
3
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
4
Networking with JavaN-4 Writing using Sockets Get 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 = new ObjectInputStream(client.getInputStream()); ObjectInputStream inputFromClient = 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 with MyClass c = (MyClass)inputFromClient.readObject(); Assuming the client also has IO streams that is …
5
Networking with JavaN-5 The Client In 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);
6
Networking with JavaN-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());
7
Do some Input MyClass c = (MyClass) inputFromServer.readObject(); System.out.println(c.toString()); Object read and written objects must be 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 JavaN-7
8
Networking with JavaN-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
9
Networking with JavaN-9 Server with a loop replace simple write with loop BankAccount theClientsAccount = null; // theClientsAccount will be read from client after connection to this server BankAccount theClientsAccount = null; // This server's account will deposit the money withdrawn from the clients // account and then writes back the modified clients account. BankAccount theServersAccount = new BankAccount("Greedy", 0.00); // Continue as long as the client sends a positive amount (as a double) 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);}
10
Networking with JavaN-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(); } }
11
Networking with JavaN-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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.