Core Java - SPLessons

Java Networking

Chapter 38

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Networking

Java Networking

shape Description

Java Networking, As everyone know that networking means connecting one or more systems together to share the resources, the advantage of Java networking is that centralize the software management. The networking classes and interfaces will be available in the java.net package and provide the support for the two protocols, they are as follows.

Networking Terminology

shape Description

While working with Java Networking, following are the terms need to know.

  • IP Address is just a unique address, it will be separated by dot, it ranges from 0 to 255.
  • Protocol is nothing but set of rules used for the communication, for example: TCP, FTP, SMTP.
  • Port Number will be associated with IP Address to provide the communication between two applications.
  • MAC Address stands for Media Access Control which is uses as network address for IEEE 802 network technologies.
  • Java Socket programming

    shape Description

    Java Networking, The purpose of socket programming is that providing communication between two applications running on various JRE. It may be a connection oriented and connection less. connection oriented socket programming uses Socket and ServerSocket classes. The connection-less socket programming uses DatagramSocket and DatagramPacket classes.

    shape Example

    Java Networking, Following is an example to understand the concept of socket programming. Here client is going to send the message and server will receives that.
    MyServer.java

    package socket;
    
    import java.io.*;  
    import java.net.*;  
    public class MyServer {  
    public static void main(String[] args){  
    try{  
    ServerSocket ss=new ServerSocket(6666);  
    Socket s=ss.accept();//establishes connection   
    DataInputStream dis=new DataInputStream(s.getInputStream());  
    String  str=(String)dis.readUTF();  
    System.out.println("message= "+str);  
    ss.close();  
    }catch(Exception e){System.out.println(e);}  
    }  
    }  

    An application uses a data output stream to write data that can later be read by a DataInputStream.

    DataInputStream dis=new DataInputStream(s.getInputStream());  

    MyClient.java

    package socket;
    
    import java.io.*;  
    import java.net.*;  
    public class MyClient {  
    public static void main(String[] args) {  
    try{      
    Socket s=new Socket("localhost",6666);  
    DataOutputStream dout=new DataOutputStream(s.getOutputStream());  
    dout.writeUTF("Hello Splessons");  
    dout.flush();  
    dout.close();  
    s.close();  
    }catch(Exception e){System.out.println(e);}  
    }  
    }  

    Output
    When compile the code result will be as follows.

    message= Hello Splessons

    The following is an example to find the ipaddress of the system.

    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    public class IPTest {
    	  
    	  
        public static void main(String args[]) throws UnknownHostException {
        
            InetAddress addr = InetAddress.getLocalHost();
          
            //Getting IPAddress of localhost - getHostAddress return IP Address
            // in textual format
            String ipAddress = addr.getHostAddress();
          
            System.out.println("IP address of localhost from Java Program: " + ipAddress);
          
            //Hostname
            String hostname = addr.getHostName();//Used to get the host name
            System.out.println("Name of hostname : " + hostname);
          
        }
      
    }
    

    Output:
    Now compile the code result will be as follows.

    IP address of localhost from Java Program: 192.168.1.23
    Name of hostname : SPlessons
    

    Summary

    shape Key Points

    • Socket is simply an endpoint of communication between machines.
    • The connect() method is used to connect socket to a server.