Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Java Networking Computer Science

Java for computer science

Uploaded by

Sura Se
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java Networking Computer Science

Java for computer science

Uploaded by

Sura Se
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

CLIENT SERVER

APPLICATION

By Woldekian.G
CLIENT PROGRAM
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class MyClient {
public static void main(String[] args) {
try{
Socket clientSoc = new Socket("localhost",6666); //Using socket using
Server IP address & port number
Scanner userInput = new Scanner(System.in);
Scanner sIn = new Scanner(clientSoc.getInputStream()); //getting input
stream for accepting input from server
PrintStream printer = new PrintStream(clientSoc.getOutputStream());
int num, res;
CLIENT PROGRAM
System.out.println("Enter the number");
num = userInput.nextInt();
printer.println(num);
res = sIn.nextInt();
System.out.println("Result = "+res);
clientSoc.close();
}catch(Exception e){ System.out.println(e);
}
}
}
SERVER PROGRAM
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class MyServer {
public static void main(String[] args){
try{
ServerSocket ss = new ServerSocket(6666);// creating server socket with port
number
Socket serSocket = ss.accept();//establish connection
Scanner serIn = new Scanner(serSocket.getInputStream());//accept input from
client
PrintStream pStream = new PrintStream(serSocket.getOutputStream());//for
sending data to client
int numIn, fRes;
SERVER PROGRAM
numIn = serIn.nextInt();
fRes = numIn*numIn;
pStream.println(fRes);//sending result to client
ss.close();
}catch(Exception e){System.out.println(e);
}
}
}
UDP CLIENT
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class UDPClient {
public static void main(String[] args)throws Exception{
DatagramSocket dSocket = new DatagramSocket();
byte[] receive = new byte[1024];
byte[] send = new byte[1024];
InetAddress ip = InetAddress.getByName("localhost");
UDP CLIENT
System.out.println("Enter sentence to be converted: ");
Scanner in = new Scanner(System.in);
String sen = in.next();
send = sen.getBytes();
DatagramPacket sendPacket = new
DatagramPacket(send,send.length,ip,6666);
dSocket.send(sendPacket);
DatagramPacket receivePacket = new
DatagramPacket(receive,receive.length);
dSocket.receive(receivePacket);
String Converted = new String(receivePacket.getData());
System.out.println("Converted Sentence:" + Converted);
}
}
UDP SERVER
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPServer {
public static void main(String[] args)throws Exception{
DatagramSocket sSocket = new DatagramSocket(6666);
byte[] receive = new byte[1024];
byte[] send = new byte[1024];
DatagramPacket rPacket = new
DatagramPacket(receive,receive.length);
sSocket.receive(rPacket);
UDP SERVER
InetAddress ip = rPacket.getAddress();
int portNo = rPacket.getPort();
String CleintSentence = new String(rPacket.getData());
String converted = CleintSentence.toUpperCase();
send = converted.getBytes();
DatagramPacket sPacket = new
DatagramPacket(send,send.length,ip,portNo);
sSocket.send(sPacket);
}
}
IMPLEMENTING RMI
To write an RMI Java application, you would have to follow the steps
given below :
1.Create the remote interface

2.Provide the implementation of the remote interface

3.Start the registry service by rmiregistry tool

4.Create and start the remote application

5.Create and start the client application


STEP 1. CREATE THE REMOTE INTERFACE
//pls save it as SampleServer.java using Notepad
import java.rmi.*;
public abstract interface SampleServer extends Remote{
public int add(int a, int b) throws RemoteException;//method
that calculates the addition of two numbers
}
STEP 2. PROVIDE THE IMPLEMENTATION OF THE REMOTE INTERFACE
//pls save it as SampleServerImpl.java using Notepad
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class SampleServerImpl extends UnicastRemoteObject implements
SampleServer{
SampleServerImpl() throws RemoteException{
super();
}
public int add(int a, int b) throws RemoteException
{
return a+b;
}
}
STEP 3. IMPLEMENTATION OF CLIENT
//pls save it as MyClient.java using Notepad
import java.rmi.*;
import java.util.Scanner;
public class MyClient{
public static void main(String[] args){
int x,y;
//get the remote object from the registry
try{
String url = "rmi://localhost:1099/wolde";
SampleServer remoteObject = (SampleServer)Naming.lookup(url);
System.out.println("Remote object accessed");
Scanner input = new Scanner(System.in);
System.out.println("Enter the first number: ");
x = input.nextInt();
System.out.println("Enter the second number: ");
y = input.nextInt();
System.out.println("Result= "+remoteObject.add(x,y));
}catch(Exception e){
System.out.println(e);
}
}
}
STEP 4. IMPLEMENTATION OF THE SERVER
//pls save it as MyServer.java using Notepad
import java.rmi.*;
import java.rmi.registry.*;
public class MyServer{
public static void main(String[] args){
try{
//Create a local instance of the object
SampleServer server = new SampleServerImpl();
//put the local instance in the registry
Naming.rebind("rmi://localhost:1099/wolde", server);
}catch(Exception e){
System.out.println(e);
}
}
}
STEP 5. RUNNING ALL THE JAVA FILES AT ONCE
Create RMI folder on Desktop then save all the .java files you
have created above to it and Open command line prompt
write the following command

If there’s any error in your code fix it and recompile.


STEP 6. START THE REGISTRY SERVICE
Open a new cmd and register your registry using the
following command

If Firewall blocks allow it.


STEP 7. START(RUN) THE SERVER
Open a new cmd run your server using the following
command
STEP 8. START(RUN) THE CLIENT
Open a new cmd run your server using the following
command

If successful, the following lines appeared:

You might also like