Download presentation
Presentation is loading. Please wait.
Published bySilvia Caldwell Modified over 9 years ago
1
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP 112 2015
2
COMP112 22: 2 Menu Networking 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 Setting up the sockets 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 portServer ClientServer port
8
COMP112 22: 8 Echo Service 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
9
COMP112 22: 9 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
10
COMP112 22: 10 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
11
COMP112 22: 11 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 Switching the order of the server operation 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
12
COMP112 22: 12 What dose the server look like if there are two clients Two Clients and what Server? C Wait input send to server Wait echo Display S C Wait input send to server Wait echo Display
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 asynchronous send and receive.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.