Presentation of practical exercise 1 - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Presentation of practical exercise 1

Description:

TCP is a transport layer protocol ... catch (IOException ioe) { ioe.printStackTrace(); System.exit(1); private void connect ... catch (IOException ioe) ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 32
Provided by: ottarvik
Category:

less

Transcript and Presenter's Notes

Title: Presentation of practical exercise 1


1
Presentation of practical exercise 1
  • TCP/IP communication
  • in Java using Sockets

2
TCP/IP connections
  • TCP is a transport layer protocol providing a
    reliable stream communication channel between two
    connection points.
  • The connection points are often referred to as
    sockets.
  • TCP is implemented over the network layer IP
    protocol, hence the name TCP/IP connection.
  • TCP/IP connections must be opened before any data
    may be transferred. When data transfers are
    complete, it must be closed.

3
Sockets
  • The socket abstraction describes an endpoint of a
    TCP/IP connection.
  • A socket is described by an internet address (a
    DNS hostname or an IP address) and a port number.
  • A TCP/IP connection consists of two connected
    sockets (usually on separate computers).
  • A socket belongs to an application and is the
    applications access point to the TCP/IP
    connection.
  • Sockets are also used to describe access points
    to other protocols than TCP/IP.

4
Creating a TCP/IP connection
  • When establishing a connection between two
    processes, one process must play the client role
    and the other plays the server role.
  • The server process creates a server socket bound
    to a given port on the server process computer,
    and then invokes the accept() / listen() routine.
  • The client process creates a socket on a free
    port of his computer, and attempts to connect
    this socket to the server.
  • The accept() routine detects the connection
    attempt and creates a new socket on a free port
    of the server computer to serve as the second
    endpoint of the new connection. The server
    process can then continue to accept connections
    on the original port, if it so desires.
  • Once the connection has been established between
    the two processes, they can abandon the
    client/server roles and act as peers.

5
A Java example
Server
Client
private void acceptConnections() try
ServerSocket serverSocket new
ServerSocket(myPort) do Socket s
serverSocket.accept() ClientHandler ch
new ClientHandler(this, s) while
(true) catch (IOException ioe)
ioe.printStackTrace() System.exit(1)
private void connect() hubSocket new
Socket(hubAddress, hubPort)
  • The purple socket is used to accept new
    connections.
  • The red sockets form a new connection.
  • The blue object is a new thread taking care of
    the new connection at the server side.

6
Using a TCP/IP connection
  • Once a connection has been established, it can be
    used to transmit data between processes.
  • The sockets are connected by two streams, one in
    each direction. These are used to transfer data.
  • To transmit data, a process writes data to its
    sockets output stream.
  • To receive data, a process reads data from its
    sockets input stream.
  • One sockets input stream equals the other
    sockets output stream.

7
Sending data
  • The method getOutputStream() gives access to a
    sockets output stream.
  • Encapsulate the OutputStream with an
    ObjectOutputStream to transmit serialized Java
    objects.
  • Use the OutputStream class directly to transfer
    byte data.
  • Encapsulate the OutputStream with an
    OutputStreamWriter to transfer character data.
  • Example Sending serialized Java objects.

/ Sends a message object over a given
socket. Serializes the message object and
writes it to the socket's output stream.
_at_param s The socket to transfer the message
over. _at_param m The message to transmit.
/ public static void sendMessage(Socket s,
Message m) try ObjectOutputStream oos
new ObjectOutputStream(s.getOutputStream())
oos.writeObject(m) oos.flush() catch
(IOException ioe)
8
Receiving data
  • The method getInputStream() gives access to a
    sockets input stream.
  • Encapsulate the InputStream with an
    ObjectInputStream to receive serialized Java
    objects.
  • Use the InputStream class directly to receive
    byte data.
  • Encapsulate the InputStream with an
    InputStreamReader to receive character data.
  • Example Receiving serialized Java objects.

/ Receives a message on a given socket.
Receives a bit stream from the socket and
deserializes it to a message object. This method
will block the calling thread until a message
has been received. _at_param s The socket to
receive a message on. _at_return The message
object that was received. / public static
Message receiveMessage(Socket s) try
ObjectInputStream ois new ObjectInputStream(s.ge
tInputStream()) return (Message)ois.readObjec
t() catch (Exception e) return null
9
Closing a connection
  • When a connection is no longer needed, close it
    using the close() method in the Socket class.
  • When one process closes its socket, the
    connection is broken and the other socket cannot
    be used for communication (a SocketException will
    be thrown).
  • Closing the sockets will lead to the associated
    streams being closed.

10
The exercise
  • Complete a file sharing system consisting of a
    Hub and numerous clients.
  • The clients have client roles with respect to the
    Hub but act as peers with respect to each other.
  • Each client has a TCP/IP connection with the Hub,
    used to exchange information about the
    whereabouts of other clients.
  • Each client is sharing a list of files and is
    listening for connections on an advertised
    address and port number.
  • Clients can contact other clients in order to
    download shared files. This is done by connecting
    to that clients advertised address and port
    number.
  • The file is transferred over the established
    TCP/IP connection and the connection is then
    closed.

11
The system
Hub parameters
Double-click a user to see his files
Client parameters
12
The system
Downloads are listed here
Double-click a file to download it
13
Partial solution
  • A GUI, the Hub, and parts of the Client are
    implemented.
  • The GUI functionality is defined in the Gui
    interface, and the functionality expected from
    the client is defined in the FileSharer
    interface.
  • Message passing is done by transferring
    serialized Message objects.

14
Message passing
  • The Message object consists of
  • A message type (int)
  • The content (Object, must be serializable)
  • An optional error message (String)
  • The sender ID (User)
  • Messages are used to coordinate the actions of
    the system participants.
  • Since all messages are Message objects, messages
    can be received without the recipient knowing
    what kind of message to expect.

15
Hub Client communication
  • The Client connects to the Hub.
  • The client sends a LOGIN message, with its own
    user description (nickname, internet address,
    port number) attached.
  • The Hub responds with a LOGIN_ACCEPTED message,
    with a list of all logged in users attached.
  • Alternatively, the Hub responds with a
    LOGIN_DENIED message, with a clarifying error
    message attached.
  • If the login was accepted, the connection is kept
    open.
  • Whenever users log in or out, an
    UPDATED_CLIENT_LIST message is sent from the Hub
    to all logged in clients, with an updated list of
    clients attached.
  • When a user wants to log out, he sends a LOGOUT
    message to the Hub, and closes the connection.

16
Client Client communication
  • The client connects to another client.
  • The connecting client sends a REQUEST_FILE_LIST
    message, to indicate that it wants the other
    clients list of shared files.
  • The other client responds with an
    UPDATED_FILE_LIST message, with the list of
    shared files attached. (This list can be acquired
    through the getMyFileList() method in the Gui
    interface.)
  • The client that requested the connection closes
    it again.

17
Client Client communication
  • The client connects to another client.
  • The connecting client sends a REQUEST_DOWNLOAD
    message, to indicate that it wants to download a
    shared file from the other client. Attached to
    the message is a SharedFile object indicating
    which file is wanted.
  • The other client responds with a DOWNLOAD_DENIED
    message, with a clarifying error message. The
    requesting client closes the connection.
  • Alternatively, the other client responds with a
    DOWNLOAD_GRANTED message. The requested file is
    then transferred over the connection. Both
    clients will need to create a new thread to take
    care of this transfer, in order to avoid blocking
    the system for the duration of the file transfer.
    (Remember, file transfers may take a long time.)
  • When the file transfer is complete, the
    connection is closed.

18
Your task (optional)
  • Implement the part of the program dealing with
    peer to peer communication between clients.
  • Demonstrate the program to one of the assistants
    if you want to.

19
Practical considerations
  • The computers that are reserved for these
    exercises are actually terminal clients.
  • This means that everybody is sharing a limited
    number of terminal servers.
  • Everybody using the same terminal server will
    have the same IP address.
  • Consequently, to avoid conflicts we must ration
    the use of port numbers.
  • Every group will have exclusive rights to use
    port numbers 3000n10, 3009n10 where n is
    your group number!

20
Hints
  • Take advantage of examples and information given
    in this presentation.
  • You need a separate thread for each active upload
    and download.
  • You can run the handed out code by running the
    P1.bat batch file.
  • The code that remains to be implemented is
    similar to the code that has already been
    implemented in many ways.

21
Clients invoke individual server
22
A distributed application based on peer processes
23
Thin clients and compute servers
Compute server
Network computer or PC
Application
network
Thin
Process
Client
24
Processes and Communication channels
Socket bind to 192.168.2.13000
Process B2
Process A1
Port 3000
Port 4586
Outgoing Message Buffer
InputStream
Send
Receive
Communication channel
Send
Receive
Incoming Message Buffer
OutputStream
InputStream
Socket bind to 162.188.20.24586
Computer A Internet Address 192.168.2.1
Computer B Internet Address 162.188.20.2
25
File Sharing System
Computer Hub IP address 129.241.107.128
A3012 B3015 C3019
Hub Server Process
Port 3010
login logout
Port 3015
Port 3019
Port 3012
Peer-to-Peer
Peer-to-Peer
download upload
Client
Client
Client
Computer A IP Address 129.241.139.100
Computer B
Computer C
26
File Sharing System
Computer Server IP address 129.241.107.128
A3011 B3015
Hub Server Process
Port 3010
129.241.107.1283010
login logout
129.241.107.1283015
Peer-to-Peer
129.241.107.1283011
Port 3015
Port 3011
download upload
Client
Client
Process A
Process B
27
Protocol layers in the ISO Open Systems
Interconnection (OSI) model
28
Sockets and ports
29
Internetwork layers
30
TCP/IP layers
31
Client and server with threads
Write a Comment
User Comments (0)
About PowerShow.com