Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 501N Fall ‘09 22: Introduction to Networking November 24 2009 Nick Leidenfrost.

Similar presentations


Presentation on theme: "CSE 501N Fall ‘09 22: Introduction to Networking November 24 2009 Nick Leidenfrost."— Presentation transcript:

1 CSE 501N Fall ‘09 22: Introduction to Networking November 24 2009 Nick Leidenfrost

2 2 Lecture Outline Networking  Java resources for networking  Client-server architecture  Protocols

3 3 Networking Overview Java Program Networking Infrastructure Networking Infrastructure Hardware wire

4 4 Networking Overview Sending data from A to B is not easy  The wire is not reliable Due to all kinds of interference  Have to develop protocols Agree on a format for the data Agree on who talks when Agree on what additional information needs to be supplied  2 Kinds of protocols: Network Protocols Application Protocols  What data your application sends across the network, what it expects to receive as a result, etc.  Specific to each application

5 5 Packets Data that is sent between two computers is sent in bundles called packets In addition to the data being sent, packets also carry information about the sender and the recipient needed by low level protocols

6 6 Protocols Common protocols  Internet Protocol (“IP”) Unreliable data transmission protocol Cannot guarantee packets will arrive in order, or at all  Transmission Control Protocol “TCP” Reliable data transmission protocol Ordered delivery Operates over IP Uses a system of acknowledgements, or “ACKs”  Datagram Protocol “UDP” Unreliable (loss / duplication possible) Operates over IP Streaming media, Multiplayer online games, VOIP  Applications willing to sacrifice reliability for speed

7 7 Seems like a lot of work! We are essentially trying to send data reliably over an unreliable channel …But there is help for programmers  TCP/IP makes the channel reliable  Abstraction of the network means we don’t have to deal with low level things like packets and can write at a high level  Java provides resources that allow you to send data easily Contained in the java.net package

8 8 What do we need to send data? The destination of the data:  IP address  Port The actual data being sent:  Has to be Serializable Capable of being sent over a stream Primitives ( int s, double s, etc) Objects of classes that implement java.io.Serializable

9 9 IP Address The IP address is a sequence of 4 numbers  Each number is a byte E.g. 128.252.19.34  The special address 127.0.0.1 always points to your own machine To send data to B, A needs to know B's Internet address (Relatively) Recently, IPv6 was introduced  Uses 16 bytes instead of 4  (Many, many) More addresses  We will focus on IPv4

10 10 Port Numbers One computer can offer multiple services over the Internet  For example, both a web server program and an email server program When data packets are sent to that computer, they need to indicate which program is meant to receive and handle the data

11 11 Port Numbers We use port numbers to differentiate networking applications on a computer  In TCP, a port number is an integer between 0 and 65,535  The sending program must know the port number of the receiving program  Ports numbers are divided into 3 categories Well Known (Reserved): 0 - 1023  21: FTP  22: SSH  23: Telnet  80: HTTP Registered (registered with IANA): 1024 - 49151 Dynamic / Private: 49,152 – 65,535

12 12 A Client Program – Sockets A socket is an object that encapsulates a TCP/IP connection There is a socket on both ends of a connection Syntax to create a socket in a Java program If it can't find the host, the Socket constructor throws an UnknownHostException Socket s = new Socket(hostname, portnumber);

13 13 A Client Program – Input and Output Streams Use the input and output streams attached to the socket to communicate with the other endpoint Code to obtain the input and output streams InputStream instream = s.getInputStream(); OutputStream outstream = s.getOutputStream();

14 14 A Client Program – Input and Output Streams When you send data to outstream, the socket forwards them to the server The socket catches the server's response and you can read it through instream When you are done communicating with the server, close the socket s.close();

15 15 Client and Server Sockets Differentiating Input and Output streams with Sockets can be a bit confusing Remember: We read from an input stream and write to an output stream

16 16 More friendly Stream Interaction Scanners and Writers InputStream and OutputStream send and receive bytes To send and receive text, or primitive data use a Scanner and a Writer Scanner in = new Scanner(instream); PrintWriter out = new PrintWriter(outstream);

17 17 More friendly Stream Interaction A PrintWriter buffers the characters and only sends when the buffer is full  Buffering increases performance When sending a command, you want the whole command to be sent immediately  Flush the buffer manually: out.print(command); out.flush();

18 18 A Server Program Sample server program: enables clients to manage bank accounts in a bank When you develop a server application, you need some application-level protocol The client can use this protocol to interact with the server A simple bank access protocol is described on the next slide

19 19 Simple Bank Access Protocol Client Request Server Response Meaning BALANCE int n The balance as a double Get the balance of account n DEPOSIT int n double a The new balance as a double Deposit amount a into account n WITHDRAW int n double a The new balance as a double Withdraw amount a from account n QUIT noneQuit the connection

20 20 A Server Program The server waits for clients to connect on a certain port  Use a number in the Dynamic / Private range …say, 50,000 To listen for incoming connections, use a ServerSocket ( java.net ) To construct a ServerSocket, just provide the port number you want to accept connections on ServerSocket server = new ServerSocket(50000);

21 21 A Server Program Use the accept method to wait for client connection and obtain a socket accept blocks until a program tries to connect to the ServerSocket Blocking methods: Methods which halt control flow until some external cue allows them to proceed  External cue: Connection from remote client Signal from another Thread (More on this next time) ServerSocket server = new ServerSocket(portNum); Socket s = server.accept(); BankService service = new BankService(s, bank); service.run();

22 22 A Server Program – BankService BankService carries out the service requested by the client Implements the Runnable interface Its run method will be executed in each thread that serves a client connection

23 23 A Server Program – BankService run gets a scanner and writer from the socket, then executes: public void doService() throws IOException { while (true) { if (!in.hasNext()) return; String command = in.next(); if (command.equals("QUIT")) return; executeCommand(command); } }

24 24 A Server Program – executeCommand Processes a single command If the command is DEPOSIT, it carries out the deposit WITHDRAW is handled in the same way int account = in.nextInt(); double amount = in.nextDouble(); bank.deposit(account, amount);

25 25 A Server Program – executeCommand After each command, the new balance is sent to the client: out.println(bank.getBalance(account));

26 26 A Server Program The run method executes until the client closes the connection or the command equals QUIT  How can we support multiple simultaneous clients?  Spawn a new thread whenever a client connects  Each thread is responsible for serving one client

27 27 A Server Program – Threads BankService implements Runnable ; so, it can start a thread using start() (of class Thread ) The thread dies when the client quits or disconnects and the run method exits

28 28 A Server Program – Threads In the meantime, BankServer loops back to accept the next connection The server program never stops When you are done running the server, you need to kill it while (true) { Socket s = server.accept(); BankService service = new BankService(s, bank); Thread t = new Thread(service); t.start(); }

29 29 Sending Objects Sometimes, you may want to send actual Java objects across the wire  Easier than sending textual representations of all data  Once the object is received at the other end, methods can be called and state accessed To do this, wrap the socket’s streams with object streams instead of using Scanner and PrintWriter

30 30 Object Stream Creating an object stream - client side Socket s = new Socket(“128.252.19.34”, 20000); OutputStream os = s.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); LinkedList list = new LinkedList(); oos.writeObject(list);

31 31 Object Stream Creating an object stream - server side readObject() returns an Object  Must know what type of object you are expecting to perform the correct cast Protocol definition becomes important ServerSocket ss = new ServerSocket(20000); Socket s = ss.accept(); InputStream is = s.getInputStream(); ObjectInputStream ois = new ObjectInputStream(is); LinkedList list = (LinkedList)ois.readObject();

32 32 Sending a message to multiple hosts: Multicasting NOTE: For your reference only. Probably not the best choice for your Labs. Need to use a special type of Socket called MulticastSocket  This type of socket does not support TCP/IP  It is built to support the User Datagram Protocol (UDP) UDP is not as powerful as TCP/IP  Used for sending short messages (datagrams)  Unreliable connection Out of order delivery, No guarantees  Cannot support objects

33 33 Using MulticastSockets Code to send a message  Group addresses must be between 228.0.0.1 - 239.255.255.255 The system knows to treat these addresses as multicast addresses and not standard IP addresses String msg = "Hello"; InetAddress group = InetAddress.getByName("228.5.6.7"); MulticastSocket s = new MulticastSocket(6789); s.joinGroup(group); DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(), group, 6789); s.send(hi);

34 34 Using MulticastSockets Code to receive messages When a message is sent, all members who have joined the group receive the message Do not have to be a member to send messages to a group byte[] buffer = new byte[1000]; DatagramPacket recv = new DatagramPacket(buffer, buffer.length); s.receive(recv); s.leaveGroup(group);

35 35 Popular uses of multicasting Beaconing Connecting to a server for which you do not know the IP address (DNS) Sending short messages to multiple hosts at the same time  Online Multiplayer Game, perhaps

36 36 Conclusion Questions? Quiz #6 Now In Lab Afterwards!


Download ppt "CSE 501N Fall ‘09 22: Introduction to Networking November 24 2009 Nick Leidenfrost."

Similar presentations


Ads by Google