***NETWORKING***
*Networking:
A Network is an inter connection of computers and it is generally a combination
of client and server machines.
A client machine is that machine which Receives information
from the network.
A server machine is that machine which sends information on
to the network.
To make communication between two different machines on a
network they follow a
protocol.
A protocol is a standard or a set of Rules that must be
followed to make communication possible between the difference machines of a
networks.
Ex: htp, ftp, tcp/ip, smtp, nntp, pop and etc.
Very machine on a network will be Identify by a u-nick number
called as ‘IP Address’.
Port number is a u-nick identification given to very program
running on a system.
Ex: 1 to 65536 (Range of
port numbers)
Shoket is an end point of communication.
Both the client and server machine Requires of shoket for
communication.
To create a shoket in a client machine to use a shoket class.
To create a shoket in a server machine we use a server shoket
class.
Client IP Address port NO DNS->Domain
Naming Service ex: gmail.com
//program Server
import java.net.*;
import java.io.*;
class Server{
public static void main(String[] args) throws IOException{
//creating the server side socket with 222 as the server
program port no.
ServerSocket ss = new ServerSocket(222);
//waiting for the client and accepting the client side socket
Socket s = ss.accept();
//getting the client socketoutputstream
OutputStream out = s.getOutputStream();
//connecting the client outputstream to the printstream
printStream ps = new printStream(out);
//sending data to the client
ps.println(“hai”);
ps.println(“hello h r u?”);
ps.close(); //closing the connection
}
}
//program is a client side
import java.net.*;
import java.io.*;
class Client{
public static void main(String[] args)throws Exception{
//creating a client side socket by specifying the server
machine
//ip address and server program port no.
Socket s = new Socket(“localhost”,222);
//getting the inputStream associated clientSocket
InputStream in = s.getInputStream();
//connecting theinputstream to the br to read the data line
by line
BufferedReader br = new BufferedReader(new
InputStreamReader(in));
//reading the data from server and display the data in the
client
while(str! = null){
System.out.println(str);
str = br.readLine();
}
br.close(); //closing the connection
}
}
To make networking(communication) we need to write tow
programs one for the server and the other for the client machine.
This networking program is called also socket programming.
To execute the network programs we required two commands
prompts one for the server and other client.
Always the server program is execute the first and wait for
the client request.
Otherwise we get a runtime exception is called “connect
exception”.
Default IP address = “127.0.0.1” (standard machine)
//Two way communication between client and server
import java.net.*;
import java.io.*;
class ServerApp{
public static void main(String[] args) throws IOException{
//creating the server side socket with 9999 as the server
program port number
ServreSocket ss = new ServerSocket(9999);
//waiting for the client and accessing the client side socket
Socket s = ss.accept();
//stream for reading data from client
InputStream in = s.getInputStream();
BufferedReader br = new BufferedReader(new
InputStreamReader(in));
//Stream for reading data from keyboard
BufferedReader br = new BufferedReader
(new
InputStreamReader(System.in));
//stream for sending data to the client
OutputStream out = s.getOutputStream();
printStream ps = new printStream(out);
//sending and receiving data from client
while(true){
String str, msg;
while((str = br.readLine())! = null){
System.out.println(str);
msg = kb.readLine();
ps.println(msg);
}
//releasing the resources
br.close();
kb.close();
ps.close();
System.exit(0);
}
//end of while
}
//end of main()
} //end of class
// Client program
import java.net.*;
import java.io.*;
class ClientApp{
public static void main(String[]
args) throws IOException{
//creating a socket in client
machine by specifying the server machine
//IP address andserver program port
no
Socket s = new Socket(“localhost”,
9999);
//Stream for reading data from
keyboard
BufferedReadered kb = new
BufferedReadered
(new
InputStream(System.in));
OutputStream out =
s.getOutputStream();
printStream ps = new
printStream(out);
//Stream for reading data from the
server
InputStream in = s.getInputStream();
BufferedReadered br = new
BufferedReadered
(new
InputStreamReader(in));
//reading and sending data into a
server
String str, msg;
while(!(str = kb.readLine()),
equals(“bye”)){
ps.println(str);
msg = br.readLine();
System.out.println(msg);
}
//reading the resources
kb.close();
ps.close();
br.close();
}
}
}
Note: Instead of using local
hosts we can use 127.0.0.1, which is default IP address of a standalone system.
Server socket Socket
Receive Keyboard
Displaying Sending
Reading data from kb Receiving
Sending Displaying
0 comments:
Post a Comment