IT495 Network Programing-Slides
IT495 Network Programing-Slides
IT495 Network Programing-Slides
High-Speed Networks
Part 8: Network Programming
• Network Programming
– Network API.
– Protocol Implementation issues.
• Sockets
– TCP and UDP.
– Client & Server application example.
• Assignment.
Introduction to Network programming
Application
Network API
– process-per-protocol model
– process-per-message model
Process-per-Protocol model
– (context switching)
Process-per-Message model
• 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.
• 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
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);
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.
• 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
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
create DatagramSocket
Create
DatagramPacket with
Client the server IP and Port
Receive packet
Close socket
Server Operation
Receive 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
• 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());
• 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
Client Client
Downloads
ImageRetrivalClient.java
Image request (String imageName)
Image response (byte[] image)
ImageRetrivalServer.java
Image
Server
ImageRetrivalProtocol.java ImageRepository
Assignment (Server 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”
– FileOutputStream
• FileOutputStream fos = new FileOutputStream("picture.jpg");
• fos.write(buffer);
Additional References
•http://java.sun.com/docs/books/tutorial/networking/inde
x.html