CS212 Lecture 6 Networking 7/7/03 ----------------------------------------------------------------------- 0) Announcements - phase 2 of project should be up soon ----------------------------------------------------------------------- 1)Networking Basics + COMMUNICATION PROTOCOLS FOR NETWORKING - 2 common protocols: UDP and TCP - messages/data are "chopped up" and sent in packets, which are are collected and reassembled at the receiving end - UDP and TCP have their own strengths/weaknesses... different uses + TCP (Transmission Control Protocol) - reliable (no lost packets, no lost data) - connection-oriented (the 2 sides must establish a TCP connection before exchanging data) - has flow-control and congestion-control, which serve to limit the rate at which the sender sends. This helps both the receiver (who may be slow or running out of buffer space) and the network in \ general (to prevent congestion) - TCP sends "packets" of data. These may need to be reassembled into at the receiver if a message was placed in several packets - useful for applications that require no lost data (ex: instant messaging, web browsers/HTTP) + UDP (User Datagram Protocol) - not reliable: packets can be lost. Useful for applications that require speed but can tolerate lost info (streaming audio, games) - no flow- or congestion-control: UDP sends as fast as it can - sends independent packets of data, called "datagrams" - not connection oriented: no connection setup and teardown. UDP just starts sending. + PORTS - computer usually has 1 connection to network. How does it know which application to send received data to? - use ports! These are just numbers. Your computer has 2^16 (65536) ports. Some (0-1023) are reserved for well-known services such as HTTP, FTP, telnet, etc... - each packets sent over network has an IP address, which specifies which computer it should go to, and a port number, indicating which application on that computer it should go to. + SOCKETs (see 3 below) + IN JAVA - use URL, URLConnection, Socket, and ServerSocket classes for TCP communication - use DatagramPacket, DatagramSocket, and MulticastSocket for UDP communication java.sun.com/docs/books/tutorial/networking/overview/networking.html ----------------------------------------------------------------------- 2) URLs + URL (Uniform Resorce Locator) - an address to a resource on the internet - commonly identifies webpages, but can identify many other types of resources - has 2 parts: Protocol Identifier, Resource Name. Seperated by colon - example: PI = "http", RN = "//java.sun.com" - can also specify the port (80 here): http://www.cnn.com/:80 + CREATING URLs - Create a URL object, pass in the String representation of the URL into the constructor of the URL object URL url = new URL("http://www.cnn.com/"); + not really necessary for our project, but good to know about java.sun.com/docs/books/tutorial/networking/urls/definition.html ----------------------------------------------------------------------- 3) Sockets + SOCKETS - one endpoint of a 2-way connection between two programs over network - URL and URLConnection objects provide high-level mechanism for accessing internet resources. We can use TCP and UDP for lower-level access. Useful for writing client-server apps. - TCP uses sockets for communication between computers (TCP "binds to" a socket) on a specific port. - A socket can both read and write - in Java, use Socket on client side and ServerSocket on server side + STEPS OF COMMUNICATION - server binds to a socket on a specific port and listens for connection requests from clients - to connect to server, client opens a connection to the server at the specified port number (it should know IP address of server and port number of app on server beforehand) - server can accept of reject the connection - upon successful acceptance of connection, the connection on the server side gets a different socket (with a different port number) so the original port can still be used to listen for connections - client and server can now communicate by reading from and writing to their sockets java.sun.com/docs/books/tutorial/networking/sockets/definition.html ----------------------------------------------------------------------- 4) Sockets: reading and writing + CREATING A SOCKET - many different constructors for Socket object. One useful one is: Socket(String host, int port) - ex) Socket socket = new Socket("localhost", 7000); - creates a socket to the same machine (Java can do this) over TCP to port 7000 - when developing, I recommend connecting to "localhost" so you can develop on the same computer + READING FROM A SOCKET - use socket.getInputStream() to get the Socket's InputStream - can connect it to any other type of input stream, or an InputStreamReader, for example. - use the stream's read methods. For example, BufferedReader has the readLine() method. ObjectInputStream has readObject() - the read method should return null if the end of the stream has been reached. Check API to be sure. + WRITING TO A SOCKET - use socket.getOutputStream() method to get the OutputStream - can connect that output stream to other output streams, such as a PrintWriter, ObjectOutputStream, etc. - use the outer stream's write method (such as println() in PrintWriter, or writeObject() in ObjectOutputStream) + OVERVIEW 1) Open a socket. 2) Open an input stream and output stream on the socket. 3) Read from and write to the stream according to application's protocol. 4) Close the streams. 5) Close the socket. + Lets look at the EchoClient example from the tutorial java.sun.com/docs/books/tutorial/networking/sockets/readingWriting.html ----------------------------------------------------------------------- 5) Sockets on the server side + SERVER SOCKETS - use the ServerSocket class on the server side to wait for client connection requests - the ServerSocket is ONLY used for accepting connection requests, not for any other communication - use the accept() method of the ServerSocket object to wait for a a request. When one comes in, accept() returns with a new Socket object, which you can use to communicate with the client - From then on just read and write to the socket as explained above - the protocol (including order of messages) must be reflected in your code (see below for more info) + SUPPORTING MULTIPLE CLIENTS - in this case you must use threads - have a loop that continually accepts new client connections, thus getting a new Socket for each new client - spawn a new thread to handle communication with that client. The thread must have access to the Socket used to communicate with the client - by spawning a new thread, that thread can handle communication on its own, while the main thread can go back to accepting more client connections ----------------------------------------------------------------------- 6) Overall + PROTOCOL - the language and rules governing how two entities communicate. May take into account, syntax, ordering of messages, etc. - can be very simple or very complex - example: i say "hello", you respond with "hello" - your protocol for communication should be well-thought-out - for example: client asks for elections, server responds by sending election data. Also: client votes, server responds by sending a success or failure message. - your code must implement your protocol. For example, if the client must respond to every "hello" message with "hello", you must program that into your server