Adv Java
Programming

1. What Is the Java Technology 2. Points to keep in mind for pyramid. 3. Java Pyramid Program on type 1 4. Java Pyramid Program on type 2 5. Java Pyramid Programs on Type 3 6. Java Pyramid Programs on type 4 7. Java Pyramid Programs on type 5 8. Classes and Objects 9. Recursive functions 10. Constructors in Java 11. Method Overloading 12. Constructor Overloading 13. Inheritance in Java 14. Interface in Java 15. Method Overriding and Hiding Method 16. The final keyword 17. Exception Handling 18. Properties, System & Runtime Class in Java 19. Building Java GUI 20. Layout Managers 21. Menu Bar 22. Applets 23. Threading 24. Java Netwoking 25. Programs for Science and Engineer 26. Prg to demonstrate pre and post increment 27. Prg to convert Farenheit to Celsius temperature 28. Prg to convert kilogram to different units 29. Prg to calculate surface area and volume of sphere 30. Prg to calculate frequency 31. Prg to determine co-ordinate points 32. Prg to determine Equilateral, Isosceles or Scalene triangle 33. Prg to demonstrate conditional operator 34. Prg to calculate Final and initial interest amount 35. Prg to separate entered number 36. Prg to examine while loops and conditions 37. Prg to calculate number of year 38. Prg to find Armstrong number or not 39. Prg to find all Armstrong number 40. Prg to find limited Armstrong number 41. Prg to find Armstrong number using user defined function 42. Prg to find frequency of student 43. Prg to find maximum and minimum number 44. Prg to print number triangle 45. Prg to print vertical number triangle 46. Prg to print number triangle 47. Prg to print binary right angle triangle 48. Prg to print * triangle 49. Prg to to print rhombus 50. Prg to print entered number triangle 51. Prg to to find nCr and nPr 52. Prg to enter an element in array 53. Prg to sort number using array 54. Prg to find the position of element in array 55. Prg to find the position of element in array using boolean flag 56. Prg to add one extra element in an array 57. Prg to find the equation of line 58. Prg to find the common element from two array 59. Prg to find the highest scorer 60. Prg to create a square of number 61. Prg to create Pascal triangle 62. Prg to subtraction, multiplication and division using methods and classes 63. Prg to determine salary grade 64. Prg for complex number 65. Prg to calculate area 66. Prg for Greatest Common Divisior 67. Prg for displaying studenta mark sheet 68. Prg for demonstrating inheritance 69. Prg to calculate area of triangle 70. Prg for calculating volume of sphere or hemisphere 71. Prg for demonstrate classes 72. Prg for employee details 73. Prg for demonstrating super and base class 74. Prg for Bank Account 75. Prg for professor and student details 76. Prg for Addition, Multiplication and finding Transpose of two matrix 77. Prg for counting number of words in a string 78. Prg to count number of vowels and consonants in a string 79. Prg to fine number of upper case, lower case, white spaces and digit in a string 80. Prg to fine number of upper case, lower case, white spaces and digit in a string without inbuilt function 81. Prg to arrange a string in alphabetic order 82. Prg to count the number of character occurrence in a string 83. Prg to find the string length and character position 84. Prg for converting first character of word into upper case 85. Prg for demonstrating vectors for manipulating records 86. Prg for manupulting items record using vectors 87. Prg for bank transaction 88. Prg for counting number of days between any two different date 89. Prg for exception handling 90. Prg for throwing exception 91. Prg for exception 92. Prg to throw exception when number is invalid 93. Prg for renaming the current thread 94. Prg for demonstrating strat method of thread 95. Prg to demonstrate sleep and and join method of thread 96. Prg to demonstrate yield method of thread 97. Prg for demonstrating thread in class inheritance 98. Threading program using runnable interface 99. Thread program using sleep 100. Java Applet program for creating fancy round eye 101. Java Applet program for creating fancy face 102. Java Applet program for drawing string 103. Java Applet program for creating fancy face 104. Java Applet program for displaying fancy fonts 105. Java Applet program for drawing and filling rectangle 106. Java Applet program for creating house 107. Java Applet program to show lifecycle of applet 108. Java Applet program for creating polygon 109. Java Applet program for creating rectangle 110. Java Applet program for drawing line 111. Java Applet program for getting applet width and height 112. Interview Question & Answer part 1 113. Interview Question & Answer part 2 114. Interview Question & Answer part 3 115. Interview Question & Answer part 4 116. Interview Question & Answer part 5 117. Interview Question & Answer part 6 118. Interview Question & Answer part 7 119. Interview Question & Answer part 8 120. Interview Question & Answer part 9 121. Interview Question & Answer part 10 122. Interview Question & Answer part 11
Pr.Pg Next Pg

Java Netwoking tutorials

  • 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.

  • The java.net package provides support for the two common networkprotocols:

    1. 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.

    2. UDP: UDP stands for User Datagram Protocol, a connection-less protocol that allows for packets of data to be transmitted between applications.

Sockets

  • If TCP is similar to placing a telephone call, a socket is the telephone. 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.

  • The following steps occur when establishing a TCP connection between two computers using sockets:

    1. The server instantiates a ServerSocket object, denoting which port number communication is to occur on.

    2. The server invokes the accept() method of the ServerSocket class. This method waits until a client connects to the server on the given port.

    3. After the server is waiting, a client instantiates a Socket object, specifying the server name and port number to connect to.

    4. 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.

    5. 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.

  • Below diagram shows the Java Networking Mode using sockets:

 

 

Example: Java Network program for Server which will accept the connection from Client

 Sam Madam

//Program Server.java

// Java Network program for Server which will accept the connection from Client

import java.io.*;

import java.net.*;

 

public class Server

{

public static void main(String[] args){

try

{

ServerSocket ss=new ServerSocket(5000);

Socket s=ss.accept();

 

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);}

}

}

Output

Now compile and run the Server program and wait for the Client as below:

 

  • Note: Do not close the Server prompt.

  • Now Below is the Client program:

 

 

Example: Java network program for Client Program.

 Sam Madam

//Client.java

// Java network program for Client Program

import java.io.*;

import java.net.*;

 

public class Client

{

public static void main(String[] args)

{

try{

Socket s=new Socket("localhost",5000);

 

DataOutputStream dout=new DataOutputStream(s.getOutputStream());

 

dout.writeUTF("Echo Server");

dout.flush();

 

dout.close();

s.close();

}catch(Exception e){System.out.println(e);}

}

}

Output

Now compile and run the Client.java in new prompt as below:

 

Note : When you run the Client “Echo Server Message will pop in the Server prompt as below:

 

Note: If Server prompt is closed and you run the client then it will show ConnectionException as below:

 

InetAddress Class

  • InetAddress class represents an Internet Protocol (IP) address.

  • An IP address is either a 32-bit or 128-bit unsigned number used by IP, a lower-level protocol on which protocols like UDP and TCP are built.

  • An instance of an InetAddress consists of an IP address and possibly its corresponding host name depending on whether it is constructed with a host name or whether it has already done reverse host name resolution.

 

Host Name Resolution

  • Host name-to-IP address resolution is accomplished through the use of a combination of local machine configuration information and network naming services such as the Domain Name System (DNS) and Network Information Service(NIS).

  • The particular naming services(s) being used is by default the local machine configured one.

  • For any host name, its corresponding IP address is returned.

  • Reverse name resolution means that for any IP address, the host associated with the IP address is returned.

  • Below is the simple program which returns the IP address of the localhost.

 

 

Example: Java program to get the IP address of the localhost machine

 Sam Madam

//Program name localhostAddress.java

// Java program to get the IP address of the localhost machine

import java.net.InetAddress;

public class localhostAddres {

public static void main(String[] args) throws Exception

{

System.out.println("Your Address is :"+InetAddress.getLocalHost());

}

}

Output

Your Address is :sam-lab103/192.168.1.104

 

 

Example: Java program to get IP address of Host

 Sam Madam

//Program name GetIPAddress.java

//Java program to get IP address of Host

import java.net.*;

public class GetIPAddress

{

public static void main(String[] args)

{

try

{

InetAddress ip=InetAddress.getByName("www.cbtsam.com");

System.out.println("Host Name: "+ip.getHostName());

System.out.println("IP Address: "+ip.getHostAddress());

}catch(Exception e)

{

System.out.println(e);

}

}

}

Output

Host Name: www.cbtsam.com

IP Address: 76.72.165.79

 

Note: Now lets modify the above program to get IP address of any host we entered:

 

 

Example: Java program to get IP address of Host

 Sam Madam

//Program name GetIPAddress.java

//Java program to get IP address of Host

import java.net.*;

import java.util.Scanner;

public class GetIPAddress

{

public static void main(String[] args)

{

String host;

System.out.print("Enter host name : ");

Scanner scan=new Scanner(System.in);

host=scan.nextLine();

try

{

InetAddress ip=InetAddress.getByName(host);

System.out.println("Host Name: "+ip.getHostName());

System.out.println("IP Address: "+ip.getHostAddress());

}catch(Exception e)

{

System.out.println(e);

}

}

}

Output

Enter host name : www.cbtsam.com

Host Name: www.cbtsam.com

IP Address: 76.72.165.79

 

Enter host name : www.samedu.in

Host Name: www.samedu.in

IP Address: 106.10.153.167

Pr.Pg border                                              Next Pg