Presentation is loading. Please wait.

Presentation is loading. Please wait.

Echo Networking COMP 112 2017.

Similar presentations


Presentation on theme: "Echo Networking COMP 112 2017."— Presentation transcript:

1 Echo Networking COMP

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

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 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 (“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 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 Addressing a connection
Client 1 2 : 80 6667 Server 1 2 : 80 6667 xx xx 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 Application is still listening!

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

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); } Socket socket = new Socket("irc.ecs.vuw.ac.nz", PORT); …set up Scanner and PrintStream and communicate via socket Server port Client Server port

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);} For ever Waits Waits

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

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

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

13 Your IRC client Will be larger than the echo client BUT
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: User inputting a message IRC sending you a message Coded as asynchronous send and receive.


Download ppt "Echo Networking COMP 112 2017."

Similar presentations


Ads by Google