26
CSC 480 Software Engineering Socket

CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Embed Size (px)

Citation preview

Page 1: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

CSC 480Software Engineering

Socket

Page 2: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

What is Socket?

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 server side of the

connection ServerSocket – that implement the client side of

the connection

Page 3: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

The Socket Server

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.

Page 4: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

The Socket Client

The client knows the hostname of the machine on which the server is

running, and the port number to which the server is connected

To make a connection request, the client tries to rendezvous with the server on the server's machine and port.

http://java.sun.com/docs/books/tutorial/networking/sockets/index.html

Page 5: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Communication through Socket

Upon acceptance, the server gets a new socket bound to a different port

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

Page 6: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Client’s Responsibilities

Open a socket. Open an input stream and output stream to the

socket. Read from and write to the stream according to

the server's protocol. Close the streams. Close the socket.

Page 7: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Set up Connection

Open a socket. an input stream and output stream to the socket.

try{ aSocket = new Socket(hostName, portNumber); out = new PrintWriter(aSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(

aSocket.getInputStream())); } catch (IOException ex) { //error handling … }

Page 8: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Talk Based on Protocol

Using the out writer object to read text messages

out.println(userInput);

in.readLine();

Using the in reader object to print text messages

Page 9: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Clean up

Close the connection objects so that they are subject to garbage collection

out.close(); in.close(); aSocket.close();

Page 10: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Server’s Responsibilities

Open a ServerSocket Wait until a client request arrives

Accept the client open an input stream and output stream designated

to this client Read from and write to the stream according to

the server's protocol. Close the streams, when shutting down Close the socket.

Page 11: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Open a ServerSocket

Open a ServerSocket which listens to a designated port

Avoid using well-known port numbers, such as 80 for web servers

try { serverSocket = new ServerSocket(4444); } catch (IOException e) { System.out.println("Could not listen on port: 4444"); System.exit(-1); }

Page 12: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Accept a Client Request

Waiting for client request and accept it

Socket clientSocket = null; try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.out.println("Accept failed: 4444"); System.exit(-1); }

Page 13: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Set up Connection to a Client

Gets the socket's input and output stream and opens readers and writers on them. To facilitate text-based communication

PrintWriter out = new PrintWriter( clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream()));

Page 14: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Client-Server Communication

Design pattern: generate response by invoking the processInput method of the protocol class

// initiate conversation with client KnockKnockProtocol kkp = new KnockKnockProtocol(); outputLine = kkp.processInput(null); out.println(outputLine);

// conduct conversation with client until the finish is signaled by clientwhile ((inputLine = in.readLine()) != null) { outputLine = kkp.processInput(inputLine); out.println(outputLine); if (outputLine.equals("Bye.")) break; }

Page 15: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

KnockKnockProtocol

This class provides All constants for this application Clues and corresponding answers The processInput method which

Reply with an answer if theInput is in the expected format Reply with the expected input

public String processInput(String theInput);

Page 16: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Team Formation

The whole class is divided into 3 teams Each team has

2 developers works in pair on the client app in the Windows (or RedHat Linux) environment

2 developers works in pair on the server app on Cobra

Page 17: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Part I – The Knock Knock App

Build up confidence Activities

Set up environment for server and client Compile and execute Add a couple of new clue-answer pairs (optional)

Page 18: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Part II – A Simple ATM App

Purpose: upgrade an existing client-server application to include a GUI

Code bases available A bank application with a client and a server talking

through socket A simple GUI app managing phone directory entries,

A working GUI ready for modification A method facilitating file access

Page 19: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

The Bank Server App

The server app includes A BankServer class (with main)

A Bank class (holds an array of BankAccounts)

A BankService class (takes charge of socket communication)

A BankAccount class (facilitates balance, deposit, and withdraw)

Page 20: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Association – The Bank Server App

BankServer Bank

BankAccountBankService

doService()processCommand()

Page 21: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

The Existing GUI

A list to hold names Two text fields to display

and accept a name and a number

Three buttons for clearing fields, and add and delete the current entry

A method accessing loading entries from a file

Page 22: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Tasks – client side

Develop a bank client with a GUI Change the List to a TextArea to display messages

from server Change the labels Name and Number to Account #

and Amount, respectively Change the button labels to Balance, Deposit, and

Withdraw, respectively; and change the control logic Write a class designated to socket connection with

the server

Page 23: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Tasks – server side

Modify the server so that account information can be read from a file Write a utility class which has a static method

designated for file accessing, or Write a method for BankServer to access the account

file Create a file with 10 account records

Page 24: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

Association – The Bank Client App

BankClientMain BankClientFrame

BankClientConnector

request(String cmd):String

Page 25: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

A Sample Screenshot

Page 26: CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network

A Possible Approach

Set up environment and run the whole application

Read the existing code base Modify the client app and the server app

separatelyThe server team may take over the connector

class from the client team Integrate and test