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

Slides:



Advertisements
Similar presentations
CS Network Programming CS 3331 Fall CS 3331 Outline Socket programming Remote method invocation (RMI)
Advertisements

Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Prepared By: Eng. Ola M. Abd El-Latif May/2010.  In this Lab we will answer the most frequently asked questions about programming sockets in Java. 
1 L54 Networking (3). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Network Programming CS3250. References Core Java, Vol. II, Chapter 3. Book examples are available from
System Programming Practical session 10 Java sockets.
Unit 201 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
Multithreaded Java COMP1681 / SE15 Introduction to Programming Fast Track Session 3.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L22 (Chapter 25) Networking.
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology 1 Let’s get started. Let’s start by selecting an architecture from among.
Networking with Java CSc 335 Object-Oriented Programming and Design Spring 2009.
Networking with Java. Basic Concepts A Network exists when two or more computers are connected such that they can communicate data back and forth. There.
Networking java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
Io package as Java’s basic I/O system continue’d.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
Socket Lab Info. Computer Network. Requirement Use TCP socket to implement a pair of programs, containing a server and a client. The server program shall.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 12 Communicating over.
Cli/Serv.: Chat/121 Client/Server Distributed Systems v Objectives –discuss a client/server based chat system –mention two other ways of chatting.
Working with files By the end of this lecture you should be able to: explain the principles of input and output and identify a number of different input.
Lab 6: Introduction to Sockets (Web Programming – Part 1) Reference: Head First Java (2 nd Edition) by Kathy Sierra & Bert Bates.
Outline Overview Video Format Conversion Connection with An authentication Streaming media Transferring media.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 11.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Rick Mercer Multi Threaded Chat Server. 2 Client – Server with Socket Connections We've seen how to establish a connection with 1 client Review a simple.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
© Lethbridge/Laganière 2005 Chap. 3: Basing Development on Reusable Technology The Client-Server Architecture A distributed system is a system in.
Web Design & Development 1 Lec - 21 Umair Javed. Web Design & Development 2 Socket Programming.
Sockets Sockets A socket is an object that encapsulates a TCP/IP connection There is a socket on both ends of a connection, the client side and the server.
Java Sockets Brad Vander Zanden. Agenda What are sockets Client/Server interaction Single client/server example Synchronized data objects Multi-threaded.
Java Sockets Tutorial Rahul Malik Nov 12, 2005.
VII. Sockets. 1. What is a Socket? A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes.
CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
Field Trip #25 Creating a Client/Server By Keith Lynn.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Java Server Programming Web Interface for Java Programs.
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
CSE 501N Fall ‘09 22: Introduction to Networking November Nick Leidenfrost.
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Files and Serialization. Files Used to transfer data to and from secondary storage.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Advanced Java Programming CSE 7345/5345/ NTU 531 Multithreaded/Sockets/Server Welcome.
Sockets A popular API for client-server interaction.
Operating Systems {week 11a}
Object-Orientated Analysis, Design and Programming
Threads in Java Two ways to start a thread
Block 15 Developing the Calculator application
Section 64 – Manipulating Data Using Methods – Java Swing
CH5 TCP Client - Server Example:
Client server programming
Clients and Servers 19-Nov-18.
Clients and Servers 1-Dec-18.
Introduction to Object-Oriented Programming
Clients and Servers 19-Jul-19.
CS18000: Problem Solving and Object-Oriented Programming
Clients and Servers 13-Sep-19.
Presentation transcript:

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

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

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

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 …

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

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

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

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

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

Networking with JavaN-10 Client with a Loop replace simple read with loop // The clients account // The clients account BankAccount myAccount = new BankAccount("Sucker", ); BankAccount myAccount = new BankAccount("Sucker", ); // 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(); } }

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