www.4cnotes.info

visit xdeem.com also

4cnotes.info

4cnotes.info for Free College Notes, Placement trainng

visit www.xdeem.com also

Thanks for ure visit

Thanks for visiting our site

visit xdeem.com also.

4cnotes.info

Thank u

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

TWO WAY COMMUNICATION USING TCP

Step 1 : Open 2 editors and write the coding separately for client and server.
Step2 : Include the  necessary   packages.
Step  3 : Create a new server socket in server side and storing the inputstream  in the buffered    
                   Reader.
Server Socket  ss = new  Server Socket(98);
Socket  s=  ss. Accept ();
BufferedReader  d = new BufferedReader(new  InputStreamReader (SyStepm.in));
Step  4 :  Print  the output stream  by using   print  writer.
PrintWriter  out=  new  PrintWriter(s.getOutputStream(),true);
Step 5 : Read the input and print the string  from the client until the string gets exit.
While(!(inp.equals(“exit”)))
{
String l= sin.readLine();
Out.println(l);
String inp=d.readLine();
}

Step 6 : Create a new socket in the client side.
Socket c= new Socket(InetAddress.getLocalhost(),98);
Step 7 : Print the outputstream and stored in buffered reader.
PrintWriter out=new Printwriter(c.getoutpustream(),true);
BufferedReader in=new BufferedReader(new InputStreamReader(c.getInputStream()));
Step 8 : Save the program separately with the   java extensions.
Step 9 : Compile and execute the program.


 PROGRAM
SERVER
import java.io.*;
import java.net.*;
import java.lang.*;
class tcpc1
{
public static void main(String args[]) throws IOException
{
try
{
Socket ctsoc=new Socket("localhost",8000);
while(true)
{
BufferedReader in=new BufferedReader(new InputStreamReader(ctsoc.getInputStream()));
String str1=in.readLine();
SyStepm.out.println("Server:"+str1);
if(str1.equals("exit"))
{
ctsoc.close();
}
else
SyStepm.out.println("Client");
BufferedReader stdin=new BufferedReader(new InputStreamReader(SyStepm.in));
PrintWriter out=new PrintWriter(ctsoc.getOutputStream(),true);
String userinput=stdin.readLine();
out.println(userinput);
if(userinput.equals("exit"))
{
ctsoc.close();
}}}
catch(IOException se)
{
SyStepm.out.println("server disconnected...");
}}}

CLIENT
import java.io.*;
import java.net.*;
import java.lang.*;
class tcps1
{
public static void main(String args[])throws IOException
{
ServerSocket srsoc=new ServerSocket(8000);
Socket clientsoc=srsoc.accept();
while(true)
{
try{
SyStepm.out.println("Start:");
String ipln;
BufferedReader stdin=new BufferedReader(new InputStreamReader(SyStepm.in));
ipln=stdin.readLine();
PrintWriter out=new PrintWriter(clientsoc.getOutputStream(),true);
out.println(ipln);
if(ipln.equals("exit"))
{
clientsoc.close();
srsoc.close();
break;
}
BufferedReader in=new BufferedReader(new InputStreamReader(clientsoc.getInputStream()));
String str1=in.readLine();
if(str1.equals("exit"))
{
SyStepm.out.println("Client disconnected...");
clientsoc=srsoc.accept();
}else
{
SyStepm.out.println("Client:"+str1);
}}catch(IOException se)
{
SyStepm.out.println("Client not found...");
clientsoc=srsoc.accept();        
}}}}
OUTPUT
Server
C:\Users\User>cd C:\j2sdk1.4.2_08\bin
C:\j2sdk1.4.2_08\bin>javac tcps1.java
C:\j2sdk1.4.2_08\bin>java tcps1
Start:welcome to india
Client:thank you
Start:hello how are you
Client:fine thanks
Start:
Client
C:\Users\User>cd C:\j2sdk1.4.2_08\bin
C:\j2sdk1.4.2_08\bin>javac tcpc1.java
C:\j2sdk1.4.2_08\bin>java tcpc1
Server: welcome to india
Client: thank you
Server: hello how is you
Client: fine thanks

Send Message from Server to Client using TCP in Java

SERVER
import java.net.*;
import java.io.*;
public class myserv{
public static void main(String s[]) throws IOException
{
ServerSocket s1=new ServerSocket(98);
Socket c=s1.accept();
System.out.println("Connection"+c);
PrintWriter out=new PrintWriter(c.getOutputStream(),true);
BufferedReader in=new BufferedReader(new InputStreamReader(c.getInputStream()));
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Send Message to Client :");
String l;
while((l=sin.readLine())!=null)
{
out.println(l);
System.out.print(" message :");
}
out.close();
sin.close();
c.close();
s1.close();
}}

CLIENT
import java.net.*;
import java.io.*;
public class myclient{
public static void main(String str[]) throws IOException
{
Socket s=new Socket(InetAddress.getLocalHost(),98);
BufferedReader b=new BufferedReader(new InputStreamReader(s.getInputStream()));
String inp;
System.out.println("Ready to receiver");
while((inp=b.readLine())!=null)
{
System.out.println("Input from server : "+inp);
}
b.close();
s.close();
}}
OUTPUT
Server
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\>cd C:\Program Files\Java\jdk1.6.0_11\bin
C:\Program Files\Java\jdk1.6.0_11\bin>javac myserv.java
C:\Program Files\Java\jdk1.6.0_11\bin>java myserv
ConnectionSocket[addr=/169.254.146.151,port=2873,localport=98]
Send Message to Client : god loves you
 message :have a nice day
 message :
Client
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\>cd C:\Program Files\Java\jdk1.6.0_11\bin
C:\Program Files\Java\jdk1.6.0_11\bin>javac myclient.java
C:\Program Files\Java\jdk1.6.0_11\bin>java myclient
Ready to receive
Input from server :  god loves you
Input from server : have a nice day

FInd the Factorial Number between Server and Client

SERVER
import java.net.*;
import java.io.*;
public class myserv{
public static void main(String ar[]){
try{
            DatagramSocket s =  new DatagramSocket(1234);
            while ( true ) {
            DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
            s.receive( packet );
            String message = new String(packet.getData(), 0, 0, packet.getLength());
int res=1;
int ms=Integer.parseInt(message);
for(int i=1;i<=ms;i++) res=res*i;
String str1=res+" ";
  System.out.println( "Factorial of " + 
              message + " is " + str1);       }   }
catch(Exception e){}    }}
CLIENT
import java.net.*;
import java.io.*;
public class myclient{     
 public static void main(String ar[]) {
  int myPort = 1234;
try {

  DatagramSocket ds = new DatagramSocket();
  DatagramPacket pack;
  InetAddress addr = InetAddress.getLocalHost();
  BufferedReader b=new BufferedReader (new InputStreamReader(System.in));  
       {
         System.out.print("Enter the number to find factorial : ");
         String message=b.readLine();   
         byte [] data = new byte [ message.length() ];
         message.getBytes(0, data.length, data, 0);   
         pack =  new DatagramPacket(data, data.length, addr, myPort);
        ds.send( pack );
  }  }
        catch ( IOException e )  { 
        System.out.println( e );
    }  }}
OUTPUT
Client
C:\Program Files\Java\jdk1.6.0_11\bin>java client1
Enter the number  : 4
C:\Program Files\Java\jdk1.6.0_11\bin>java client1
Enter the number to find factorial : 6
Server:
C:\Program Files\Java\jdk1.6.0_11\bin>java serv
Factorial of 4 is 24
Factorial of 6 is 720