Advanced Programming Networking in Java Gera 2020 1
Networking in Java •The Java programming language is supposed to become the premier tool for connecting computers over the Internet and corporate intranets and, in this realm, Java mostly lives up to the hype. •If you are accustomed to programming network connections in C or C++, you will be pleasantly surprised at how easy it is to program them in the Java programming language. 2
Networking in Java… •The term network programming refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network. •The java.net package of the J2SE APIs contains a collection of classes and interfaces that provide the low-level communication details, allowing you to write programs that focus on solving the problem at hand. 3
Networking in Java… • The java.net package provides support for the two common network protocols: • TCP: TCP stands for Transmission Control Protocol, which allows for reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCP/IP. • UDP: UDP stands for User Datagram Protocol, a connection-less protocol that allows for packets of data to be transmitted between applications. 4
Implementing Servers in Java • To implement a basic network client that receives data from the Net, let’s implement a simple server that can send information out to the Net. • Once you start the server program, it waits for some client to attach to its port. • We chose port number 8189, which is not used by any of the standard services. • The ServerSocket class is used to establish a socket. • In our case, the command establishes a server that monitors port 8189 • ServerSocket s = new ServerSocker (8189); 5
Socket Programming •Sockets provide the communication mechanism between two computers using TCP. •A client program creates a socket on its end of the communication and attempts to connect that socket to a server. •When the connection is made, the server creates a socket object on its end of the communication. • The client and server can now communicate by writing to and reading from the socket. 6
Socket Programming… • The java.net.Socket class represents a socket, and the java.net.ServerSocket class provides a mechanism for the server program to listen for clients and establish connections with them. • The following steps occur when establishing a TCP connection between two computers using sockets: • The server instantiates a ServerSocket object, denoting which port number communication is to occur on. • The server invokes the accept() method of the ServerSocket class. This method waits until a client connects to the server on the given port. 7
Socket Programming… • After the server is waiting, a client instantiates a Socket object, specifying the server name and port number to connect to. • The constructor of the Socket class attempts to connect the client to the specified server and port number. If communication is established, the client now has a Socket object capable of communicating with the server. • On the server side, the accept() method returns a reference to a new socket on the server that is connected to the client's socket. 8
Socket Programming… • After the connections are established, communication can occur using I/O streams. Each socket has both an OutputStream and an InputStream. The client's OutputStream is connected to the server's InputStream, and the client's InputStream is connected to the server's OutputStream. • The following GreetingServer program is an example of a server application that uses the Socket class to listen for clients on a port number specified by a command-line argument: 9
Manipulating URLs… 10 Compile client and Server and then start server as follows:
Manipulating URLs • The Internet offers many protocols. • The Hypertext Transfer Protocol (HTTP), which forms the basis of the World Wide Web, uses URIs (Uniform Resource Identifiers) to identify data on the Internet. • URIs that specify the locations of documents are called URLs (Uniform Resource Locators). • Common URLs refer to files or directories and can reference objects that perform complex tasks, such as database lookups and Internet searches. • If you know the HTTP URL of a publicly available HTML document anywhere on the web, you can access it through HTTP. 11
Manipulating URLs… •Java makes it easy to manipulate URLs. •When you use a URL that refers to the exact location of a resource (e.g., a web page) as an argument to the showDocument method of interface AppletContext, the browser in which the applet is executing will display that resource. 12
Manipulating URLs… URL Processing •URL stands for Uniform Resource Locator and represents a resource on the World Wide Web, such as a Web page or FTP directory. •The following URLDemo program demonstrates the various parts of a URL. •A URL is entered on the command line, and the URLDemo program outputs each part of the given URL. 13
Manipulating URLs… 14 A sample run of the program would produce the following result:
Manipulating URLs… URL Connections Class Methods: •The openConnection() method returns a java.net.URLConnection, an abstract class whose subclasses represent the various types of URL connections. • The following URLConnectionDemo program connects to a URL entered from the command line. • If the URL represents an HTTP resource, the connection is cast to HttpURLConnection, and the data in the resource is read one line at a time. 15
Manipulating URLs… 16 A sample run of the program would produce the following result:
References ➢S. Horstmann and Gary Cornell, Core Java 2 – Volume II- Advanced Features, Sun Microsystems Press ➢Harvey M. Deitel and Paul J. Deitel, Java How to Program, Deitel & Associates ➢Tutorials Point “Java Tutorial,” tutorialspoint.com 17 Gerabirhan Paulos ToCourseInfo@gmail.com

Networking in java, Advanced programming

  • 1.
  • 2.
    Networking in Java •TheJava programming language is supposed to become the premier tool for connecting computers over the Internet and corporate intranets and, in this realm, Java mostly lives up to the hype. •If you are accustomed to programming network connections in C or C++, you will be pleasantly surprised at how easy it is to program them in the Java programming language. 2
  • 3.
    Networking in Java… •Theterm network programming refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network. •The java.net package of the J2SE APIs contains a collection of classes and interfaces that provide the low-level communication details, allowing you to write programs that focus on solving the problem at hand. 3
  • 4.
    Networking in Java… •The java.net package provides support for the two common network protocols: • TCP: TCP stands for Transmission Control Protocol, which allows for reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCP/IP. • UDP: UDP stands for User Datagram Protocol, a connection-less protocol that allows for packets of data to be transmitted between applications. 4
  • 5.
    Implementing Servers inJava • To implement a basic network client that receives data from the Net, let’s implement a simple server that can send information out to the Net. • Once you start the server program, it waits for some client to attach to its port. • We chose port number 8189, which is not used by any of the standard services. • The ServerSocket class is used to establish a socket. • In our case, the command establishes a server that monitors port 8189 • ServerSocket s = new ServerSocker (8189); 5
  • 6.
    Socket Programming •Sockets providethe communication mechanism between two computers using TCP. •A client program creates a socket on its end of the communication and attempts to connect that socket to a server. •When the connection is made, the server creates a socket object on its end of the communication. • The client and server can now communicate by writing to and reading from the socket. 6
  • 7.
    Socket Programming… • Thejava.net.Socket class represents a socket, and the java.net.ServerSocket class provides a mechanism for the server program to listen for clients and establish connections with them. • The following steps occur when establishing a TCP connection between two computers using sockets: • The server instantiates a ServerSocket object, denoting which port number communication is to occur on. • The server invokes the accept() method of the ServerSocket class. This method waits until a client connects to the server on the given port. 7
  • 8.
    Socket Programming… • Afterthe server is waiting, a client instantiates a Socket object, specifying the server name and port number to connect to. • The constructor of the Socket class attempts to connect the client to the specified server and port number. If communication is established, the client now has a Socket object capable of communicating with the server. • On the server side, the accept() method returns a reference to a new socket on the server that is connected to the client's socket. 8
  • 9.
    Socket Programming… • Afterthe connections are established, communication can occur using I/O streams. Each socket has both an OutputStream and an InputStream. The client's OutputStream is connected to the server's InputStream, and the client's InputStream is connected to the server's OutputStream. • The following GreetingServer program is an example of a server application that uses the Socket class to listen for clients on a port number specified by a command-line argument: 9
  • 10.
    Manipulating URLs… 10 Compile clientand Server and then start server as follows:
  • 11.
    Manipulating URLs • TheInternet offers many protocols. • The Hypertext Transfer Protocol (HTTP), which forms the basis of the World Wide Web, uses URIs (Uniform Resource Identifiers) to identify data on the Internet. • URIs that specify the locations of documents are called URLs (Uniform Resource Locators). • Common URLs refer to files or directories and can reference objects that perform complex tasks, such as database lookups and Internet searches. • If you know the HTTP URL of a publicly available HTML document anywhere on the web, you can access it through HTTP. 11
  • 12.
    Manipulating URLs… •Java makesit easy to manipulate URLs. •When you use a URL that refers to the exact location of a resource (e.g., a web page) as an argument to the showDocument method of interface AppletContext, the browser in which the applet is executing will display that resource. 12
  • 13.
    Manipulating URLs… URL Processing •URLstands for Uniform Resource Locator and represents a resource on the World Wide Web, such as a Web page or FTP directory. •The following URLDemo program demonstrates the various parts of a URL. •A URL is entered on the command line, and the URLDemo program outputs each part of the given URL. 13
  • 14.
    Manipulating URLs… 14 A samplerun of the program would produce the following result:
  • 15.
    Manipulating URLs… URL ConnectionsClass Methods: •The openConnection() method returns a java.net.URLConnection, an abstract class whose subclasses represent the various types of URL connections. • The following URLConnectionDemo program connects to a URL entered from the command line. • If the URL represents an HTTP resource, the connection is cast to HttpURLConnection, and the data in the resource is read one line at a time. 15
  • 16.
    Manipulating URLs… 16 A samplerun of the program would produce the following result:
  • 17.
    References ➢S. Horstmann andGary Cornell, Core Java 2 – Volume II- Advanced Features, Sun Microsystems Press ➢Harvey M. Deitel and Paul J. Deitel, Java How to Program, Deitel & Associates ➢Tutorials Point “Java Tutorial,” tutorialspoint.com 17 Gerabirhan Paulos ToCourseInfo@gmail.com