Presentation is loading. Please wait.

Presentation is loading. Please wait.

School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP 112 2016.

Similar presentations


Presentation on theme: "School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP 112 2016."— Presentation transcript:

1 School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP 112 2016

2 COMP112 22: 2 Menu Review Networking Protocol stack & abstraction Scocket as point to point “wire” Sever Client Echo Server Client Code Comunication Portocol Admin

3 COMP112 22: 3 Getting a Socket Connection requires each computer to set up a socket connected to the other How do you specify where you want the connection to go to? Which program running on which computer is going to serve you a web page? Which program goes first? Need a connection at the other end in order to set up the one at this end!

4 COMP112 22: 4 Addressing a connection Two parts to the address: host computer: IP address, (can be specified by Host name) “port” (an integer) Each computer on the network must have an IP address. 130.195.5.9 (“debretts.ecs.vuw.ac.nz”) Operating system provides a set of “ports” that connections can be associated with. Many programs have a standard port number. Application can specify a remote host and a port. Can hope the program at the other end will listen 1 2 : 80 : 6667 1 2 : 80 : 6667

5 COMP112 22: 5 Who goes first? If application requests connection to a port on a remote host, but there is no application on that port at the remote host that will respond to the request, connection will fail. Need to have an application on remote host listening to the port before a client requests connection: Client – Server model: If host provides some service: Server application runs on a remote host Server application listens on a known port Client application can request a connection to host : port Server application will hear, and can accept the request Operating systems will set up a new socket on each end with a connection between them.

6 COMP112 22: 6 Addressing a connection Client 1 2 : 80 : 6667 Server 1 2 : 80 : 6667 Listening on port 6667 Request connection to port 6667 on Server via port xx Application accepts connection OS creates socket connected to port xx on Client OS creates socket connected to port 6667 on Server xx Application is still listening!

7 COMP112 22: 7 Echo Service - Very Simple The Echo Server provides a Service to the Client Echo server will echo back any message sent to it: ClientServer Client Hello there. Are you listening Echo: Hello there. Are you listening I want help Echo: I want help

8 COMP112 22: 8 Connecting Client and Server try { ServerSocket serverSocket = new ServerSocket(PORT); while (true) { Socket socket = serverSocket.accept(); … set up Scanner and PrintStream and communicate via socket } } catch (IOException e){System.out.println("Failed connection "+ e); } try { Socket socket = new Socket("irc.ecs.vuw.ac.nz", PORT); …set up Scanner and PrintStream and communicate via socket } catch (IOException e){System.out.println("Failed connection "+ e); } Server port Server Client

9 COMP112 22: 9 Echo Client. public static final String SERVER = "localhost"; public static final int PORT = 6667; public static void main(String[ ] args) { try { Socket socket = new Socket(SERVER, PORT); Scanner input = new Scanner(socket.getInputStream()); PrintStream output = new PrintStream(socket.getOutputStream(), true); while (true) { String toSend = UI.askString(">"); output.println(toSend); if (toSend.equals("QUIT")){ UI.quit(); } String rcvd = input.nextLine(); UI.println(rcvd); } }catch(IOException e){UI.println("IO failure "+ e);} } Waits For ever

10 COMP112 22: 10 Client 1.Wait for user input 2.Send to server 3.Wait for echo 4.Display to user Server 1.Wait for client 2.Send echo This Service might be very simple but: Switching the operation order and the system fails to function. Client and Server C 1 Wait for user input 2 send to server 3 Wait for echo 4 display to user S 1 wait for client 2 send echo

11 COMP112 22: 11 What dose the server look like if there are two clients Two Clients and what Server? B Wait input send to server Wait echo Display S A Wait input send to server Wait echo Display Receive from AReceive from B Send to A Send to B Receive from A Receive from B Send to A Send to B

12 COMP112 22: 12 One server many clients What are the requirements that the server must satisfy? 1.A always goes before B 2.B always goes before A 3.Any order including only A and never B. 4.…… How do you write code that satisfies these requirements?

13 COMP112 22: 13 Your IRC client Will be larger than the echo client Will be more than just a main method Needs a richer interface (buttons, etc) Needs to deal with messages better. Needs a more complex logging in. BUT The basic ideas are mostly present in the echo client EXCEPT for any order response between: 1.User inputting a message 2.IRC sending you a message Coded as asynchronous send and receive.


Download ppt "School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP 112 2016."

Similar presentations


Ads by Google