Java Unit 9 6-11

Ace your homework & exams now with Quizwiz!

Constructing / Sending a DatagramPacket (Client)

'DatagramSocket socket = new DatagramSocket(); DatagramPacket packet = new DatagramPacket(byte_array, byte_array.length, "www.open.ac.uk", 3000); socket.send(packet);' Note that the socket doesn't contain an address because the packets themselves do and no connection is made. The UDP protocol also requires that the size of the data being sent is specified.

UDP server, receiving Datagrams

'DatagramSocket socket = new DatagramSocket(3000); DatagramPacket packet = new DatagramPacket(byte_array, byte_array.length); socket.receive(packet); System.out.println(new String(packet.getData(), 0, packet.getLength()));' Where receive() is the blocking statement and could be in a loop. Note that the server needs to know the data length ahead of receiving the packet (ie. the fixed length is pre-agreed).

Setting up a BufferedReader to receive data

'InputStream is = socket.getInputStream; BufferedReader br = new BufferedReader(new InputStreamReader(is)); String receivedData = br.readLine();' Note that the byte oriented InputStream is wrapped in a new object InputStreamReader that bridges the byte to character reading.

Setting up a PrintWriter to send data

'OutputStream os = socket.getOutputStream(); PrintWriter toSend = new PrintWriter(os, AUTO_FLUSH);' So the println() and print() methods can be used to send text data.

Server Loop (multiple clients with threads)

'ServerSocket ss = new ServerSocket(3000); while(true) { Socket socket = ss.accept(); Thread connection = new Thread(new ConnectionThread(socket)); connection.start(); }' where ConnectionThread is a class that implements Runnable. A Thread is created for each new socket created by a client connection and *started*.

Socket construction (client example)

'Socket sock = new Socket("catalogue.acme.co.uk", 4000);' Specifies a host address and a port where we hope a server process is listening. This will initiate a connection from the client.

Server Loop (one client at a time)

'while(true) { Socket sock = ss.accept(); ...open streams...process client requests...close streams... sock.close(); }'

loopback address / local host

127.0.0.1 addresses the host computer running this process. Useful for testing a server on the local machine before deploying remotely.

Connection

A *linked* pair of sockets (client and server address each other and their appropriate ports).

UDP Costs / Benefits

A raw packet sending facility that is much less sophisticated than TCP. Doesn't require a connection handshake. Lost or corrupt packets aren't sent again so data may be incomplete. Without the handshaking and data correction speed of transmission should be faster. Good to use when correctness is less important than speed (media streaming) or if data to be transmitted is small (DNS).

Concurrent Connection Costs

A server is limited on the number of concurrent connections it processes only by its memory and processing capabilities. However response time may decrease if many client requests are being processed concurrently. If each connection is accessing a shared resource then caution needs to be taken about synchronising access (to avoid lost updates).

Sending and Receiving data (TCP/IP sockets)

Achieved using streams accessed from a socket and then reading and writing to those streams. E.g. 'InputStream is = socket.getInputStream()' to receive or 'OutputStream os = socket.getOutputStream()' to send data.

Sockets, Streams and .close()

Any streams or sockets that are created and effectively opened, should also be closed when no longer in use. Note that these can give rise to an IOException like many network and stream methods.

Termination Message

As part of a protocol often a message is defined that is sent by the client when it is ready to quit the connection. The server can then stop listening to requests on that connection and close it.

ServerSocket

Class used to *listen for client connections* on a port and *create sockets* that are connected to clients. E.g. 'ServerSocket ss = new ServerSocket(80, 30)' where 80 specified the port that the server listens and 30 is an optional argument to specify the maximum number of clients that can be queued whilst the server is dealing with the current connection.

Connectionless Transport

Communication using datagrams (packets) that use the User Datagram Protocol. Transfer is fast but error prone.

DatagramPacket

Defines objects that will contain the data to be sent using UDP. Are fixed-length byte buffers regardless of if the buffer is filled by the message.

System.exit(0);

Exits and closes down the program with a return code suggesting there were no errors.

FTP

File Transfer Protocol uses two TCP connections, one for control information (21) and the other for data transfer (20). E.g. requests on the control connection might be USER, PASS (authentification) and RETR (retrieve a given file to be sent on the other connection, port 20).

Name Service

Gives information about a resource when provided with the symbolic name of that resource (e.g. it might return the properties of that resource, e.g. gfx card caps).

Server Client Connection Queues

If a client tries to connect to a server whilst it is processing another client then it will be *queued* until the server has finished processing the current client. However, it is possible for a server to process many client connections using *threads*, especially if each client only makes infrequent requests.

Server

In the context of Internet Protocol (IP) networking, a server is a program that operates as a socket listener. In most common use, server is a physical computer (a computer hardware system) dedicated to running one or more *services* (as a host) for clients.

Connection Thread

Object to deal with each client socket, open streams on the socket, process that clients request and close the streams and connection when finished. Likely to implement the Runnable interface and so require a Thread object to run. Servers therefore *separate listening for connections to processing their requests*.

0 to 1023

Range of port numbers reserved for standard internet services.

readLine()

Reads a line from a stream (for example, input stream from a socket), effectively blocking as it waits for the line to arrive / complete.

Trying to connect to a non-existent server

Results in an IOException which needs to be declared or caught in the calling method.

accept()

ServerSocket method that 'blocks' until a client tries to connect on the servers port then *creates a socket linked to the connecting client*. E.g. 'Socket sock = ss.accept()'. Note that this method effectively establishes the connection with the client, the *handshake*.

PrintWriter Auto flush

Specifies that output should be sent immediately after execution of a print() or println() method, not buffered.

Connection Streamed Data Flow

The OuputStream data on one socket is received by the InputStream on the other and vice-verse

DatagramSocket

The class that implements connectionless message sending. Includes mechanisms for launching datagram packets into the internet and for listening to incoming datagrams addressed to a port.


Related study sets

Advertising and Promotion Management

View Set

sq23, sq22, sq21, sq24, sq19, sq18, sq17, sq16, SQ15, sq14, sq13, SQ12, SQ11, sq09 & sq10, sq08, SQ07, SQ06, SQ05, POLI 102

View Set

AP Government Chapter 2 Assignment

View Set

Chapter 1 introduction to nursing

View Set

Ch 24- liability, defenses, and discharge

View Set