Monday 3 December 2012

Networking

Posted by Naveen Katiyar On 11:21 No comments
Networking is the process of connecting the devices into a group to share the resources or information by using a medium.

Terminology used in networking:

i)IP address-It is a numeric value which is used to uniquely identify a particular device in the network.It can be assigned in the following two ways:-
a)Statically
b)Dynamically

ii)Protocol-A protocol defines set of rules and regulations for data communication.There are following two basic protocols for data communication:
a)TCP
b)UDP

iii)Port no-It is an integer value  which is used to uniquely identify a process in a particular system.Range of port no given to a process is 0-65535.Among these,0-1023 are already reserved.
           e.g. 80-http,25-SMTP

iv)Socket-Socket is the abstraction of IP address,port and protocol,i.e. Socket is the combination of IP address,protocol,and port.Socket can also be defined in the following ways:

  • It represents the end point of a connection.
  • Socket is the combination of a process and buffer.
  • Socket represents the logical representation of physical connection. 

Sockets supported by java

java supports the following two types of sockets-
i)TCP based sockets
ii)UDP based sockets

i)TCP Based Sockets:-There are two types of TCP sockets-
   a)Client-side Socket-An object of java.net.Socket represents TCP based client side socket.
   b)Server-side Socket-An object of java.net.ServerSocket represents TCP based server side socket.

Constructors of Socket class-
=> public Socket()
=> public Socket(String host address,int host port)

Commonly used methods of Socket-
=> public InputStream getInputStream()
=> public OutputStream getOutputStream()
=> public void Connect(String host address,int host port)

Constructor of ServerSocket-
=> public ServerSocket(int port)

Commonly used method of ServerSocket-
=> public Socket accept()

Steps involved in Server and Client Socket communication:-

1.0--> server program creates ServerSocket and then wait for client request to establish the connection.
2.0--> client program creates a client Socket and this client Socket connects with ServerSocket.
2.1--> client Socket connects with ServerSocket.
2.2--> A copy of client Socket is obtained from the ServerSocket at server side.
3.0--> client program obtain an OutputStream associated with the client Socket.
3.1--> data is written to the Socket and transmittedto the corresponding ServerSocket at the server side.
3.2--> server program obtains InputStream from the client Socket at server side to read the data. 

        java class for server program is as under:

package mypack;

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer {
public static void main(String arg[])
{
try
{
ServerSocket ss= new ServerSocket(1234);
System.out.println("server is started and waiting for client request");
Socket sk = ss.accept();
System.out.println("client is connected.");
InputStream in = sk.getInputStream();
DataInputStream din = new DataInputStream(in);
while(true)
{
String msg = din.readUTF();
if(msg.equals("stop"))
break;
System.out.println("msg");
}
}catch(Exception e){e.printStackTrace();}
}

}


                    java class for client program is:-


package mypack;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPClient {
public static void main(String arg[])
{
try
{
Socket sk = new Socket("localhost",1234);
OutputStream os = sk.getOutputStream();
DataOutputStream dout = new DataOutputStream(os);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
String msg = br.readLine();
dout.writeUTF(msg);
if(msg.equals("stop"))
break;
}
}catch(Exception e){e.printStackTrace();}
}


}

                      

0 comments: