250 likes | 360 Views
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering. What Is Socket. A socket is one endpoint of a two-way communication
E N D
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616 Course Name: Software Engineering
What Is Socket • A socket is one endpoint of a two-way communication • link between two programs running on the network. • 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 socket in use usually has an address bound to it. The • nature of the address depends on the communication • domain of the socket. • A single socket can communicate in only one domain.
Where Is Socket From? • 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).
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. • User Datagram Protocol (UDP): sits next to TCP and can • be used directly to support fast, connectionless, unreliable • transport of packets.
Five Socket Types • Datagram socket • Stream socket • Reliably delivered message socket • Raw socket • Sequenced packet socket
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)
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.
TCP/IP Server Figure 1 Socket function for TCP client-server ServerSocket well-known port bind listen TCP/IP Client accept Socket blocks until connection from client connection establishment connect (TCP three-way handshake) write Read data (request) process request data (reply) read Write end-of-file notification read Close Conn. Close Conn.
Seven Basic Operations • Connect to a remote machine • Send data • Receive data • Close a connection • Listen for incoming data • Bind to port Needed only by the server • Accept connections from remote machines • on the bound port
Socket Class -Characteristics • Socket class is Java’s fundamental class for performing • client-side TCP operation. • This class is used for creating TCP connections over an • IP network • 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
Socket Class -Constructors • 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 • Two protected constructors • Protected Socket() • Protected Socket(SocketImpl impl)
Socket Class -How does it work? Step one The creation of a Socket object implicitly establishes a connection between the client and server. p o r t Server Connection request Client Step two Server p o r t Client p o r t connection port
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.
ServerSocket Class -Constructors Three Public Constructors ServerSocket(int port), ServerSocket(int port, int maxQueue) ServerSocket(int port, int maxQueue, InetAddress localAddress) How many request I can hold?
UDP Socket in Java • Less complex • Incurs fewer overheads • Used in bandwidth-limited applications, where • the overhead associated with resending packets • is not tolerable. A good example: real-time • network audio applications. • Implemented by DatagramPacket class & • DatagramSocket class
UDP Sever Socket() bind recvfrom UDP Client Socket() blocks until datagram received from a client data (request) sendto process request data (reply) sendto recvfrom close Socket functions for UDP client-server
The Two Major Classes 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 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.
How does it work? • 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
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.
With Multicast Sockets Without Multicast Sockets Client Router The Internet Router Server the simplest possible multicast configurations
Why need Multicasting? The growing requirement 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. The resources saving and the traffic relief 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
How to do it? 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 . .
Four Key Operations The behavior of MulticastSocket is very similar to DatagramSocket’s • Join a multicast group • Send data to the member of the group • 3. Receive data from the group • 4. Leave the group
Problems • Performance • Security • Complexity • Restriction on multicast socket