Outline ClientServer Example Steps required on the server

  • Slides: 11
Download presentation
Outline • Client-Server Example Steps required on the server side Steps required on the

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 Server. Socket // 4000 is the only port available

Server Using Sockets Create a Server. Socket // 4000 is the only port available in our labs! Server. Socket socket = new Server. Socket(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 client

Server Using Sockets Wait for a connection from the client with a 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 Object. Input. Stream and Object. Output. Stream Networking with Java N-3

Writing using Sockets Get two IO streams to allow communication with the client and

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 Object. Output. Stream output. To. Client = new Object. Output. Stream(client. get. Output. Stream()); Object. Input. Stream input. From. Client = new Object. Input. Stream(client. get. Input. Stream()); Later, a client will be able to read this server's output when it write an object to the client with output. To. Client. write. Object(new My. Class()); and the client can write to this server that reads with My. Class c = (My. Class)input. From. Client. read. Object(); 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

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

The Client Get references to the Server's IO streams of the server with which this client can now read from and write to Object. Output. Stream output. To. Server = new Object. Output. Stream(server. get. Output. Stream()); Object. Input. Stream input. From. Server = new Object. Input. Stream(server. get. Input. Stream()); Networking with Java N-6

Do some Input My. Class c = (My. Class) input. From. Server. read. Object();

Do some Input My. Class c = (My. Class) input. From. Server. read. Object(); System. out. println(c. to. String()); Object read and written objects must be Serializable public class My. Class implements public String to. String() { return "This could be any } } Networking with Java Serializable { of your types. "; N-7

Use Object IO streams • • This example has one simple read. Object with

Use Object IO streams • • This example has one simple read. Object with a write. Object 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 Battle. Boat 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 Bank. Account the. Clients. Account

Server with a loop replace simple write with loop Bank. Account the. Clients. Account = null; // the. Clients. Account will be read from client after connection to this server Bank. Account the. Clients. Account = null; // This server's account will deposit the money withdrawn from the clients // account and then writes back the modified clients account. Bank. Account the. Servers. Account = new Bank. Account("Greedy", 0. 00); // Continue as long as the client sends a positive amount (as a double) while (true) { double amount = ((Double) input. From. Client. read. Object()). double. Value(); if (amount <= 0. 0) break; // read the client's account the. Clients. Account = (Bank. Account) input. From. Client. read. Object(); // "Take" the money (at least present the illusion) the. Clients. Account. withdraw(amount); the. Servers. Account. deposit(amount); // and run. Send back the modified object output. To. Client. write. Object(the. Clients. Account); } Networking with Java N-9

Client with a Loop replace simple read with loop // The clients account Bank.

Client with a Loop replace simple read with loop // The clients account Bank. Account my. Account = new Bank. Account("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 amount. As. String = JOption. Pane. show. Input. Dialog(null, "You've won! Enter desired amount" + " you have " + my. Account. get. Balance()); double amount = Double. parse. Double(amount. As. String); // Write the amount this client thinks is winning output. To. Server. write. Object(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 output. To. Server. write. Object(my. Account); // Get a new version of this client's account back from the server my. Account = (Bank. Account) input. From. Server. read. Object(); } } Networking with Java N-10

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

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 Buffered. Reader and Print. Writer, but Use Object. Output. Stream and Object. Input. Stream 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