Java Server Programming Web Interface for Java Programs.

Slides:



Advertisements
Similar presentations
Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project.
Advertisements

Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
Files from Ch4. File Input and Output  Reentering data all the time could get tedious for the user.  The data can be saved to a file. Files can be input.
1 Creating a network app Write programs that  run on different end systems and  communicate over a network.  e.g., Web: Web server software communicates.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 Java Networking – Part I CS , Spring 2008/9.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet.
System Programming Practical session 10 Java sockets.
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Java sockets. From waiting.
System Programming Practical session 11 Multiple clients server Non-Blocking I/O.
Client/Server In Java An Introduction to TCP/IP and Sockets.
© 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.
28-Jun-15 Basic Protocols. 2 Sockets Sockets, or ports, are a very low level software construct that allows computers to talk to one another When you.
15-Jul-15 Basic Protocols. 2 Sockets Sockets, or ports, are a very low level software construct that allows computers to talk to one another When you.
CSE 341, S. Tanimoto Java networking- 1 Java Networking Motivation Network Layers Using Sockets A Tiny Server Applets URLs Downloading Images, MediaTracker.
Web Proxy Server. Proxy Server Introduction Returns status and error messages. Handles http CGI requests. –For more information about CGI please refer.
TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery.
Io package as Java’s basic I/O system continue’d.
Greg Jernegan Brandon Simmons. The Beginning…  The problem Huge demand for internet enabled applications and programs that communicate over a network.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Socket programming 1. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "
Web Security Programming I Building Security in from the Start Except where otherwise noted all portions of this work are Copyright (c) 2007 Google and.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
Cli/Serv.: Chat/121 Client/Server Distributed Systems v Objectives –discuss a client/server based chat system –mention two other ways of chatting.
Lab 6: Introduction to Sockets (Web Programming – Part 1) Reference: Head First Java (2 nd Edition) by Kathy Sierra & Bert Bates.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
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.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
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.
1 CSCD 330 Network Programming Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 9 Client-Server Programming.
Part 4: Network Applications Client-server interaction, example applications.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
Creating a GUI Class An example of class design using inheritance and interfaces.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016.
Java Programming II Java Network (I) Java Programming II.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
Socket Programming in Java -First Step of Network Programming-
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
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.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
Echo Networking COMP
Threads in Java Two ways to start a thread
Lecture 21 Sockets 1 (Not in D&D) Date.
CSE 341, S. Tanimoto Java networking-
Block 15 Developing the Calculator application
MCA – 405 Elective –I (A) Java Programming & Technology
Client-server Programming
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Socket Programming 2: Application Layer.
CS18000: Problem Solving and Object-Oriented Programming
Presentation transcript:

Java Server Programming Web Interface for Java Programs

CSE 477 Winter 1999Introduction2/44 Introduction to Sockets What is a socket What is a socket –In the most simple view a socket is a way for programs to communicate across a network. For this to work we need to know some information. –There are 2 main things we need to know for this simple socket. We need to know the name or address of the other computer. With that information we should be able to find it on the network.We need to know the name or address of the other computer. With that information we should be able to find it on the network. We need to know what type of network we are talking across. Here we are going to talk about TCP/IP. With the success of the internet TCP/IP is the most prevalent protocol in use today.We need to know what type of network we are talking across. Here we are going to talk about TCP/IP. With the success of the internet TCP/IP is the most prevalent protocol in use today. –Can sockets be used to talk to different programs on the same computer? The answer is yes and we will go into more information later about that.The answer is yes and we will go into more information later about that.

CSE 477 Winter 1999Introduction3/44 Sockets - a more In-Depth Look More Information Needed More Information Needed –So a question that might occur to me from the simple view is can there be more then one program waiting for a socket on a computer at a time, and if so how can we distinguish them. –That leads us to Ports Ports are very simple. They are a way to distinguish which program on a computer that you want to talk to.Ports are very simple. They are a way to distinguish which program on a computer that you want to talk to. Some ports are reserved - the operating system says that any port under 3000 can only be listened to by a program I know about. This was for security reasons but proved in-effective. It is still around because of tradition.Some ports are reserved - the operating system says that any port under 3000 can only be listened to by a program I know about. This was for security reasons but proved in-effective. It is still around because of tradition. –So what information do we need to talk to a computer on the network? The type of Network - TCP/IPThe type of Network - TCP/IP The computer Name or AddressThe computer Name or Address The Port of the programThe Port of the program

CSE 477 Winter 1999Introduction4/44 Two sides of a Socket Sockets have 2 sides Sockets have 2 sides –First is the side that is listening. Listening means that it is waiting for some other program to talk to it. In a client - server model, the end that is listening is called the server. –The other end is the program making the connection. This end is referred to as the client. This end does all the work to set up the socket between the programs. It must know the address of the server and which port it is listening on. –Once the connection is made both ends look exactly the same and information can pass both ways. Later we will talk about how the programs communicate.

CSE 477 Winter 1999Introduction5/44 How do the programs communicate Now that we have this connection between the 2 computers how do they talk to each other? Now that we have this connection between the 2 computers how do they talk to each other? –Both programs need to come to an agreement. This agreement is called a protocol. –Protocols vary from simple to complex. Simple protocol - send the number of bytes you are going to send then each byte.Simple protocol - send the number of bytes you are going to send then each byte. Complex protocol usually is contained within a simple protocol. An example would be to send a command followed by some parameters. When the server receives the command it will respond back with the results of the command then the client will respond back acknowledging the results were received.Complex protocol usually is contained within a simple protocol. An example would be to send a command followed by some parameters. When the server receives the command it will respond back with the results of the command then the client will respond back acknowledging the results were received.

CSE 477 Winter 1999Introduction6/44 A Simple Example: Server Side import java.net.*; import java.io.*; public class SimpleServer extends Thread { private ServerSocket listenSocket; public int listenPort = 6000; public String filename = "MyFile.txt";

CSE 477 Winter 1999Introduction7/44 public void run() { String input; Socket sock; PrintWriter fileOutput; PrintWriter socketOutput; BufferedReader socketInput; try { System.out.println("Setting up Server socket"); listenSocket = new ServerSocket(listenPort); while(true) { try { System.out.println("Listening for connection"); sock = listenSocket.accept(); System.out.println("We have a connection"); // We are only going to be getting strings so we will use a buffered input stream. // They will send us 1 line of text and we will appended it to the file. socketInput = new BufferedReader( new InputStreamReader( sock.getInputStream()));

CSE 477 Winter 1999Introduction8/44 socketOutput = new PrintWriter( sock.getOutputStream()); fileOutput = new PrintWriter( new FileWriter(filename, true)); System.out.println("Opened file"); // Read a line until there are no more left. Add them to the file. input = socketInput.readLine(); while(input != null) { System.out.println("Input: " + input); fileOutput.println( input ); input = socketInput.readLine(); } fileOutput.flush(); fileOutput.close(); sock.close(); System.out.println("closed file and socket"); } catch(Exception e) { System.out.println("Exception: " + e); sock = null; System.gc(); } catch(IOException e) { System.out.println("Unable to open socket: " + e); }

CSE 477 Winter 1999Introduction9/44 public static void main(String args[]) { SimpleServer s = new SimpleServer(); s.setDaemon(false); try { if(args.length > 2) throw new Exception("Wrong number of arguments"); if(args.length > 0) { s.listenPort = Integer.valueOf(args[0]).intValue(); } if(args.length > 1) { s.filename = args[1]; } s.start(); } catch(Exception e) { }

CSE 477 Winter 1999Introduction10/44 Understanding the Code: Server Side First we make our server class extend Thread., allowing us to start the server and return from the function call. We would then have our server running in the background. Making a thread in JAVA is easy. You inherit from the class Thread, then you make a function called run that describes what you want the thread to do. Next we set up some things for are server. The most important one to note is the port. We store this in a variable to allow us to change it’s value at run time. A ServerSocket is a class that can listen on a port and accept incoming connections. When we create the instance of the ServerSocket (we use the new operator), we give it a port number that it binds to. If this port is already in use an exception will be thrown. Java requires us to catch any possible thrown exceptions hence the try / catch surrounding the main body of the code. Well we are bound to the port and ready to get connections and our server wants to do this over and over again. So we go into an infinite loop, requiring the operating system to shut us down at the appropriate time. Then we accept a connection. What happens when we accept a connection is that we wait until someone else connects to the port we are listening on, on this server of course. We are just sitting there waiting unable to do any other work. This is important to remember for when our server gets a little more advanced. Now a client connects to us. When this happens we get a socket. This socket is how all information will be passed back and forth between the client and the server. Sockets send bytes across them to the other server. This can be tedious when working with strings. Java allows us to use the Output stream of a socket in any manner we wish.

CSE 477 Winter 1999Introduction11/44 Understanding the Code: Server Side … cont Next we get the Output stream from the socket and create a PrintWriter from it. PrintWriters send text very well and make it simple to pass text messages back and forth. If we were going to be sending an image this would not be a good choice. We do something similar with the input stream, creating a BufferedReader. BufferedReaders allow us to read a line of text from a stream. That is exactly what we need for this example. In general most things are sent as text across the net because it is convenient and easy to debug. Unfortunately because of those traits it is also very insecure and sometimes slow. Our example now opens a file that it will append incoming information to. We receive 1 line of text at a time writing it out to the file. When we get an empty line we close the connection and the file and go back to listing for a new connection. Lastly we have a simple main that will take a port and filename both optional set those parameters in the Server and start it going.

CSE 477 Winter 1999Introduction12/44 Simple Example: Client Side import java.net.*; import java.io.*; public class SimpleClient { private String serverName; private int serverPort; private Socket socket; public void sendMessage() { String message; PrintWriter socketOutput; BufferedReader socketInput; BufferedReader inReader; try { socket = new Socket(serverName, serverPort); socketOutput = new PrintWriter(socket.getOutputStream()); socketInput = new BufferedReader( new InputStreamReader( socket.getInputStream())); inReader = new BufferedReader( new InputStreamReader(System.in));

CSE 477 Winter 1999Introduction13/44 System.out.println("Enter Message to be Sent. Enter a blank line when done."); message = inReader.readLine().trim(); while(message.equals("") == false) { socketOutput.println(message); socketOutput.flush(); message = inReader.readLine().trim(); } socket.getOutputStream().close(); socket.close(); } catch(UnknownHostException uhe) { System.out.println("Unknown Server Name: " + serverName); } catch(IOException ioe) { System.out.println("Unable to send string: " + ioe); } public SimpleClient(String serverName, int serverPort) { this.serverName = serverName; this.serverPort = serverPort; }

CSE 477 Winter 1999Introduction14/44 public static void main(String args[]) { SimpleClient sc; if( args.length != 2) { System.err.println("Invalid Usage: SimpleClient servername serverport"); System.exit(1); } sc = new SimpleClient(args[0], Integer.valueOf(args[1]).intValue()); sc.sendMessage(); }

CSE 477 Winter 1999Introduction15/44 Understanding the Code: Client Side Unlike servers the client needs more information to get the connection going. The most important pieces of information are the address of the server and the port number it is listening to. With out that all else is moot. Our simple example connects to the server, sends several lines of text the disconnects. Main passes the information to our client application and calls sendMessage. sendMessage attempts to connect to the server. If this fails an exception will be thrown that must be handled. Like the server we use the input / output streams as BufferedReader / PrintWriter accordingly. We then prompt the user to enter text. We read each line and send it to the server. When they type a blank line we close the output stream so the server knows that no more information will be coming. The server closes the connection and we are done.