1 / 17

Java Networking

Java Networking. RGEC Meerut. Open System Connection . The OSI model describes the architecture for inter computer Communications. Data moves down the layers of the source computer,across a physical medium, then back up the layers of the destination computer. Networks/Protocols.

Download Presentation

Java Networking

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Networking RGEC Meerut RGEC MEERUT(IWT CS703)

  2. Open System Connection • The OSI model describes the architecture for inter computer Communications. • Data moves down the layers of the source computer,across a physical medium, then back up the layers of the destination computer RGEC MEERUT

  3. Networks/Protocols • Computer network • A set of computers using common protocols to communicate over connecting transmission media. • Protocol • A formal description of message formats and the rules two or more machines follow to exchange messages. • Transmission Control Protocol/Internet Protocol (TCP/IP) is a very popular protocol in use today. RGEC MEERUT(IWT CS703)

  4. Socket Based Communication • Programs running on separate machines can communicate with each other through designated TCP/IP sockets • Sockets are the endpoints of logical connections between two hosts and can be used to send and receive data • Socket communication in Java works similarly to I/O Operations RGEC MEERUT(IWT CS703)

  5. Stream Sockets • Stream sockets use TCP (Transmission ControlProtocol) for data transmission . • TCP is lossless and reliable because it can detect lost transmissions and resubmit them. • All data sent is received in the same order it was sent. • A stream of 8-bit bytes is exchanged across a TCP connection. • Connections provided by TCP allow concurrent transfer in both directions. Such connections are called full duplex. • A fitting analogy is a telephone connection with a dedicated link. • With the chat project, all conversations between usersare handled using stream sockets . RGEC MEERUT(IWT CS703)

  6. Connection –Oriented Transfer RGEC MEERUT(IWT CS703)

  7. Datagram Sockets • Datagram sockets use UDP (User Datagram Protocol) for data transmission . • UDP cannot guarantee that packets are not lost, or not received in duplicate, or received in the order they were sent. • An analogy is the sending of letters through the post office • Although you don’t get duplicate letters. • With the chat project, the broadcasting of the registered user list is handled using datagram sockets. RGEC MEERUT(IWT CS703)

  8. Connectionless Transfer RGEC MEERUT(IWT CS703)

  9. Client/Server Computing RGEC MEERUT(IWT CS703)

  10. Server Socket • To establish a server, you need to create a server socket and attach it to a port where the server will listen for connections • Port numbers range from 0 to 65536 • Ports 0 to 1024 are reserved for privileged services • ftp runs on port 21 • sendmail runs on port 25 • http runs on port 80 • To create a server socket on port 8000: • // running on server: rgec.edu • try { • ServerSocket servSocket = new ServerSocket(8000); • } catch (java.net.BindException) { • // port is already in use} RGEC MEERUT(IWT CS703)

  11. Client Sockets • After a server socket is created, the server can listen for client connections using accept() • // running on server: rgec.edu • Server socket = servSocket.accept(); • The statement blocks until a client connects to the server socket using the server host name • // running on client: ikaruga.cs.rit.edu • Socket socket = new Socket(“rgec.edu”, 8000); • alternatively, you can specify the IP address in a string: • “129.21.36.165” RGEC MEERUT(IWT CS703)

  12. Client Socket • If you provide a host name instead of an IP when creating a socket, Java will query a Domain Name Server to do the translation. • The hostname localhost refers to the machine on which a client is running “There’s no place like 127.0.0.1” RGEC MEERUT(IWT CS703)

  13. Data Transmission Through Sockets RGEC MEERUT(IWT CS703)

  14. Data Transmission Through Sockets • The socket returns an InputStream and OutputStream for reading and writing bytes. • InputStream is =socket.getInputStream(); • OutputStream os = socket.getOutputStream(); • • For efficiency, wrap those streams to do binary I/O: • DataInputStream input = new DataInputStream(is); • DataOutputStream output = new DataOutputStream(os); RGEC MEERUT(IWT CS703)

  15. InetAddress • Use InetAddress to find the hostname and IP of a client connecting to a server. • InetAddress inetAddress = socket.getInetAddress(); • System.out.println(“Client’s host name is “ + inetAddress.getHostName()); • System.out.println(“Client’s IP address is “ +inetAddress.getHostAddress()); • • You can create an instance of InetAddress using the static method getByName(String). • InetAddress address =InetAddress.getByName(“rgec.edu”); RGEC MEERUT(IWT CS703)

  16. Client/Server Example • Write a client program which sends the radius of a circleto a server program, which computes the area and then sends the result back to the client RGEC MEERUT(IWT CS703)

  17. Port Protocol Port Number Datatime 13 FTP –data 20 FTP –Control 21 Telnet 23 SMTP 25 HTTP 80 POP3 110 NNTP 119 RGEC MEERUT(IWT CS703)

More Related