IT495 Network Programing-Slides

Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

IT495

High-Speed Networks
Part 8: Network Programming

Haitham S. Hamza, Ph.D.


Amir Ibrahim, Shady Khalifa
Cairo University
Spring 2010

Acknowledgement: This presentation


contains some figures and text from:
Computer networks a systems approach.
by Larry L. Peterson & Bruce S. Davie
Agenda

• Network Programming
– Network API.
– Protocol Implementation issues.
• Sockets
– TCP and UDP.
– Client & Server application example.
• Assignment.
Introduction to Network programming

• Implementing an application program on


top of a network.

• Implementing the protocols running


within the network.

• Network API and network protocols


differences.
Implementing an Application
• Most network protocols are implemented in software
• Nearly all computer systems implement their network protocols
as part of the OS.
• There is an interface called application programming interface
(API) offers services provided by the OS to its network
applications.

Application

Network API

Protocol A Protocol B Protocol C


Implementing an Application [cont.]

• Each OS is free to define its API.

• The advantage of industry-wide support


for a single API is that applications can
be easily ported from one OS to another
(other aspects may limit this portability).
Implementing an Application [cont.]

• Each protocol provides a certain set of


services.

• The API provides a syntax by which


those services can be invoked in this
particular OS.

• Well defined interface (generality,


abstraction) enables you invoke the
services of many different protocols.
Generic Programming Interface

• Support multiple communication


protocol suites (TCP/IP family: Sockets,
WinSock & MacTCP)

• Address (endpoint) representation


independence.

• Provide special services for Client and


Server.
Socket Interface
• A socket is an abstract representation of a communication
endpoint.

• The point where a local application process attaches to the


network.
• The socket interface originally provided by the Berkeley
distribution of Unix was widely used and ported to OS’s other
than its native system in 80’s.
– Windows WinSock was provided in the 90’s.
• Uses existing I/O programming interface as much as possible.
– Socket is considered as a file in reading from and writing
to.
Socket Interface
• The interface defines operations for:
– Creating a socket.
– Attaching the socket to the network
– Sending/receiving messages through the
socket
– Closing the socket.

• An example application will be given in


the second part (client - server).
Implementing the protocols running within the
network
• Higher layers protocols interact with
lower layer protocols and vice versa.
– TCP needs an interface to send outgoing
messages to IP.
– IP needs to be able to deliver incoming
messages to TCP.
• A protocol-to-protocol interface is
needed to accomplish the overall task.
Process model

• Two models available:

– process-per-protocol model
– process-per-message model
Process-per-Protocol model

– Each protocol is implemented by a


separate process. This means that as a
message moves up or down the protocol
stack, it is passed from one
process/protocol to another

– (context switching)
Process-per-Message model

• Treats each protocol as a static piece of


code and associates the processes with
the messages.
• When a message arrives from the
network, the OS dispatches a process
that it makes responsible for the
message as it moves up the protocol
graph.
Message Buffers

• Application process provides a buffer


that contains the outbound message
when calling send & a buffer into which
an incoming message is copied when
invoking the receive operation.
• This forces the topmost protocol to copy
the message from the application’s
buffer into a network buffer, and vice
versa. (very expensive operation)
Message Buffers
• Most network subsystems define an abstract data type for
messages that is shared by all protocols in the protocol
stack.

• This abstraction provides copy-free ways of


manipulating messages in other ways, such as:
– adding and stripping headers.
– fragmenting large messages into a set of small
messages.
– and reassembling a collection of small messages into
a single large message.
Networking basics

• Computers running on the Internet


communicate to each other using
either the Transmission Control
Protocol (TCP) or the User Datagram
Protocol (UDP), as this diagram
illustrates:
JAVA APIs Your Code
Java.net
package
TCP
• TCP (Transmission Control Protocol) is a
– connection-based protocol
– that provides a reliable flow of data between two
computers.

• TCP guarantees that data sent from one end of the


connection actually gets to the other end and in the same
order it was sent. Otherwise, an error is reported.

• TCP provides a point-to-point channel for applications that


require reliable communications.
UDP

• UDP (User Datagram Protocol) is a protocol that


– sends independent packets of data, called datagrams,
from one computer to another
– with no guarantees about arrival.
– UDP is not connection-based like TCP.

• Sending datagrams is much like sending a letter through the


postal service: The order of delivery is not important and is
not guaranteed, and each message is independent of any
other.
Using Ports
• A computer has a single physical
connection to the network.
• All data destined for a particular
computer arrives through that
connection.
• However, the data may be intended for
different applications running on the
computer.
• So how does the computer know to
which application to forward the data?
Ports
• Data transmitted over the Internet is accompanied by addressing
information that identifies the computer and the application for
which it is destined.
• The computer is identified by its 32-bit IP address, which IP uses
to deliver data to the right computer on the network.
• Application is identified by a 16-bit port number, which TCP and
UDP use to deliver the data to the right application.
• Ports are locally significant so different port numbers can be
assigned to different clients for the same application.
• Port numbers range from 0 to 65,535 because ports are
represented by 16-bit numbers.
– The port numbers ranging from 0 - 1023 are restricted; they are reserved for
use by well-known services such as HTTP and FTP and other system
services. These ports are called well-known ports.
– Your applications should not attempt to bind to them.
Sockets

• A socket is one end-point of a two-way


communication link between two programs
running on the network.
• Socket classes are used to represent the
connection between a client program and a
server program. The java.net package
provides two classes:
– Socket : implement the client side of the
connection.
– ServerSocket : implement the server side of the
connection.
TCP Connection
• Normally, a server runs on a specific computer and has a socket that is bound to a specific
port number. The server just waits, listening to the socket for a client to make a connection
request.

• To make a connection request, the client tries to rendezvous with the server using the
server's IP and port.

• The client also needs to identify its’ application to the server so it binds to a local port number
that it will use during this connection. This is usually assigned by the system.

• If everything goes well, the server accepts the connection.

• Upon acceptance, the server gets a new socket bound to the same local port and also has its
remote endpoint set to the address and port of the client.

• It needs a new socket so that it can continue to listen to the original socket for connection
requests while tending to the needs of the connected client.

• On the client side, if the connection is accepted, a socket is successfully created and the
client can use the socket to communicate with the server.

• The client and server can now communicate by writing to or reading from their sockets.
Client Operation

create Socket to
communicate with Server

Connect to server

Client
using its IP and Port

Operation Read

Write

Close Socket
Server Operation
create bind socket with the Listen to connection
ServerSocket application port requests

Return to Listen to Accept


connection requests connection

create Socket to
communicate with client

Read

Read Client
Close socket disconnect Write
request
Java Client

• Import
– java.io.*;
– java.net.*;

• Socket:
– Socket socket = new Socket(server IP or hostname, port number);

• PrintWriter to write to the server:


– PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

• BufferedReader to read from the server :


– BufferedReader in = new BufferedReader(new InputStreamReader
(socket.getInputStream()));
Java Client
• write to server
– out.println(“text sent to server”);
– out.flush();
• Read from server
– String data = in.readLine();
• Closing the socket
– Close PrintWriter
• out.close();
– Close BufferReader:
• in.close();
– Close Socket
• socket.close();
To Summarize JAVA Client

1. Open a socket.
2. Open an input stream and output stream to the socket.
3. Read from and write to the stream according to the
server's protocol.
4. Close the streams.
5. Close the socket.

• Note : Only step 3 differs from client to client,


depending on the server. The other steps remain
largely the same.
Java Server

• The server program is implemented by two


classes:
– XServer : which contains the main method for the
server program and performs the work of listening
to the port, establishing connections, and reading
from and writing to the socket.
– XProtocol : which keeps track of the current state
and returns the various responses depending on
the input.
• This object implements the protocol (the language that
the client and server have agreed to use to
communicate).
XServer Class
• Import
– java.io.*;
– java.net.*;

• ServerSocket:
– ServerSocket serverSocket = new ServerSocket(port number the server is
going to listen on);
– Port number must not be already used by any other application

• Client Socket:
– Socket clientSocket = serverSocket.accept();
– The accept method waits until a client starts up and requests a connection
on the host and port of this server.
– When a connection is requested and successfully established, the accept
method returns a new Socket object which is bound to the same local port
and has it's remote address and remote port set to that of the client.
– The server can communicate with the client over this new Socket and
continue to listen for client connection requests on the original ServerSocket
XServer Class

• PrintWriter to write to the client:


– PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

• BufferedReader to read from the client :


– BufferedReader in = new BufferedReader(new InputStreamReader
(clientSocket.getInputStream()));

• Declare and Initialize the protocol:


– XProtocol protocol = new XProtocol();
– XProtocol is just a simple class with one function :
• public String processInput(String theInput)
• This function takes an input from the client -> do some
processing -> return the output
XServer Class
• Read from Client and process the input
– String data = protocol.processInput(in.readLine());
• write to Client
– out.println(data);
– out.flush();
• Closing the socket
– Close PrintWriter
• out.close();
– Close BufferReader:
• in.close();
– Close Client Socket
• clientSocket.close();
– Close ServerSocket
• serverSocket.close();
To summarize XServer Class

1. Open a ServerSocket.
2. Open one or more client Socket.
3. Bind client socket(s) to clients by accepeting their
connection requests.
4. Open an input stream and output stream to the client
socket.
5. Read from and write to the stream according to the
server's protocol.
6. Close the streams.
7. Close the client socket(s).
8. Close the ServerSocket.
Datagrams

• The UDP protocol provides a mode of network communication


whereby applications send packets of data, called datagrams, to
one another.

• A datagram is an independent, self-contained message sent over


the network whose arrival, arrival time, and content are not
guaranteed.

• In contrast of TCP, applications that communicate via datagrams


send and receive completely independent packets of information.
These clients and servers do not have and do not need a
dedicated point-to-point channel. The delivery of datagrams to
their destinations is not guaranteed. Nor is the order of their
arrival.
Java Datagrams
• The java.net package contains three classes to help you write Java programs
that use datagrams to send and receive packets over the network:
– DatagramSocket :
• A datagram socket is the sending or receiving point for a packet delivery service.
• UDP broadcasts sends are always enabled on a DatagramSocket.
– DatagramPacket :
• Datagram packets are used to implement a connectionless packet delivery service.
Each message is routed from one machine to another based solely on information
contained within that packet.
– MulticastSocket :
• A MulticastSocket is a (UDP) DatagramSocket used for sending and receiving IP
multicast packets. .
• A multicast group is specified by a class D IP address and by a standard UDP port
number. Class D IP addresses are in the range 224.0.0.0 to 239.255.255.255,
inclusive. The address 224.0.0.0 is reserved and should not be used.

– An application can send and receive DatagramPackets through a


DatagramSocket.
– In addition, DatagramPackets can be broadcast to multiple recipients all
listening to a MulticastSocket.
Client Operation

create DatagramSocket

Create
DatagramPacket with
Client the server IP and Port

Operation Send packet

Receive packet

Close socket
Server Operation

create bind socket with the Wait for Receiving


DatagramSocket application port packet

Receive packet

Server Send packet to

Operation the IP and port


of the received
packet

Close socket
Java Client
• Import
– java.io.*;
– java.net.*;

• Create DatagramSocket:
– DatagramSocket socket = new DatagramSocket();

• Send Request
– byte[] buf = new byte[256];
– InetAddress address = InetAddress.getByName(String server IP);
– DatagramPacket packet = new DatagramPacket(buf, buf.length, address, server application port);
– socket.send(packet);

• Get Response:
– packet = new DatagramPacket(buf, buf.length);
– socket.receive(packet); // wait till a packet is received
– String received = new String(packet.getData(), 0, packet.getLength());

• Close Socket:
– socket.close();
Java Server

• The server program is implemented by two


classes:
– XServer : which contains the main method for the
server program and performs the work of listening
to the port, receiving, and sending packets.

– XProtocol : which keeps track of the current state


and returns the various responses depending on
the input.
• This object implements the protocol (the language that
the client and server have agreed to use to
communicate).
XServer Class
• Import
– java.io.*;
– java.net.*;

• DatagramSocket:
– DatagramSocket socket = new DatagramSocket (port number the server is going
to listen on);
– Port number must not be already used by any other application

• Listening Thread:
– Create and run a thread to wait for packets to arrive
– Thread t = new Thread(){
– public void run() {
– while (true) {
– //receive request packets
– //do processing using the XProtocol class
– //send response using the received packet IP and port
– }
– }
– };
– t.start();
XServer Class
• Receive request packet
– byte[] buf = new byte[256];
– DatagramPacket packet = new DatagramPacket(buf, buf.length);
– socket.receive(packet);
– String received = new String(packet.getData(), 0, packet.getLength());

• Declare and Initialize the protocol:


– XProtocol xProtocol = new XProtocol();
– buf = xProtocol.processInput(received);
– XProtocol is just a simple class with one function :
• public String processInput(String theInput)
• This function takes an input from the client -> do some processing -> return the output

• Send Response:
– InetAddress address = packet.getAddress();
– int port = packet.getPort();
– packet = new DatagramPacket(buf, buf.length, address, port);
– socket.send(packet);

• Close Socket:
– socket.close();
UDP Datagram Packet Constraint
• The theoretical maximum amount of data for an IPv4 UDP
datagram is 65,507 bytes.
• In practice, On many platforms, the actual limit is more likely to
be 8,192 bytes (8K).
• In fact, many operating systems don't support UDP datagrams
with more than 8K of data and either split, or discard larger
datagrams.
• If a large datagram is too big and as a result the network drops it,
your Java program won't be notified of the problem. (UDP is an
unreliable protocol)
• Consequently, you shouldn't create DatagramPacket objects with
more than 8,192 bytes of data.
• This is a problem for TCP datagrams too, but the stream-based
API provided by Socket and ServerSocket completely shields
programmers from these details.
Datagrams Broadcast Server

• Server does not wait to receive requests it just send packets


to all clients.
• Server send packets to the broadcast IP address of the sub
network
– Ex in network 192.168.1.0/24 the broadcast address
would be 192.168.1.255
– DatagramSocket socket = new DatagramSocket();
– InetAddress all= InetAddress.getByName(“192.168.1.255");
– DatagramPacket packet = new DatagramPacket(buf, buf.length, all, clients listening port
number);
– socket.send(packet);
• Client just receive the broadcasted packets
– DatagramSocket socket = new DatagramSocket(client listening port number);
– byte[] buf = new byte[256];
– DatagramPacket packet = new DatagramPacket(buf, buf.length);
– socket.receive(packet);
– String received = new String(packet.getData(), 0, packet.getLength());
Datagrams Multicast Server

• Server does not wait to receive requests it just send packets


to a group of the clients.
• Server send packets to a multicast IP address
– A multicast address is a class D IP address
– DatagramSocket socket = new DatagramSocket();
– InetAddress group= InetAddress.getByName(“230.0.0.1");
– DatagramPacket packet = new DatagramPacket(buf, buf.length, group, clients listening
port number);
– socket.send(packet);
• Client in the multicast group just receive the packets
– MulticastSocket socket = new MulticastSocket(listening port number);
– InetAddress address = InetAddress.getByName("230.0.0.1");
– socket.joinGroup(address);
– byte[] buf = new byte[256];
– DatagramPacket packet = new DatagramPacket(buf, buf.length);
– socket.receive(packet);
– String received = new String(packet.getData(), 0, packet.getLength());
– socket.leaveGroup(address);
– socket.close();
Assignment

Client Client

Downloads

ImageRetrivalClient.java
Image request (String imageName)
Image response (byte[] image)
ImageRetrivalServer.java
Image
Server

ImageRetrivalProtocol.java ImageRepository
Assignment (Server Side)

• Write an Image retrieval server ImageRetrivalServer.java:


– The Server listen on port 1222
– Start a thread for every client
– Use the Image retrieval protocol to find the requested image
– Send the image to the client
• Write an Image retrieval protocol ImageRetrievalProtocol.java :
– This class has one function :
• public byte[] processInput(String imageName)
• This function takes the image name -> search for the file in a
folder called “”ImageRepository” -> if found read the image and
return its’ byte[] representation otherwise return empty array.
• Hint Check the following :
– FileInputStream
• FileInputStream fis = new FileInputStream("picture.jpg");
• byte[] buffer = new byte[fis.available()];
• fis.read(buffer);
– ObjectOutputStream
• ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream()) ;
• oos.writeObject(buffer);
Assignment (Client Side)

• ImageRetrivalClient.java:
– Connect to server on port 1222
• For testing and debugging of client and server on the
same PC use localhost (127.0.0.1) as the server address.
– Send request for an image
– Read the image retrieved from the server and write
it to a folder called “Downloads”

• Hint Check the following:


– ObjectInputStream
• ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
• byte[] buffer = (byte[])ois.readObject();

– FileOutputStream
• FileOutputStream fos = new FileOutputStream("picture.jpg");
• fos.write(buffer);
Additional References

• Computer Networks a Systems approach 3rd edition (


ISBN: 1-55860-833-8) By Larry L. Peterson and Bruce
S. Davie - Morgan Kaufmann Publishers

• Java Network Programming, Third Edition (ISBN: 0-


596-00721-3) By Elliotte Rusty Harold - O'Reilly

•http://java.sun.com/docs/books/tutorial/networking/inde
x.html

You might also like