Networking with java tutorial

Networking with java tutorial - contributed by Pradip Patil


In a computer system, Internet runs on two protocols TCP(Transmission control protocol) and UDP(user datagram protocol) but our java program communicates over the network. Advantage of java program is that you can use classes in package.

Socket: Socket provides the communication between the two computers or we can say that IP port on specific host machine is called socket.

Port: Port can refer to either physical or virtual connection points.

Proxy server: A proxy server is a server that acts as an intermediary for requests from clients seeking resources from other servers.

Internet addressing : Internet address is a unique number that we can use to identify the computer system on the net. There are 32/64 bits available in IP address.

DNS: Domain naming system means parallel hierarchy of the name to go with ip numbers.

URL : (universal resource locator) main advantage of URL is that it comes with the www. With the use of URL, browser can identify information from web. – networking classes and interfaces:

Following are the some classes and interfaces available in the package.

ContentHandlerFactory FileNameMap
SocketOptions Authenticator
ContentHandler DatagramPacket
ServerSocket Socket
InetAddress SocketAddress
URI URLConnection

Implementing TCP/IP based Server and Client:

For the set up of the communication between the client and server over the internet we are using BufferedReader, PrintWriter objects and we can communicate over the internet with the sockets.

Following program shows how client can make communication with server.


class Client
      public static void main(String args[]) throws UnknownHostException, IOException
           //Accept file name
           BufferedReader dis=new BufferedReader(new InputStreamReader(;
           System.out.println("Enter file name to search");
           String fn=dis.readLine();
           Socket s=new Socket ("localhost",1000);
           //send to server
          OutputStream os=s.getOutputStream();
           DataOutputStream dos=new DataOutputStream(os);
           // reading the server response
           InputStream is=s.getInputStream();
           DataInputStream ds=new DataInputStream(is);
           String msg=ds.readUTF();
                   System.out.println ("File Contents are-");
                  System.out.println (msg);
           { System.out.println ("File not present"); }
                      dis.close(); dos.close(); ds.close(); s.close();


class Server
         public static void main(String args[]) throws UnknownHostException, IOException
               ServerSocket ss=new ServerSocket (1000);
               System.out.println ("Server started, waiting for file to search-");
               Socket s=ss.accept ();
               System.out.println ("Client connected");
               // Reading file name from client side.
               InputStream is=s.getInputStream();
               DataInputStream dis=new DataInputStream(is);
               String fn=dis.readUTF();
               OutputStream os=s.getOutputStream();
               DataOutputStream dos=new DataOutputStream(os);
               File f1=new File(fn);
               String msg=""; int c;
               // Checking for file existence.
               if (f1.exists())
                              System.out.println ("File Exists");
                              FileInputStream fis=new FileInputStream(f1);
                              while(( ())!=-1)
                                             { msg=msg+(char)c; }
                              fis.close ();
                              { dos.writeUTF("0"); }
               dis.close(); dos.close(); s.close();

Above program shows how you can search the file from one end to another i.e from client to server

URL connections:

It is the predefined class which we use for accessing the attribute of the remote resource. Afer making the connection with the server you can use URLConnection, attributes are exposed by HTTP Protocols .


class Demo
               public static void main(String[] args) {
               try {
               System.out.println("Hello World!"); // Display the Message.
                              URLConnection connection = new URL("").openConnection();
               InputStream response = connection.getInputStream();
               catch(IOException ex) {}

Datagrams – Datagram packet

Datagrams : A datagram is an independent, self-contained message sent over the network whose arrival, arrival time, and content are not guaranteed.
Datagram packet: A DatagramSocket is a communcation link used to send datagrams between applications. A DatagramPacket is a message sent between applications via a DatagramSocket

Write your comment - Share Knowledge and Experience

Interview questions
Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us | We are hiring