Unformatted text preview:

COP 4610L: Java Networking Part 2 Page 1 Mark Llewellyn ©COP 4610L: Applications in the EnterpriseSpring 2006Java Networking and the Internet – Part 2COP 4610L: Applications in the EnterpriseSpring 2006Java Networking and the Internet – Part 2School of Electrical Engineering and Computer ScienceUniversity of Central FloridaInstructor : Mark [email protected] 242, 823-2790http://www.cs.ucf.edu/courses/cop4610L/spr2006COP 4610L: Java Networking Part 2 Page 2 Mark Llewellyn ©Networking• Java’s fundamental networking capabilities are declared by classes and interfaces of the java.net package, through which Java offers stream-based communications.• The classes and interfaces of java.net also offer packet-based communications for transmitting individual packets of information. This is most commonly used to transmit audio and video over the Internet.• We will focus on both sides of the client-server relationship.•The client requests that some action be performed, and the server performs the action and responds to the client.COP 4610L: Java Networking Part 2 Page 3 Mark Llewellyn ©Networking (cont.)• A common implementation of the request-response model is between Web browsers and Web servers.– When a user selects a Web site to browse through a browser (a client application), a request is sent to the appropriate Web server (the server application). The server normally responds to the client by sending the appropriate HTML Web page.COP 4610L: Java Networking Part 2 Page 4 Mark Llewellyn ©java.net• “High-level” APIs– Implement commonly used protocols such as HTML, FTP, etc. • “Low-level” APIs– Socket-based communications• Applications view networking as streams of data• Connection-based protocol• Uses TCP (Transmission Control Protocol)– Packet-based communications• Individual packets transmitted• Connectionless service• Uses UDP (User Datagram Protocol)COP 4610L: Java Networking Part 2 Page 5 Mark Llewellyn ©Internet Reference ModelApplication Layer(HTTP, FTP, DNS, etc.)Transport Layer(TCP, UDP)Network Layer(IP)Link and Physical LayerSee page 22 in part 1 for a more detailed version of this diagram.COP 4610L: Java Networking Part 2 Page 6 Mark Llewellyn ©Sockets• Java’s socket-based communications enable applications to view networking as if it were file I/O. In other words, a program can read from a socket or write to a socket as simply as reading from a file or writing to a file.•A socket is simply a software construct that represents one endpoint of a connection.• Stream sockets enable a process to establish a connection with another process. While the connection is in place, data flows between the processes in continuous streams.• Stream sockets provide a connection-oriented service. The protocol used for transmission is the popular TCP (Transmission Control Protocol). Provides reliable , in-order byte-stream serviceCOP 4610L: Java Networking Part 2 Page 7 Mark Llewellyn ©Sockets (cont.)• Datagram sockets transmit individual packets of information. This is typically not appropriate for use by everyday programmers because the transmission protocol is UDP (User Datagram Protocol).• UDP provides a connectionless service. A connectionless service does not guarantee that packets arrive at the destination in any particular order.• With UDP, packets can be lost or duplicated. Significant extra programming is required on the programmer’s part to deal with these problems.• UDP is most appropriate for network applications that do not require the error checking and reliability of TCP.COP 4610L: Java Networking Part 2 Page 8 Mark Llewellyn ©Sockets (cont.)• Under UDP there is no “connection” between the server and the client. There is no “handshaking”.• The sender explicitly attaches the IP address and port of the destination to each packet.• The server must extract the IP address and port of the sender from the received packet.• From an application viewpoint, UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server.COP 4610L: Java Networking Part 2 Page 9 Mark Llewellyn ©Example: client/server socket interaction via UDPServer (running on hostid)create socket, port=xfor incoming request:serverSocket = DatagramSocket()read request from serverSocketWrite reply to serverSocketspecifying client host address, port numberClientcreate socketclientSocket = DatagramSocket()create, address(hostid, port=x)send datagram request using clientSocketread reply from clientSocketclose clientSocketCOP 4610L: Java Networking Part 2 Page 10 Mark Llewellyn ©Example: Java server using UDPimport java.io.*;import java.net.*;class UDPServer {public static void main(String args[]) throws Exception{//Create datagram socket on port 9876DatagramSocket serverSocket = new DatagramSocket(9876);byte[] sendData = new byte[1024];byte[] receiveData = new byte[1024];while (true){//create space for the received datagramDatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);//receive the datagramserverSocket.receive(receivePacket);String sentence = new String(receivePacket.getData());COP 4610L: Java Networking Part 2 Page 11 Mark Llewellyn ©Example: Java server using UDP (cont.)//get IP address and port number of senderInetAddress IPAddress = receivePacket.getAddress();int port = receivePacket.getPort();String capitalizedSentence = sentence.toUpperCase();sendData = capitalizedSentence.getBytes();//create datagram to send to clientDatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);//write out the datagram to the socketserverSocket.send(sendPacket);} //end while loop}}COP 4610L: Java Networking Part 2 Page 12 Mark Llewellyn ©Example: Java client using UDPimport java.io.*;import java.net.*;class UDPClient {public static void main(String args[]) throws Exception{//Create input streamBufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));//Create client socketDatagramSocket clientSocket = new DatagramSocket();//Translate hostname to IP address using DNSInetAddress IPAddress = InetAddress.getByName("localhost");byte[] sendData = new byte[1024];byte[] receiveData = new byte[1024];String sentence = inFromUser.readLine();sendData = sentence.getBytes();COP 4610L: Java Networking Part 2 Page 13 Mark Llewellyn ©Example: Java client using UDP (cont.)DatagramPacket sendPacket = new


View Full Document

UCF COP 4610L - Java Networking and the Internet

Download Java Networking and the Internet
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java Networking and the Internet and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java Networking and the Internet 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?