Download presentation
Presentation is loading. Please wait.
Published byPolly Quinn Modified over 6 years ago
1
Object-Orientated Analysis, Design and Programming
Presentation by Dr. Phil Legg Senior Lecturer Computer Science 10: Sockets and Datagrams Autumn 2016
2
Network Applications Network application consists of client program and server program (usually) on different hosts. The client process and the server process communicate with each other through their sockets. See the Oracle Tutorial for more details
3
Socket as an Interface Socket: a door between application process and end-end-transport protocol (UDP or TCP)
4
Stream mode sockets and datagram sockets
Sockets are the interface (“doors”) between the application process and the transport layer. A socket programming construct can use either the UDP or TCP protocol. Sockets that use UDP for transport are known as datagram sockets, while sockets that use TCP are termed stream sockets or just socket.
5
Stream mode socket (using TCP)
TCP provides reliable, in-order transfer of bytes ("pipe") between client and server. A pair of (data) sockets in client and server are connected by a pair of streams, one in each direction. Each socket has an input stream and an output stream. To send information from one process to the other, the sending process writes the information to its output stream, and the other process reads from its input stream.
6
Stream mode socket (using TCP)
7
Client and server interaction using TCP socket
Server must be running server process must first be running server must have created server socket that welcomes client’s contact Client contacts server by: creating client-local TCP socket specifying address, and port number of server process When client creates socket: client TCP establishes connection to server TCP When contacted by client, server TCP creates new socket for server process to communicate with client
8
Java stream-mode Socket API
In Java, the stream-mode socket API is provided with two classes: ServerSocket: used on the server for accepting connection requests; we will call an object of this class a connection socket. Socket: used on both processes for data exchange; we will call an object of this class a data socket.
9
Key methods in the ServerSocket class
ServerSocket(int port) constructor creating an instance of the server socket (i.e. the “welcome socket” or “listening socket”) Socket accept() listens for a connection request and accepts it and returns an instance of a socket. A blocking operation. void close() closes this server socket
10
Key methods in the Socket class
Socket(String addr, int port) constructor creates a socket and connects to addr and port void close() closes the socket InputStream getInputStream() returns an input stream so that data may be read from the socket. A read operation on the InputStream is blocking. OutputStream getOutputStream() returns an output stream so that data may be written to the socket. A write operation on the OutputStream is nonblocking.
11
Client/Server program structure
12
Client/Server in Java (1)
Server creates listening socket ServerSocket welcomeSocket = new ServerSocket(6789); Server waits for income connection request Socket serverDataSocket = welcomeSocket.accept(); Client requests a connection to the server Socket clientDataSocket = new Socket("hostname", 6789);
13
Client/Server in Java (2)
Set up InputStream from a socket BufferedReader socketInput = new BufferedReader( new InputStreamReader( clientDataSocket.getInputStream())); Receive message from the other process via socket Inputstream String message = socketInput.readLine();
14
Client/Server in Java (3)
Set up OutputStream from a socket PrintWriter socketOutput = new PrintWriter( new OutputStreamWriter( clientDataSocket.getOutputStream()); Send message to the other process via socket Outputstream socketOutput.println(message);
15
Templates for Acceptor (Server) and Requestor (Client)
16
In-Class Activity Develop two simple programs (a Client and a Server). Use the templates from the previous slide as a guide. The client should be able to connect to the server, and send a message. The server should receive the message, reverse this, and return it to the client. // To reverse a String s, you may use the following line String result = new StringBuffer(s).reverse().toString();
17
Server - key concepts int portNo = 12345;
String message = "Hello from server"; ServerSocket connectionSocket = new ServerSocket(portNo); Socket dataSocket = connectionSocket.accept(); OutputStream outStream = dataSocket.getOutputStream(); PrintWriter socketOutput = new PrintWriter(outStream); socketOutput.println(message); dataSocket.close( ); connectionSocket.close( );
18
Client - key concepts String acceptorHost =“localhost";
int acceptorPort = 12345; Socket mySocket = new Socket(acceptorHost,acceptorPort); InputStream inStream = mySocket.getInputStream(); BufferedReader socketInput = new BufferedReader( new InputStreamReader(inStream)); String message = socketInput.readLine( ); mySocket.close( );
19
Acceptor and Request Example
First run ‘Acceptor’ to initialize server Then, if we run ‘Receptor’ as a client, it will receive the message from the server
20
In-Class Activity Extend the Server to accept multiple Clients.
You may want a loop that can establish a connection and then wait for new connections again
21
In-Class Activity Develop a chat system that could be used for the UWEfilm online booking system
22
Datagrams A socket programming construct can use either the UDP or TCP protocol. Stream sockets for TCP and Datagram sockets for UDP. The datagram socket API supports the exchange of discrete units of data Datagram sockets usually are used to support connectionless communication No need to establish connection But each time we need to include the receiver’s address
23
Java Datagram Socket API
DatagramSocket class for the sockets. Sending and receiving DatagramPacket class for the datagram exchanged. Receiver’s address Data
24
DatagramSocket and DatagramPacket
25
DatagramSocket DatagramSocket() used if no need to receive datagram
DatagramSocket(int port) used if need to receive packet. The port number can then be specified in the datagram packet sent by the other process. void send(DatagramPacket p) to send the datagram packet p void receive(DatagramPacket p) object referred to by p will hold the packet void setSoTimeout(int timeout) Sets a timeout period for the blocking receive operations
26
DatagramPacket DatagramPacket(byte[]buf, int l)
used for received packet packet length upto l; data will be stored in the array buf DatagramPacket(byte[]buf,int l, InetAddress rcvAddr, int port) used for packet to be sent; buf contains the data, rcvAddr and port identifies the receiver.
27
Sending a Datagram in Java
The sender process: creates a DatagramPacket object that represents the datagram itself. This object carries the payload data as a reference to a byte array, the destination address (the host ID and port number to which the receiver’s socket is bound). Creates a DatagramSocket, and issues a call to the send method in the DatagramSocket object, specifying a reference to the DatagramPacket object as an argument
28
Receiving a Datagram in Java
In the receiving process, a DatagramSocket object must also be instantiated and bound to a local port. To receive datagrams sent to the socket, the process creates a DatagramPacket object which references a byte array Calls the receive method in the DatagramSocket object, specifying as argument a reference to the DatagramPacket object.
29
Example – Sending ‘hello’
Sender – version 1 Create a datagram socket Put the message “Hello!” in a byte array Create a datagram packet with the receiver’s address and port number, and a reference to the byte array Send datagram in the packet through the datagram socket Receiver – version 1 Create a datagram socket bound to a port (e.g.12345) Create a datagram packet includes a reference to a byte array Receive a data packet through the datagram socket
30
Code Examples
31
Working with Datagram After receiving a datagram packet,
datagram.getAddress(); //returns address of datagram packet sender datagram.getPort(); //returns port of sender Issues with Datagrams Sockets normally provide non-blocking send and blocking receive The method receive blocks until a datagram packet is received as seen in Receiver, unless it sets a time out period. To avoid infinitely blocked, use timeouts Size of the buffer for holding the data
32
In-Class Activity Extend the hello example so that receive can reply to the sender You will need to get the sender’s address and port number from the datagram, and then construct a new datagram and the original sender’s address. The sender will need to be modified to accept a response back from the receiver.
33
Summary Working with datagram (UDP) sockets and stream (TCP) sockets
Client / server interactions Working with client and server interactions in Java
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.