Java networking : Beginner's guide with complete concept and example - The Idle Coder

Java networking : Beginner's guide with complete concept and example


After so many days, today in this post I will share java networking beginner's tutorial. I think you all are aware about the term networking right? Well the creators of java have called this concept as "programming for the internet”. Well if I will describe the basic concepts of networking here, than the post will be more than 2000+ words. So you guys can easily find the basic concepts in reference book. But I must say before starting java networking programs, you should have knowledge of protocol,sockets, DNS, port etc.So it will be better if you first find relevant content of basic concepts from internet. Now coming to the main point,  I will share the concept of client server application in java networking and with the help of it we will make one beginners program of client server application to understand the concept of it in java networking.


Catch it: Java exception handling 

Points that you have to remember during java networking program


  • The first point is to import java.net.* library at header part in program. Well it is java library file for networking which contains various networking classes.
  • Java networking uses the concept of input/output operation in java. So you must have knowledge about io in java for easily understand the program of java networking.


What is client server application?

Well, you often hear about client-server in networking. It is easy to understand. Let me take an example. Suppose the user want to search java networking on the internet. Then it will simply write the query in the search box of browser. After clicking search button it will find some good relevant content about java networking in some moments. Now come to the point, here user who sent some query or request is called client and according to user request Google send some information about java networking in browser. So Google is server. Here it is necessary that both are connected. A server only gives reply when he accept client's request!

Elements that we are going to use in the java networking program


  • Server Socket is server side element which describes port number.
  • Socket is client side element which describes the packet of client's ip address and server's port number.


  • In upcoming program of java networking we will simply make two classes. One for client and one for server. We will send one request through client class and access it with server class result. I know you may be confused. So it will be good if you go through the program


Source code


Server side code


import java.io.*;    // java io library
import java.net.*;  //java networking library
class server         //Server class just started
{
ServerSocket ss;   //Server side object
Socket s;        //Client side object
InputStream is; //input file object
OutputStream os;  //outputfile object
DataInputStream dis; //Datainput object
DataOutputStream dos;//Dataoutput object

server()      //Constructor
{
try             //try block started
{
ss=new ServerSocket(1022);        //initialising port number
System.out.println("Theidlecoder server started");   //server message
s=ss.accept(); //Aceepting client request
is=s.getInputStream();
os=s.getOutputStream();
dis=new DataInputStream(is);
dos=new DataOutputStream(os);

String msg=dis.readUTF();
dos.writeUTF(msg);

is.close();
os.close();
dis.close();
dos.close();
s.close();
ss.close();
}
catch(Exception e)
{
System.out.println("Theidlecoder");
}
}

public static void main(String args[])
{
server idle=new server();  //Constructor called
}
}

Client side operation



import java.io.*; //java io library file
import java.net.*; //java networking library file
class client
{
Socket   s;   //client object
InputStream is;
OutputStream os;
DataInputStream dis;
DataOutputStream dos;

client()  //constructor started
{
try
{
s=new Socket("localhost",1022); //It will use local host and send request to 1022 port of server
is=s.getInputStream();
os=s.getOutputStream();
dis=new DataInputStream(is);
dos=new DataOutputStream(os);

dos.writeUTF("hi");

String msg=dis.readUTF();
System.out.println("Msg from idlecoder server" + msg);

is.close();
os.close();
dis.close();
dos.close();
s.close();

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

public static void main(String args[])
{
client idle=new client();     //constructor called
}
}

Note: You have to first compile both file.The you have to run server file so that it will activate server connection.Then you have to run server file.If yo will run client file first then it will catch the exception

Output


Logic part

The program looks like so long right? First of all you all need to make two different java file.One for server and one for client.Let's go to server file first.In server class we have just declared one port number which will be constant during program.By using this port number client will get the required information.We have to activate the server so we have written s=ss.accept() ! After performing some io operation server will able to read or write source of client.

Also download: Head first java free pdf

 Well in client class we have mentioned that we want to perform operation on single machine itself by declaring localhost.Besides local host we have written port number of server. Note here that port number must be same in both class file. When we write message "hi" in client class, it will be redirected to server's read portion and than from server it will print the message mentioned in client class file. So both can able to access all the resources! Still it looks harder? Carefully read theory portion first!
So this is all about java networking beginners guide
 I am sure it will be much easier after it! Stay connected for more detailed guide about java!

Previous
Next Post »