Upload
ariel-harmon
View
216
Download
0
Tags:
Embed Size (px)
Citation preview
CSC 480Software Engineering
Socket
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
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.
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
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
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.
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 … }
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
Clean up
Close the connection objects so that they are subject to garbage collection
out.close(); in.close(); aSocket.close();
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.
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); }
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); }
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()));
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; }
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);
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
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)
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
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)
Association – The Bank Server App
BankServer Bank
BankAccountBankService
doService()processCommand()
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
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
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
Association – The Bank Client App
BankClientMain BankClientFrame
BankClientConnector
request(String cmd):String
A Sample Screenshot
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