140 likes | 200 Views
Outline. Client-Server Example Steps required on the server side Steps required on the client side. Networking with Java. N- 1. Server Using Sockets. Create a ServerSocket // 4000 is the only port available in our labs! ServerSocket socket = new ServerSocket(4000);
E N D
Outline • Client-Server Example • Steps required on the server side • Steps required on the client side Networking with Java N-1
Server Using Sockets Create a ServerSocket // 4000 is the only port available in our labs! ServerSocket socket = new ServerSocket(4000); • Establishes the port where the server waits for connections from clients Networking with Java N-2
Server Using Sockets Wait for a connection from the client with a Socket 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 Networking with Java N-3
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 = new ObjectOutputStream(client.getOutputStream()); 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()); 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 … Networking with Java N-4
The Client In a separate program, create a socket to connect to the server String IPAddress = "localhost"; int port = 4000; Socket server = new Socket(IPAddress, port); Networking with Java N-5
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(server.getOutputStream()); ObjectInputStream inputFromServer = new ObjectInputStream(server.getInputStream()); Networking with Java N-6
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() { return "This could be any of your types."; } } Networking with Java N-7
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 Networking with Java N-8
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(); if (amount <= 0.0) break; // read the client's account theClientsAccount = (BankAccount) inputFromClient.readObject(); // "Take" the money (at least present the illusion) theClientsAccount.withdraw(amount); theServersAccount.deposit(amount); // and run. Send back the modified object outputToClient.writeObject(theClientsAccount); } Networking with Java N-9
Client with a Loop replace simple read with loop // The clients account BankAccount myAccount = new BankAccount("Sucker", 5000.00); // Loop as long as the client wishes to give away money. // (Okay, there really is no money being moved, it's just an illusion) while (true) { // Try to get a positive amount from the client using this program String amountAsString = JOptionPane.showInputDialog(null, "You've won! Enter desired amount" + " you have " + myAccount.getBalance()); double amount = Double.parseDouble(amountAsString); // Write the amount this client thinks is winning outputToServer.writeObject(new Double(amount)); // Since the server is reading, the write is necessary to prevent // an end of file exception when this client shuts down. // Terminate this program when the client determines a positive // input to the dialog box results in a decreasing balance for the client if (amount <= 0) break; // And the account from which the server will "withdraw" money outputToServer.writeObject(myAccount); // Get a new version of this client's account back from the server myAccount = (BankAccount) inputFromServer.readObject(); } } Networking with Java N-10
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 Networking with Java N-11