Tcp-Udp
Tcp-Udp
Communicatio
n
by Kumudha Raimond
TCP Sockets
Server Side
• Create a ServerSocket object
• Put the server into a waiting state
• Set up input and output streams
• Send and receive data
• Close the connection (after completion
of the dialogue)
Client Side
• Establish a connection to the server
• Set up input and output streams
• Send and receive data
• Close the connection
by Kumudha Raimond
Java API for TCP
⚫ Data stream abstraction
◦ enables reliable transfer (send can be
blocking)
◦ marshaling/unmarshaling of data
◦ access to TCP parameters:
ReceiveBufferSize, SendBufferSize
⚫ Classes Socket and ServerSocket
◦ Socket asks for connection
◦ ServerSocket listens and returns Socket
when contacted
⚫ Port numbers
◦ explicit for ServerSocket, transparent for
Socket by Kumudha Raimond
Java API for TCP
Class ServerSocket:
⚫ close
by Kumudha Raimond
Java API for TCP
Class Socket:
🞆 connect to SocketAddress
🞆 getRemoteSocketAddress since that was chosen by the TCP system on
the other side
🞆 getInputStream, getOutputStream
⚫ use them for reading and writing
⚫ which is/may be blocking
🞆 DataInputStream, DataOutputStream:
⚫ wrapper classes for streams
⚫ have methods for marshaling/ unmarshaling
🞆 isConnected
🞆 close
by Kumudha Raimond
TCP Sockets
Setting up a server process requires five steps
by Kumudha Raimond
TCP Sockets
Setting up a server process requires five steps
by Kumudha Raimond
TCP Sockets
Setting up a client process requires FOUR steps
by Kumudha Raimond
UDP Datagram Communication
Single Datagram Message :
User Datagram Protocol Message sent in a single packet
(called datagram)
(UDP)
• UDP is a communication protocol for time-sensitive applications
like gaming, playing videos, or Domain Name System (DNS)
lookups.
• A datagram sent by UDP is transmitted from a sending process
to a receiving process without acknowledgement or retries.
• If a failure occurs, the message may not arrive.
• A datagram is transmitted between processes when one
process sends it and another receives it.
by Kumudha Raimond
UDP Datagram Communication
• Unlike TCP/IP sockets, datagram sockets are connectionless.
• That is, the connection between client and server is not maintained
throughout the duration of the dialogue.
• Instead, each datagram packet is sent as an isolated transmission
whenever necessary.
• Datagram (UDP) sockets provide a faster means of transmitting data
than TCP/IP sockets, but they are unreliable.
• Since the connection is not maintained between transmissions, the
server does not create an individual Socket object for each client, as
in TCP/IP example.
• A further difference from TCP/IP sockets is that, instead of a
ServerSocket object, the server creates a DatagramSocket object, as
does each client when it wants to send datagram(s) to the server.
• The final and most significant difference is that DatagramPacket
Single Datagram Message :
UDP Datagram Communication Message sent in a single packet
(called datagram)
User Datagram Protocol
(UDP)
• To send or receive messages a process must first create a
socket bound to an Internet address of the local host and a
local port.
• A server will create – inform clients
• A client binds its socket to any free local port.
• The receive method returns the Internet address and port of the
sender, in addition to the message, allowing the recipient to
send a reply
by Kumudha Raimond
UDP Datagram Communication
by Kumudha Raimond
UDP Datagram Communication
Issues relating to datagram communication:
Timeouts:
⚫ The receive that blocks forever is suitable for use by a server
that is waiting to receive requests from its clients.
⚫ But not appropriate that a process that has invoked a receive
operation should wait indefinitely in situations where the
sending process may have crashed or the expected message
may have been lost.
⚫ Timeouts can be set on sockets.
⚫ Choosing an appropriate timeout interval is difficult, but it
should be fairly large in comparison with the time required to
transmit a message.
by Kumudha Raimond
UDP Datagram Communication
Issues relating to datagram communication:
Receive from any:
by Kumudha Raimond
UDP Datagram Communication
Issues relating to datagram communication:
Failure Models
by Kumudha Raimond
UDP Datagram Communication
Issues relating to datagram communication:
◦Datagram service (connectionless)
◦Data may be lost
◦Data may arrive out of sequence
◦Checksum for data but no retransmission
◦Bad packets dropped
Example
⚫ UDP Client
◦ sends a message and gets a reply
⚫ UDP Server
◦ repeatedly receives a request and sends it back to the client
) Port
6789
messag
clien e serve
t r
Use of UDP
⚫ Domain Name System, which looks up DNS names in the
Internet, is implemented over UDP.
⚫ Voice over IP (VOIP) also runs over UDP.
⚫ UDP datagrams are sometimes an attractive choice
because they do not suffer from the overheads associated
with guaranteed message delivery.
⚫ There are three main sources of overhead:
◦ the need to store state information at the source and
destination;
◦ the transmission of extra messages;
◦ latency for the sender.
UDP Sockets
Server Side
• Create a DatagramSocket object
• Create a buffer for incoming datagrams
• Create a DatagramPacket object for the incoming datagrams
• Accept an incoming datagram
• Accept the sender's address and port from the packet
• Retrieve the data from the buffer
• Create the response datagram
• Send the response datagram
• Close the Datagram Socket
UDP Sockets
Client Side
•Create a DatagramSocket object
•Create the outgoing datagram
•Send the datagram message
•Create a buffer for incoming datagrams
•Create a DatagramPacket object for the incoming datagrams
•Accept an incoming datagram
•Retrieve the data from the buffer
•Close the DatagramSocket
Java API for UDP Datagrams
The Java API provides datagram
communication by means of two classes:
DatagramPacket and DatagramSocket.
This class provides a constructor that makes an instance out of an array of bytes
comprising a
message, the length of the message and the Internet address and local port
number of the destination socket.
Datagram packet
Its arguments specify an array of bytes in which to receive the message and the
length of the array.
A received message is put in the DatagramPacket together with its length and the
Internet address and port of the sending socket.
The message can be retrieved from the DatagramPacket by means of the method
getData.
The methods getPort and getAddress access the port and Internet address.
by Kumudha Raimond
Java API for UDP Datagrams
Datagram Socket
•This class supports sockets for sending and receiving UDP datagrams.
•It provides a constructor that takes a port number as its argument, for use by
processes that need to use a particular port.
•It also provides a no-argument constructor that allows the system to choose
a free local port.
•These constructors can throw a SocketException if the chosen port is already
in use or if a reserved port is specified
by Kumudha Raimond
Java API for UDP Datagrams
The class DatagramSocket rovides methods
send and receive: These methods are for transmitting datagrams between a
pair of sockets. The argument of send is an instance of DatagramPacket
containing a message and its destination. The argument of receive is an empty
DatagramPacket in which to put the message, its length and its origin. The
methods send and receive can throw IOExceptions.
setSoTimeout: This method allows a timeout to be set. With a timeout set, the
receive method will block for the time specified and then throw an
InterruptedIOException.
connect: This method is used for connecting to a particular remote port and
Internet address, in which case the socket is only able to send messages to and
receive messages from that address
by Kumudha Raimond
Java API for UDP Datagrams
Class DatagramSocket
⚫ socket constructor
◦ bound to free port if no arg
◦ arguments InetAddress, Port
⚫ send a DatagramPacket, non-blocking
⚫ receive DatagramPacket, blocking
⚫ setSoTimeout (receive blocks for time T and throw
InterruptedIOException)
⚫ close DatagramSocket
⚫ throws SocketException if port unknown or in use
⚫ connect and disconnect (!!??)
⚫ setReceiveBufferSize and setSendBufferSize
by Kumudha Raimond
UDP Sockets
Setting up a server process requires NINE steps
1. Create a DatagramSocket object
🞆 Just as for the creation of a ServerSocket object, this means supplying the
object's constructor with the port number.
🞆 For example:
DatagramSocket datagramSocket = new DatagramSocket(1234);
2. Create a buffer for incoming datagrams
🞆 This is achieved by creating an array of bytes.
🞆 For example:
byte[] buffer = new byte[256];
3. Create a DatagramPacket object for the incoming datagrams
🞆 The constructor for this object requires two arguments:
⚫ The previously-created byte array;
⚫ The size of this array.
🞆 For example:
DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
by Kumudha Raimond
UDP Sockets
Setting up a server process requires NINE steps
by Kumudha Raimond
UDP Sockets
Setting up a server process requires NINE steps
by Kumudha Raimond
UDP Sockets
Setting up a server process requires NINE steps
7. Create the response datagram
🞆 Create a DatagramPacket object, using an overloaded form of the
constructor that takes four arguments:
⚫ The byte array containing the response message;
⚫ The size of the response;
⚫ The client's address;
⚫ The client's port number.
🞆 The first of these arguments is returned by the getBytes method of the
String class (acting on the desired String response).
🞆 For example:
DatagramPacket outPacket = new DatagramPacket(response.getBytes(),
response.length(),clientAddress, clientPort);
🞆 Here, response is a String variable holding the return message.
by Kumudha Raimond
UDP Sockets
Setting up a server process requires NINE steps
by Kumudha Raimond
UDP Sockets
Setting up a CLIENT process requires EIGHT steps
by Kumudha Raimond
UDP Sockets
Setting up a CLIENT process requires EIGHT steps
by Kumudha Raimond
UDP Sockets
Setting up a CLIENT process requires EIGHT steps
by Kumudha Raimond
External Data Representation
■ At language-level data are stored in data structures
■ At TCP/UDP-level data are communicated as ‘messages’ or streams of bytes
■ Different data types and data structures before transmission must be
flattened (converted to a sequence of bytes) and rebuilt on arrival
Problems:
Integers: 1'complement vs. 2'complement
⚫ Real/Float: IEEE 754 standard vs. IBM Mainframes
⚫ Byte order in words: big-endianness vs. little-endiannes
⚫ Character Rep : ASCII (one byte/char) vs. Unicode (two bytes/char)
External data representation and marshalling
■ Problems:
🡪 1st and 2nd approaches marshal the primitive data types into a binary form
🡪 3rd approach – primitive data types – rep textually – longer than binary form
🡪 Alternative approach : is to marshal all of the objects to be transmitted into ASCII
text – simple to implement but marshalled form will be longer – HTTP protocol
🡪 Java and XML include data type information – not CDR
Marshalling and unmarshalling activities are carried out by a middleware layer
without any involvement of the application programmer
External data representation and marshalling
■ XML :
◆ XML: software available for marshalling and unmarshalling
◆ XML: primitive datatypes are represented textually
■ Whether the marshalled data include info concerning type of its contents?
◆ CDR: no, just the values of the objects transmitted
◆ Java: yes, type info in the serialized form
◆ XML: yes, type info refer to externally defined sets of names (with types),
namespaces
1. CORBA’s Common Data Representation (CDR)
• CORBA CDR is the external data representation defined with CORBA 2.0.
• CDR can represent all of the data types that can be used as arguments and return
values but not objects in remote invocations in CORBA.
• These consist of 15 primitive types, which include short (16-bit), long (32-bit),
unsigned short, unsigned long, float (32-bit), double (64-bit), char, boolean (TRUE,
FALSE), octet (8-bit), and any (which can represent any basic or constructed type -
sequence, string, array, struct, enum and union); together with a range of
composite types.
⚫ note that it does not deal with objects (only Java does: objects and tree of objects)
1. CORBA’s Common Data Representation (CDR)
CDR Rules
Byte Order
•Data is marshaled using the native endian format of the computer that is sending a
message.
•A flag in the header of messages specifies whether the message is in big-endian or
little-endian format.
•If the receiving computer uses the same endian format then the data can be
unmarshaled directly; otherwise byte-swapping is performed when unmarshaling
numeric values.
•Bytes Requirement
•char, octet and boolean - 1 byte of memory
•short - 2 bytes of memory
•long, unsigned short, unsigned long and float - 4 bytes of memory
•Double - 8 bytes of memory
•long double - 16 bytes of memory
Marshalling in CORBA (cntd.)
struct Person{
string name;
string place;
long year
};
IDL declaration of a Person struct
Marshalling in CORBA (cntd.)
struct with value: {1934, ‘Abba’, 2, Çomp1, 12000}
index in notes
sequence of bytes 4 bytes on representation 1934 0-3
0–3 5 length of string 4 4-7
"Smit" ‘Smith’ ABBA 8-11
4–7
2 12-
8–11 "h___" 15
12–15 length of string 5 16-
16–19 6
"Lond" ‘London’ 19
"on__" Comp 20-
20-23
23
24–27 1934 unsigned long 1 24-
27
1200 28-
The flattened form represents a Person
31
struct with value: {‘Smith’, ‘London’, 1934} 0
5 Smit h 6 Lond on 1934 32-
35
Java Object Serialization
Serialization is a mechanism of converting
the state of an object into a byte stream.
Handles:
56
3. XML
⚫ XML is a markup language that was defined by the World Wide Web Consortium
(W3C) for general use on the Web.
⚫ The term markup language refers to a textual encoding that represents both a
text and details as to its structure or its appearance.
⚫ XML was designed for writing structured documents for the Web.
⚫ XML is extensible in the sense that users can define their own tags, in contrast to
HTML, which uses a fixed set of tags.
⚫ Since the fundamental format of XML is standardized, if shared or transmitted XML
across systems or platforms, either locally or over the internet, the recipient can
still parse the data due to the standardized XML syntax.
Web services are XML-based information exchange systems that use the Internet for direct
application-to-application interaction
3. XML