Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CSC 480 Software Engineering Socket

2 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

3 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.

4 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

5 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

6 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.

7 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 … }

8 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

9 Clean up Close the connection objects so that they are subject to garbage collection out.close(); in.close(); aSocket.close();

10 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.

11 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); }

12 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); }

13 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()));

14 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 client while ((inputLine = in.readLine()) != null) { outputLine = kkp.processInput(inputLine); out.println(outputLine); if (outputLine.equals("Bye.")) break; }

15 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);

16 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

17 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)

18 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

19 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)

20 Association – The Bank Server App BankServerBank BankAccountBankService doService() processCommand()

21 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

22 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

23 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

24 Association – The Bank Client App BankClientMainBankClientFrame BankClientConnector request(String cmd):String

25 A Sample Screenshot

26 A Possible Approach Set up environment and run the whole application Read the existing code base Modify the client app and the server app separately  The server team may take over the connector class from the client team Integrate and test


Download ppt "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."

Similar presentations


Ads by Google