Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exceptions and networking

Similar presentations


Presentation on theme: "Exceptions and networking"— Presentation transcript:

1 Exceptions and networking
SPL – PS9 Exceptions and networking

2 Overview Exceptions Internet protocol Java sockets

3 Exceptions Java uses exceptions to handle errors and other exceptional events. For example, reading a file that doesn’t exist, dividing by zero, networks problem, and an attempt to access an element out of the array’s bounds.

4 Exceptions (cont) When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception, contains information about the error. After an exception was thrown, the runtime system attempts to find someone to handle it. By searching the call stack in reverse order looking for a block of code that can handle the exception. If no such block of code was found, the exception is passed to the JVM which terminates the program.

5 Exceptions types There are two types of exceptions, unchecked exceptions and checked exceptions. Unchecked exceptions could be divided into errors, and runtime exceptions. Unchecked exceptions usually indicate conditions which the application cannot anticipate or recover from. Checked exceptions are conditions which a well-written application should anticipate and recover from. For example, not finding a file.

6 Exceptions types (cont)

7 Creating new exceptions

8 Handling exceptions When handling exceptions, we could do one of the following: Change conditions and retry again. Clean up and rethrow exception, for example when attempting to open a file that doesn’t exist.

9 The finally block The finally block comes right after all catch block in a try-catch clause. The code in the finally block is always executed before exiting the try- catch clause, even if an exception occurred, or return/continue/break statement was encountered.

10

11 Try with resources Instead of using the finally block, since Java 7 we have the possibility of try with resources. An object that implements the interface AutoCloseable can be declared as part of the try statement.

12 The Internet The Internet is a global system of interconnected computer networks. Every machine on the Internet has a unique identifying number, called an IP address. The IP stands for Internet Protocol, which is the language that computers use to communicate over the Internet. The original designers of TCP/IP defined an IP address as a 32bit number, this system is known as IPv4. Due to the enormous growth of the Internet, a new system called IPv6 using 128 bits for the address was introduced.

13 TCP/IP model The TCP/IP model is a description framework for computer network protocols. TCP/IP provides end-to-end connectivity specifying how data should be formatted, addressed, transmitted, routed and received at the destination. TCP/IP generally described as having four abstraction layers: Link Layer Internet Layer Transport Layer Application Layer

14 Special IP addresses There are some special IP addresses:
– is reserved for the default network. – is used for broadcasts (Messages that are intended for all computers on a network) – is used for loopback address. That means it is used by the computer to send a message back to itself. Also aliased as localhost.

15 Domain Name Systems (DNS)
Remembering IP addresses is hard for humans. The DNS service maps text names to IP addresses automatically. An helpful analogy is to think of the DNS service as a phone book of the internet, which translates user-friendly addresses to IP addresses.

16 Internet Servers and Clients
All of the machines in the Internet are either servers or clients (or both). Machines that provides services are servers, and machines which are used to connect to those services are clients.

17 Ports Any server machine makes it’s service available using numbered ports – one for each service available on the server. You can think as the IP:Port combo as this: The IP is the building address, and the Port is the apartment number. For example, if a server machine is running a web server and a file transfer protocol, the web server will typically be available in port 80, and the FTP server will be available in port 21.

18 Sockets Sometimes applications require low-level communication, for example, connecting to a database, or implementing instant messaging. Networking allows processes running on different hosts to exchange messages, using sockets. When messages are sent, they are queued at the sending socket until the network protocol has transmitted them.

19 Datagram Communication
The datagram communication protocol known as UDP (user datagram protocol), is a connectionless protocol – meaning that the datagram can be sent at any moment without prior connection preparation as in TCP. There is no guarantee that the datagram will be delivered, or that datagrams will be delivered in correct order. The main advantage of UDP is that you can broadcast, and that it is fast, the main disadvantage is that it is unreliable.

20 The stream communication
The stream communication protocol is known as TCP (transfer control protocol). Unlike UDP, TCP is a connection-oriented protocol, meaning that a connection must be established between the two pair of sockets before messages can be sent. Guaranteed that all data sent will be received, without any error and in the correct order.

21

22

23

24 The Client-Server Model
The client-server model is a very common model in many networking applications. The server provides some service, and the client uses the service provided by the server. A socket connection is established between server and client on top of a TCP-connection.

25 The Client-Server Model (cont)
To establish a connection, the model determines that: The server waits for connection requests from clients. The server only reacts to the initiative of the client. To remain available for further connections, the server doesn’t open the connection directly with the client on the incoming socket. Instead, it creates a private socket which the client can keep connected for the whole period of the session. Servers must be built to maximize availability – They must handle requests for connections as fast as possible, and then return to the mode where they can receive and handle more requests from clients.

26 Text encoding Different computers, runtime environments, languages and compilers use different symbols representation. If a server expects the text to be encoded in ASCII, and the client sends it in UTF-8, this would cause a problem. We can create the readers and writers with the encoding specified

27 The Protocol Interface
We don’t want to hold the implementation of a protocol inside the server’s code, since handling the communication is complex enough. For that purpose, we can define an interface MessagingProtocol.


Download ppt "Exceptions and networking"

Similar presentations


Ads by Google