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.

Slides:



Advertisements
Similar presentations
Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
Advertisements

Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project.
COEN 445 Communication Networks and Protocols Lab 4
28.2 Functionality Application Software Provides Applications supply the high-level services that user access, and determine how users perceive the capabilities.
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. 
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.
CSCI 4550/8556 Computer Networks Comer, Chapter 3: Network Programming and Applications.
Programming project #4 1 CS502 Spring 2006 Programming Project #4 Web Server CS-502 Operating Systems Spring 2006.
© 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.1 Computer Networks and Internets with Internet Applications, 4e By Douglas.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
© 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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
An Introduction to Internetworking. Algorithm for client-server communication with UDP (connectionless) A SERVER A CLIENT Create a server-socket (listener)and.
Aalborg Media Lab 15-Jul-15 Java & EyesWeb Lecture 14 Java Client / Server Communication.
Client Server Model and Software Design TCP/IP allows a programmer to establish communication between two application and to pass data back and forth.
Networking with Java CSc 335 Object-Oriented Programming and Design Spring 2009.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery.
Socket Programming in Java -First Step of Network Programming-
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
Understanding the CORBA Model. What is CORBA?  The Common Object Request Broker Architecture (CORBA) allows distributed applications to interoperate.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 21.
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
1 CSC111H Client-Server: An Introduction Dennis Burford
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Internet Applications and Network Programming Dr. Abraham Professor UTPA.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 11.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
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.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
Position of application layer. Application layer duties.
Java Sockets Tutorial Rahul Malik Nov 12, 2005.
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.
The Client-Server Model And the Socket API. Client-Server (1) The datagram service does not require cooperation between the peer applications but such.
Socket Programming.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
Network Architecture Protocol hierarchies Design Issues for the layers
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
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.
Socket Programming in Java -First Step of Network Programming-
Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side.
A PC Wakes Up A STORY BY VICTOR NORMAN. Once upon a time…  a PC (we’ll call him “H”) is connected to a network and turned on. Aside: The network looks.
Distributed Systems CS Project 1: File Storage and Access Kit (FileStack) Recitation 1, Aug 29, 2013 Dania Abed Rabbou and Mohammad Hammoud.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
COMP2322 Lab 4 Socket Programming Toby Lam March 2, 2016.
SOCKET PROGRAMMING WITH JAVA By Collin Donaldson.
Java’s networking capabilities are declared by the classes and interfaces of package java.net, through which Java offers stream-based communications that.
Network Programming. These days almost all devices.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
Echo Networking COMP
HTTP AND ABSTRACTION ON THE INTERNET
Lecture 21 Sockets 1 (Not in D&D) Date.
Networking COMP
Socket Programming Cal Poly Pomona Young CS380.
Providing Network Services
Exceptions and networking
Presentation transcript:

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 are two basic models for programs that use networking – “Peer-to-Peer” or “Client- Server”. We will focus upon the Client Server Model.

Client - Server Many networking programs require that the user start up a program (known as the Client) and connect to some computer on the internet (known as the Server). A classic example of this model would be when you start up your favorite web browser (a client) and connect to some website’s web Server.

Client-Server The basic idea behind this model is that the Server has access to some resource that is unavailable to the client directly. The client requests some service from the Server, the server carries out the request, and the server then returns the results to the client. In our web-example, the client/browser wishes to access a web-page that is not on the client computer. It contacts the web server and requests the web page. The web server locates the page and returns its contents to the client/browser.

Exceptions Whenever we deal with things like networks in our Java programs we need to realize that strange things can occur that have nothing to do with our program. That is, errors can pop up that are not the result of our having written bad code. For example, our internet connection may temporarily be down or the server we are trying to access may be down etc. When such an event occurs we call it an “exception”. Often, Java requires us to “handle” such exceptions.

Try Catch Statement Java requires us to protect our program against certain types of exceptions. In particular, when we deal with Networking we will need to use a Try-Catch statement which watches for certain exceptions and catches them so that our program can handle them in a clean way.

Try Catch Syntax try { body-code } catch (exception-classname variable-name) { handler-code } Body-code consists of the operations we are trying to execute that might cause an exception to occur. Handler-code consists of what we wish to do if such an exception occurs.

Internet Addresses In order to write a program that works on the Internet, we need to understand Internet addressing. Every machine on the Internet has an Internet address. Such addresses consist of 4 integers separated by ‘.’s as in Each of the 4 integers will be between 0 and 255.

Internet Addresses The problem with these addresses is that humans don’t do real well trying to remember lots of numbers. Because of this, important, computers on the internets (usually servers) are given Domain Names. For example, or or espn.go.com and so on. These are easier for humans to remember.

Ports Ever wonder how your computer can keep track of so many internet applications at once? I mean you might be running a browser, listening to some streaming music, IMing a friend all at the same time! The answer is PORTS. Each computer on the Internet has a variety of different ports that specific programs listen to for communication. For example, your browser listens to port 80. Other applications may listen to say port 23 or port 55 etc.

Network Programs It is important to remember that Networking applications will actually consist of TWO programs. The Client program and the Server program. These programs will need to communicate back and forth in order for the entire application to function properly.

Sockets The most common way of implementing client- server communication is via SOCKETS. The easiest way to understand a Socket is to think of it as a sort of phone. That is, if you wished to carry on a conversation with a friend at a distance each of you would require a phone which you would use to send information (speak into the phone) and receive information (listen to your friend’s speech).

Sockets The vast majority of programming languages supply programmers with a library of code to implement Sockets. This includes Java. Our programs will allow the Client program to open a socket and connect to the Server program. Once connected, the client and server will be able to “speak” to each other simply by writing to the socket (to speak) or reading from the socket (to listen).

Sockets – The Server The Server portion of a network application needs to “bind” itself to a specific Port #. This simply means that it is “listening” at that port for someone to try to connect to it. Once a connection request arrives, the Server will have a Socket with which to communicate with the Client on the other end of the line.

Sockets – The Client The Client portion of your program will initiate contact with the Server. It will need to know either the IP-Address or the Server or its Domain Name as well as the correct Port # in order to connect. Once connected, it too will have a socket with which to communicate back and forth with the Server.

ServerSocket - Java In Java the way the Server “binds” itself to a particular Port # is by creating an object of class ServerSocket. It passes the port number it wishes to bind to to the constructor for ServerSocket. In the example below we create a ServerSocket variable named myserversocket and bind it to Port #4567. ServerSocket myserverSocket = new ServerSocket(4567);

ServerSockets Please note that a ServerSocket is NOT a Socket!!!! It is simply a mechanism that allows the Server to listen at a particular port for a connection from a client. Clients always initiate contact. Servers always wait for a connection request.

Servers – Obtaining a Socket Once a Server has created a ServerSocket and is bound to a specific port # it must wait for some client to come “knocking”. In Java, the server does this through the accept( ) method of the ServerSocket object as shown below. Socket mysocket = myserverSocket.accept();

Sockets Socket mysocket = myserverSocket.accept(); When accept() is called the server program will wait until a client tries to connect. Once a client connects, the accept routine will return an actual Socket objet that can be used by the server to communicate back and forth to the client. In our example above, all communication will occur through the variable mysocket and NOT through the variable myserverSocket which was only needed to bind the Server to the correct Port.

Closing up shop - Server Once the Server has finished processing all of the clients requests it should close the socket it was using for communication… mysocket.close( ); When the server is finished completely and is no longer interested in connecting with anymore clients it should close the ServerSocket object as well with…. myserverSocket.close( );

Sockets – the Client At this point we have seen that the Server is pretty simple. Create a ServerSocket object and bind it to a specific port #, execute the accept( ) method on the ServerSocket object and wait for a client, use the Server object returned by the accept( ) method to communicate with the client, close everything up when it is finished. Turns out the Client is actually easier….

Sockets- Clients Clients do not bind themselves to ports and therefore they do not need to worry about the ServerSocket class. To open a Socket connection with the server all the client needs to do is create a new Socket object and pass it the internet address of the Server and the port number the server is listening on as in…. Socket csocket = new Socket(“ ", 4567);

java.net.* All of the Socket and ServerSocket classes and method are found in the java.net.* library so ALWAYS make sure you import it into any client-server program you write!!! import java.net.* ;

Sample Server – Binding Server to Port ServerSocket MyService; try { MyService = new ServerSocket(4567); } catch (IOException e) { System.out.println(“UNABLE TO BIND SERVER ” + e ); }

Server Accepts a Connection and creates a Socket Socket serviceSocket = null; try { serviceSocket = MyService.accept(); } catch (IOException e) { System.out.println(“No SOCKET! “ + e); }

Client opens a Socket to Server Socket MyClient; try { MyClient = new Socket(” ", 4567); } catch (IOException e) { System.out.println(e); } NOTE – I keep using IP adress this ALWAYS means “the machine I am running on!!!” So I am assuming in our examples that we are running the client and server on the same machine!

Sending/Receiving It sucks so I’ll just show it to you and we will go from there…..