25
1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

  • View
    223

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

1

Java Socket Support

Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

Page 2: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

2

What Is Socket A socket is one endpoint of a two-way communication

link between two programs running on the network.

A socket in use usually has an address bound to it. The

nature of the address depends on the communication

domain of the socket.

A socket is bound to a port number so that the TCP layer

can identify the application that data is destined to be sent.

A single socket can communicate in only one domain.

Page 3: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

3

Originally developed in 4.2BSD (Berkeley Software Distribution), cka Berkeley UNIX.

In UNIX, a process has a set of I/O descriptors that one reads from and writes to. These descriptors may refer to communication channels (sockets).

The lifetime of a descriptor is made up of three phases: creation (open socket), reading and writing (receive and send to socket), and destruction (close socket).

Where Is Socket From?

Page 4: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

4

User Datagram Protocol (UDP): sits next to TCP and can be used directly to support fast, connectionless, unreliable transport of packets.

Three Major Protocols Used For Socket

Internet Protocol (IP): a low-level routing protocol that breaks data into small packets and sends them to an address across a network, which is not reliable.

Transmission Control Protocol (TCP): a higher-level protocol that strings together the packets, routing and retransmitting them as necessary to reliably transmit your data.

Page 5: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

5

Five Socket Types

Datagram socket

Stream socket

Reliably delivered message socket

Raw socket

Sequenced packet socket

Page 6: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

6

Java Socket Support

The Java environment implements portable socket objects by stacking several object layers on top of low-level, platform-specific procedures.

Three types of sockets in Java

TCP/IP Socket (supported by Socket and SeverSocket classes)

UDP Socket (supported by DatagramSocket class)

Multicast Socket (supported by MulticastSocket class)

Page 7: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

7

TCP/IP Socket in Java

TCP/IP sockets are used to implement reliable,

bi-directional, persistent, point-to-point, stream-

based connections between hosts on the Internet

Useful for implementing network services –

such as remote login (telnet, rlogin) and file

transfer (FTP) – which require data of

indefinite length to be transferred.

Page 8: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

8

ServerSocket bind

listen

accept

Read

Write

read

Close Conn.

Socket

connect

write

read

Close Conn.

well-known port

blocks until connection from client

connection establishment (TCP three-way handshake)

data (request)

data (reply)

end-of-file notification

process request

Figure 1 Socket function for TCP client-server

TCP/IP Client

TCP/IP Server

Page 9: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

9

Seven Basic Operations

Connect to a remote machine

Send data

Receive data

Close a connection

Bind to port

Accept connections from remote machines on the bound port

Listen for incoming data

Needed only by the server

Page 10: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

10

Socket Class

Socket class is Java’s fundamental class for performing client-side TCP operation.

This class itself uses native code to communicate with the local TCP stack of the host operating system

The interface that the Socket class provides to the programmer is streams

This class is used for creating TCP connections over an IP network

-Characteristics

Page 11: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

11

Four public constructors:

Public Socket(String host, int port) throws UnknownHostException, IOException

Public Socket(InetAddress host, int port) throws IOException

Public Socket(String host, int port, InetAddress interface, int localPort) throws IOException

Public Socket(InetAddress host, int port, InetAddress interface, int localPort) throws IOException

Socket Class-Constructors

Two protected constructors Protected Socket() Protected Socket(SocketImpl impl)

Page 12: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

12

-How does it work?Socket Class

The creation of a Socket object implicitly establishes a connection between the client and server.

Server

Step one

ClientConnection request

port

Step two

Serverport

Clientport

connection

port

Page 13: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

13

ServerSocket Class

The SeverSocket is designed to be a “listener”, which waits for clients to connect before doing anything

Server sockets wait for connections while client sockets initiate connections

Listen for either local or remote client programs to connect to them on published ports.

Page 14: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

14

Three Public Constructors

ServerSocket(int port),

ServerSocket(int port, int maxQueue)

ServerSocket(int port, int maxQueue, InetAddress localAddress)

ServerSocket Class

-Constructors

How many request I can

hold?

Page 15: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

15

UDP Socket in Java Less complex

Used in bandwidth-limited applications, where

the overhead associated with resending packets

is not tolerable. A good example: real-time

network audio applications.

Incurs fewer overheads

Implemented by DatagramPacket class & DatagramSocket class

Page 16: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

16

Socket()

bind

recvfrom

sendto

close

recvfrom

sendto

Socket()

UDP Sever

UDP Client

blocks until datagram received from a client data (request)

data (reply)

process request

Socket functions for UDP client-server

Page 17: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

17

The Two Major Classes

DatagramSocket Class

In order for two agents to talk to each other over a UDP connection, a DatagramSocket object is created to ensure they both connected to a port on their local machines.

DatagramPacket class

Each DatagramPacket contains a data buffer, the address of the remote host to send the data to, and the port number the remote agent is listening to

Page 18: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

18

The DatagramPacket class stuffs bytes of data into UDP

packets called datagrams and lets you unstuffy datagrams

that you receive

A DatagramSocket sends and receives data using UDP

packets, represented as DatagramPacket objects

The remote process can receive the data in the form of a

DatagramPacket by calling the receive() method on its

DatagramSocket

How does it work?

Page 19: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

19

Multicast Sockets

Multicasting sends data from one host to many different

hosts, but not to everyone; the data goes only to clients

that have expressed and interest in the data by joining a

particular multicast group

On the Internet, such event like video conference, is best

implemented using Multicasting that built on top of UDP

Multicast Sockets in Java uses the DatagramPacket class

along with a new MulticastSocket class.

Page 20: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

20

The Internet

Client

Router

Router

Server

the simplest possible multicast configurations

With Multicast Sockets Without Multicast Sockets

Page 21: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

21

The goal of multicast sockets is that no matter how complex the network, the same data should never be sent more than once over any given network segment

All the unicast sockets provide point-to-point communication and create a connection with two well-defined endpoints. However, many tasks require a different model. For example, the television station need to broadcast to every user and Video conferencing, by contrast, sends an audio-video feed to a select group of people using multicast. Multicasting is broader than unicast, but narrower and more targeted than broadcast communication.

Why need Multicasting?The growing requirement

The resources saving and the traffic relief

Page 22: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

22

Step one

Create a MulticastSocket object and have the socket join a multicast group

Step two

Stuff the address of the multicast group in the DatagramPacket you want to send.

Step three

The routers and the MulticastSocket class take care of the rest

.

.

How to do it?

Page 23: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

23

1. Join a multicast group

2. Send data to the member of the group

3. Receive data from the group

4. Leave the group

Four Key Operations

The behavior of MulticastSocket is very similar to DatagramSocket’s

Page 24: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

24

Problems

Performance

Security

Complexity

Restriction on multicast socket

Page 25: 1 Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering

25

Thank You