1 / 11

Networking with Java

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);

rjessie
Download Presentation

Networking with Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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

  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 = 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

  5. 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

  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(server.getOutputStream()); ObjectInputStream inputFromServer = new ObjectInputStream(server.getInputStream()); Networking with Java N-6

  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() { return "This could be any of your types."; } } Networking with Java N-7

  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 Networking with Java N-8

  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(); 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

  10. 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

  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 Networking with Java N-11

More Related