1. i ntroduction to n etworks network programming is surprisingly easy in java ◦ most of the...

16
1

Upload: caroline-hoover

Post on 16-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net

1

Page 2: 1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net

INTRODUCTION TO NETWORKS Network programming is surprisingly easy in

Java◦ Most of the classes relevant to network

programming are in the java.net package

Sending data out onto a network involves attaching a stream to a network connection (socket) and using the stream I/O functions

The main issues to consider are client/server architectures and attaching multiple clients to a server

First we will look an introduction to networking and the TCP/IP protocol

2

Page 3: 1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net

INTERNET PROTOCOL (IP)Data is transmitted between computers

in packets◦ Each packet is marked with a destination address

130.65.83.2530 data

Internet

ClientServer

Server Ports

14

30

80

Network packet

Page 4: 1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net

Each 4 byte address is the IP addressNormally we refer to computers with domain names

www.bham.ac.uk java.sun.com

The translation from domain name to IP address is carried out using DNS (Domain Name Service) IP has no provision for re-transmission in case of failure to deliver packets

This is the job of the Transmission Control Protocol (TCP)Most internet programs (eg the WWW, email etc) are based on TCP/IP

Page 5: 1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net

5

Page 6: 1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net

SOCKETS: WHAT ARE THEY? If one looks up the definition, the most common

one would be "A socket is one endpoint of a two-way communication link between two programs running on the network." To put it differently, it is through sockets that applications access the network and transmit data. The types of sockets are as varied as the purposes and platforms of applications.

There are three types of sockets: Unix Domain Sockets Internet Domain Sockets NS Domain Sockets 6

Page 7: 1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net

Of these only Internet Domain Sockets are supported across all platforms. So to maintain the cross-platform characteristic intact, Java supports only Internet Domain Sockets.

The next question that arises is what are the characteristics of an Internet Domain Socket and what protocols are supported by it?

7

SOCKETS: WHAT ARE THEY?

Page 8: 1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net

INTERNET DOMAIN SOCKETS By definition "An Internet socket (or

commonly, a socket or network socket), is a communication end-point unique to a machine communicating on an Internet Protocol-based network, such as the Internet." All applications communicating through the Internet use a network socket. The feature that distinguishes a network sockets from other sockets is the protocols that it supports. The supported protocols are:

TCP UDP Raw IP The difference between them is based on

whether the protocol is connection oriented or not.

8

Page 9: 1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net

TCP

is one of the core protocols of the Internet protocol suite. The protocol guarantees reliable and in-order (correct order of packets) delivery of data from sender to receiver. To put it simply, it's reliable.

The second aspect of TCP is that it is connection oriented. That means TCP requires that a connection be made between the sender and receiver before data is sent.

The socket associated with TCP is known as the Stream Socket. 9

Page 10: 1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net

TCP/IP

A TCP/IP socket enables a java program running on a client machine to open a connection with a web server

There is a socket on both the client and server sides.

Client server communication is carried out through input and output streams 10

Page 11: 1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net

UDP

like TCP, is one of the core protocols of the IP suite. However, unlike TCP, it neither guarantees in-order delivery of data nor does it requires a connection to be established for sending the data.

To put it simply, UDP is an unreliable and connectionless protocol.

Sockets associated with UDP are known as Datagram Sockets.

11

Page 12: 1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net

RAW IP

Raw IP is a non-formatted protocol, unlike TCP and UDP. It works at network and transport layers. A socket associated with Raw IP is known as a Raw Socket.

UDP and TCP sockets just receive the payload or the data, whereas Raw Sockets receive the header info of the packet along with the data.

The downside of Raw Sockets is that they are tightly coupled with the implementation provided by the underlying host operating system.

12

Page 13: 1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net

13

Page 14: 1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net

HOW JAVA PLACES THE DIFFERENT TYPES OF SOCKETS IN ITS LIBRARIES.

Sockets in Java Like all other functionalities provided by Java,

functionalities to work with sockets are also "packaged" as a package and its classes. The following are the package and its main classes that help in accessing sockets:

java.net package ServerSocket Socket Among the above, Java abstracts out most of

the low-level aspects of socket programming. 14

Page 15: 1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net

SOCKETS IN JAVA

The java.net package contains all the classes required to create network enabled applications. ServerSocket and Socket are also part of this package. Apart from these classes, it also contains classes to connect to the web server, create secured sockets, and so forth.

The ServerSocket class provides server sockets or sockets at server side. Such sockets wait for requests over the network. Once such requests arrive, a server socket performs operations based on the request and may return a result. The ServerSocket class wraps most of the options required to create server-side sockets.

15

Page 16: 1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net

The Socket class provides client-side sockets or simply sockets. They are at the client side connecting to the server, sending the request to the server and accepting the returned result. Just as ServerSocket exposes only the compulsory parameters required to create a server-side socket, similarly, Socket asks the user to provide only those parameters that are most necessary.

Next: Socket programming, step by step >> 16

SOCKETS IN JAVA