0% found this document useful (0 votes)
5 views29 pages

Java Client-Server Programming Examples

The document contains a series of Java programs aimed at establishing various types of client-server connections, including one-way and two-way connections, as well as implementing Remote Procedure Calls (RPC) for mathematical and string operations. Each program includes code for both the server and client sides, along with the intended output. The programs cover topics such as URL handling, datagrams, and trigonometric function calculations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views29 pages

Java Client-Server Programming Examples

The document contains a series of Java programs aimed at establishing various types of client-server connections, including one-way and two-way connections, as well as implementing Remote Procedure Calls (RPC) for mathematical and string operations. Each program includes code for both the server and client sides, along with the intended output. The programs cover topics such as URL handling, datagrams, and trigonometric function calculations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

INDEX

[Link] Title Date Sign Remarks


1 Write a program to establish one-way connection between
client and server using java.

2 Write a program to establish two-way connection between


client and server using java.

3 Write a program to establish two-way connection (full


duplex communication) between client and server using
java.
4 Write a program in java to implement URL class.

5 Write a program to implement datagrams in java.

6 Write a program to implement RPC Client server Program


in java.

7 Write a program to implement RPC Client server Program


in java.(String Operations)

8 Write a program to implement RPC to calculate


Trigonometric Functions.

9 Write a program to implement RPC communication


between client and server to respond to client’s questions.

10 Write a program to implement RPC to calculate Function


Point.
PROGRAM-1

Aim-Write a program to establish one-way connection between client and server using java.

Code-

For server-

import [Link].*;

import [Link].*;

public class server

public static void main(String args[])throws IOException

ServerSocket ss= new ServerSocket(4999);

Socket s=[Link]();

[Link]("Client Connected");

For Client-

import [Link];

import [Link].*;

public class client

public static void main(String args[]) throws IOException

Socket s = new Socket("localhost",4999);

}}

Output-
PROGRAM-2

Aim-Write a program to establish two-way connection between client and server using java.

Code-

For Server-

import [Link].*;

import [Link].*;

public class Server

public static void main(String args[])throws IOException

ServerSocket ss= new ServerSocket(4999);

Socket s= [Link]();

[Link]("Client Connected");

InputStreamReader in1= new InputStreamReader([Link]());

BufferedReader bf= new BufferedReader(in1);

String str=[Link]();

[Link]("Client");

[Link](str);}}

For Client-

import [Link].*;

import [Link].*;

public class Client

public static void main(String args[])throws IOException

Socket s= new Socket("localhost",4999);

PrintWriter pr=new PrintWriter([Link]());

[Link]("hello");

[Link]();

Output-
``

PROGRAM-3

Aim-Write a program to establish two-way connection (full duplex communication) between client and server using
java.

Code-

For Server-

import [Link];

import [Link];

import [Link];

import [Link];

import [Link].*;

public class Server

public static void main(String args[])throws IOException

ServerSocket ss= new ServerSocket(4999);

Socket s= [Link]();

[Link]("Client Connected");

InputStreamReader in1= new InputStreamReader([Link]());

BufferedReader bf= new BufferedReader(in1);

String str=[Link]();

[Link]("Client");

[Link](str);

PrintWriter pr= new PrintWriter([Link]());

[Link]("yes!!!");

[Link]();

For Client-

import [Link];

import [Link];

import [Link];

import [Link];

import [Link].*;
public class Client

public static void main(String args[])throws IOException

{ Socket s= new Socket("localhost",4999);

PrintWriter pr=new PrintWriter([Link]());

[Link]("Is it working");

[Link]();

InputStreamReader in1= new InputStreamReader([Link]());

BufferedReader bf= new BufferedReader(in1);

String str=[Link]();

[Link]("Server");

[Link](str);

Output-
PROGRAM-4

Aim-Write a program in java to implement URL class.

Code-

import [Link].*;

public class firstcode

public static void main(String[] args)

try

{URL url=new URL("[Link]


q=moviesflix&oq=moviesflix&aqs=chrome..69i57j0l6j5.2829j0j7&sour ceid=chrome&ie=UTF-8");

[Link]("Protocol:" +[Link]());

[Link]("Hostname:" +[Link]());

[Link]("Port No.:" +[Link]());

[Link]("Default Pno.:" +[Link]());

[Link]("Query String:" +[Link]());

[Link]("File:" +[Link]());

[Link]("Path:" +[Link]());

catch(Exception e)

{[Link](e);}}}

Output-
PROGRAM-5

Aim-Write a program to implement datagrams in java.

Code-

For Server-

package demo12; import [Link].*;

public class Server

public static void main(String[] args) throws Exception

DatagramSocket ds = new DatagramSocket(3000);

byte[] buf = new byte[1024];

DatagramPacket dp = new DatagramPacket(buf, 1024);

[Link](dp);

String str = new String([Link](), 0, [Link]());

[Link](str);

[Link]();

For Client-

import [Link].*;

public class Client

public static void main(String[] args) throws Exception

DatagramSocket ds = new DatagramSocket();

String str = "Introduction to datagram";

InetAddress ip = [Link]("[Link]");

DatagramPacket dp = new DatagramPacket([Link](), [Link](), ip, 3000);

[Link](dp);

[Link]();

Output-
PROGRAM-6

Aim-Write a program to implement RPC Client server Program in java.

Code-

For Server-

import [Link].*;

import [Link].*;

class Server

public static void main(String[] args) throws Exception

ServerSocket sersock = new ServerSocket(3000);

[Link]("Server ready");

Socket sock = [Link]( );

BufferedReader keyRead = new BufferedReader(new InputStreamReader([Link]));

OutputStream ostream = [Link]();

PrintWriter pwrite = new PrintWriter(ostream, true);

InputStream istream = [Link]();

BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

String receiveMessage, sendMessage,fun;

int a,b,c;

while(true)

fun = [Link]();

if(fun != null)

[Link]("Operation : "+fun);

a = [Link]([Link]());

[Link]("Parameter 1 : "+a);

b = [Link]([Link]());

if([Link]("add")==0)

c=a+b;

[Link]("Addition = "+c);

[Link]("Addition = "+c);

}
if([Link]("sub")==0)

c=a-b;

[Link]("Substraction = "+c);

[Link]("Substraction = "+c);

if([Link]("mul")==0)

c=a*b;

[Link]("Multiplication = "+c);

[Link]("Multiplication = "+c);

if([Link]("div")==0)

c=a/b;

[Link]("Division = "+c);

[Link]("Division = "+c);

[Link]();

For Client –

import [Link].*;

import [Link].*;

class Client

public static void main(String[] args) throws Exception

Socket sock = new Socket("[Link]", 3000);

BufferedReader keyRead = new BufferedReader(new InputStreamReader([Link]));

OutputStream ostream = [Link]();

PrintWriter pwrite = new PrintWriter(ostream, true);

InputStream istream = [Link]();


BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

[Link]("Client ready, type and press Enter key");

String receiveMessage, sendMessage, temp;

while (true)

[Link]("\nEnter operation to perform(add,sub,mul,div)....");

temp = [Link]();

sendMessage = [Link]();

[Link](sendMessage);

[Link]("Enter first parameter :");

sendMessage = [Link]();

[Link](sendMessage);

[Link]("Enter second parameter : ");

sendMessage = [Link]();

[Link](sendMessage);

[Link]();

if ((receiveMessage = [Link]()) != null)

[Link](receiveMessage);

Output-
PROGRAM-7

Aim-Write a program to implement RPC Client server Program in java.(String Operations)

Code-

For Server-

import [Link].*;

import [Link];

import [Link];

public class Server

public static void main(String args[]) throws IOException

ServerSocket sersock= new ServerSocket(4999);

[Link]("Server ready");

[Link]("Waiting for a client ...");

Socket sock=[Link]();

[Link]("Client accepted");

BufferedReader b=new BufferedReader(new InputStreamReader([Link]));

OutputStream o=[Link]();

PrintWriter p=new PrintWriter(o,true);

InputStream i=[Link]();

BufferedReader r=new BufferedReader(new InputStreamReader(i));

String send,rec,fun,s1,s2,s3;

while(true)

fun=[Link]();

if(fun!=null)

[Link]("Operation:"+fun);

s1=[Link]();

[Link]("String1:"+s1);

s2=[Link]();

[Link]("String2:"+s2);

if([Link]("concat")==0)
{

s3=s1+s2;

[Link]("String after concatenation is:"+s3);

[Link]("String after concatenation is:"+s3);

if([Link]("len")==0)

[Link]("Length of first string is:"+[Link]());

[Link]("Length of first string is :"+[Link]());

[Link]("Length of second string is:"+[Link]());

[Link]("Length of first string is:"+[Link]());

if([Link]("compare")==0)

[Link]("Are the strings same:"+([Link](s2)));

[Link]("Are the strings same:"+([Link](s2)));

if([Link]("copy")==0)

s2=s1;

[Link]("First String:"+s1);

[Link]("First String :"+s1);

[Link]("Second string after copy:"+s2);

[Link]("Second string sfter copy:"+s2);

[Link]();

For Client-

import [Link];

import [Link].*;

public class Client


{

public static void main(String args[]) throws IOException

Socket sock= new Socket("localhost",4999);

BufferedReader b=new BufferedReader(new InputStreamReader([Link]));

OutputStream o=[Link]();

PrintWriter p=new PrintWriter(o,true);

InputStream i=[Link]();

BufferedReader r=new BufferedReader(new InputStreamReader(i));

String a,rec,fun,temp;

[Link]("Client Ready, Press Enter Key:");

while(true)

[Link]("Choose Operation(concat,len,copy,compare):");

temp=[Link]();

a=[Link]();

[Link](a);

[Link]("Enter First String:");

a=[Link]();

[Link](a);

[Link]("Enter Second String :");

a=[Link]();

[Link](a);

[Link]();

Output-
PROGRAM-8

Aim-Write a program to implement RPC to calculate Trigonometric Functions.

Code-

For Server-

import [Link].*;

import [Link];

import [Link];

public class Server

public static void main(String args[]) throws IOException

ServerSocket sersock= new ServerSocket(4999);

[Link]("Server ready");

[Link]("Waiting for a client ...");

Socket sock=[Link]();

[Link]("Client accepted");

BufferedReader b=new BufferedReader(new InputStreamReader([Link]));

OutputStream o=[Link]();

PrintWriter p=new PrintWriter(o,true);

InputStream i=[Link]();

BufferedReader r=new BufferedReader(new InputStreamReader(i));

String fun,s1;

double d,rad,res;

while(true)

fun=[Link]();

if(fun!=null)

[Link]("Function:"+fun);

s1=[Link]();

[Link]("Value in Degrees:"+s1);

d=[Link](s1);

rad=[Link](d);
[Link]("Value in Radians :"+rad);

if([Link]("sin")==0)

res=[Link](rad);

[Link]("Sin is:"+res);

[Link]("Sin is :"+res);

if([Link]("cos")==0)

res=[Link](rad);

[Link]("Cos is :"+res);

[Link]("Cos is :"+res);

if([Link]("tan")==0)

if(d!=90.0)

res=[Link](rad);

[Link]("Tan is :"+res);

[Link]("Tan is :"+res);

else

[Link]("Tan not defined.");

[Link]("Tan not defined");

if([Link]("cot")==0)

if(d!=0.0)

res=1/[Link](rad);

[Link]("Cot is :"+res);

[Link]("Cot is :"+res);
}

else

[Link]("Cot not defined");

[Link]("Cot not defined.");

if([Link]("sec")==0)

if(d!=90.0)

res=1/[Link](rad);

[Link]("Sec is :"+res);

[Link]("Sec is :"+res);

else

[Link]("Sec not defined.");

[Link]("Sec not defined");

if([Link]("cosec")==0)

if(d!=0.0)

res=1/[Link](rad);

[Link]("Cosec is :"+res);

[Link]("Cosec is :"+res);

else

[Link]("Cosec not defined");

[Link]("Cosec not defined");

}
}

[Link]();

For Client-

import [Link].*;

import [Link];

import [Link].*;

public class Client

public static void main(String args[]) throws IOException

Socket sock= new Socket("localhost",4999);

BufferedReader b=new BufferedReader(new InputStreamReader([Link]));

OutputStream o=[Link]();

PrintWriter p=new PrintWriter(o,true);

InputStream i=[Link]();

BufferedReader r=new BufferedReader(new InputStreamReader(i));

String a,fun,temp;

[Link]("Client Ready, Press Enter Key :");

while(true)

[Link]("Choose Function(sin,cos,tan,cot,sec,cosec) ");

temp=[Link]();

a=[Link]();

[Link](a);

[Link]("Enter degrees :");

a=[Link]();

[Link](a);

[Link]();

}
Output-
PROGRAM-9

Aim-Write a program to implement RPC communication between client and server to respond to client’s questions.

Code-

For Server-

import [Link].*;

import [Link].*;

public class Server

public static void main(String[] args) throws Exception

ServerSocket sersock = new ServerSocket(4999);

[Link]("Server is ready");

Socket sock = [Link]( );

BufferedReader b = new BufferedReader(new InputStreamReader([Link]));

OutputStream o = [Link]();

PrintWriter p = new PrintWriter(o, true);

InputStream i = [Link]();

BufferedReader r = new BufferedReader(new InputStreamReader(i));

String receiveMessage, sendMessage;

while(true)

if((receiveMessage = [Link]()) != null)

[Link](receiveMessage);

sendMessage = [Link]();

[Link](sendMessage);

[Link]();

For Client-

import [Link].*;

import [Link].*;
public class Client

public static void main(String[] args) throws Exception

Socket sock = new Socket("localhost", 4999);

BufferedReader b = new BufferedReader(new InputStreamReader([Link]));

OutputStream o = [Link]();

PrintWriter p = new PrintWriter(o, true);

InputStream i = [Link]();

BufferedReader r = new BufferedReader(new InputStreamReader(i));

[Link]("Start the conversation,type and press Enter");

String receiveMessage, sendMessage;

while(true)

sendMessage = [Link]();

[Link](sendMessage);

[Link]();

if((receiveMessage = [Link]()) != null)

[Link](receiveMessage);

Output-
PROGRAM-10

Aim-Write a program to implement RPC to calculate Function Point.

Code-

For server-

import [Link].*;

import [Link].*;

import [Link];

import [Link];

public class Server

public static void main(String args[]) throws IOException, ClassNotFoundException

ServerSocket sersock= new ServerSocket(4999);

[Link]("Server ready");

[Link]("Waiting for a client ...");

Socket sock=[Link]();

[Link]("Client accepted");

InputStream i=[Link]();

BufferedReader r=new BufferedReader(new InputStreamReader(i));

ObjectInputStream FromClient = new ObjectInputStream([Link]());

ObjectOutputStream ToClient = new ObjectOutputStream([Link]());

String fun,i1,i2,i3,i4,i5;

int sim[]=new int[]{3,4,3,7,5}, avg[]=new int[] {4,5,4,10,7}, comp[]=new int[] {6,7,6,15,10};

while(true)

fun=[Link]();

if(fun!=null)

[Link]("Complexity:"+fun);

i1=[Link]();

int ei=[Link](i1);

[Link]("No. of EI:"+ei);

i2=[Link]();
int eo=[Link](i2);

[Link]("No. of EO:"+eo);

i3=[Link]();

int eq=[Link](i3);

[Link]("No. of EQ:"+eq);

i4=[Link]();

int ilf=[Link](i4);

[Link]("No. of ILF:"+ilf);

i5=[Link]();

int elf=[Link](i5);

[Link]("No. of ELF:"+elf);

[Link]("Complexity Factors:");

int CF[] = (int [])[Link]();

for(int k=0;k<14; k++)

[Link](CF[k] + " ");

[Link]();

int index = 0;

String result="Functional Point (FP):";

int sum=0;

for (int k = 0; k < 14; k++)

sum = sum + CF[k];

if([Link]("simple")==0)

index = 0;

int sum_simple = sum;

int count_total = ei * sim[index] + eo * sim[index+1] + eq * sim[index+2] + ilf * sim[index+3] + elf *


sim[index+4];

double FP = count_total * (0.65 + 0.01 * sum_simple);

result += [Link](FP);

}
else if([Link]("average")==0)

index = 0;

int sum_average = sum;

int count_total = ei * avg[index] + eo * avg[index+1] + eq * avg[index+2] + ilf * avg[index+3] + elf *


avg[index+4];

double FP = count_total * (0.65 + 0.01 * sum_average);

result += [Link](FP);

else if([Link]("complex")==0)

index = 0;

int sum_complex = sum;

int count_total = ei * comp[index] + eo * comp[index+1] + eq * comp[index+2] + ilf * comp[index+3] + elf *


comp[index+4];

double FP = count_total * (0.65 + 0.01 * sum_complex);

result += [Link](FP);

else

result += "Wrong Input";

[Link](result);

[Link](result);

[Link]();

For Client-

import [Link];

import [Link].*;

import [Link].*;

public class Client

public static void main(String args[]) throws IOException, ClassNotFoundException


{

Socket sock= new Socket("localhost",4999);

BufferedReader b=new BufferedReader(new InputStreamReader([Link]));

OutputStream o=[Link]();

PrintWriter p=new PrintWriter(o,true);

ObjectOutputStream ToServer = new ObjectOutputStream([Link]());

ObjectInputStream FromServer = new ObjectInputStream([Link]());

String a,temp;

[Link]("Client Ready, Press Enter Key:");

int CF[]=new int[14];

while(true)

[Link]("Choose Complexity(simple, average and complex):");

temp=[Link]();

a=[Link]();

[Link](a);

[Link]("Enter no. of external inputs(EI):");

a=[Link]();

[Link](a);

[Link]("Enter no. external outputs(EO):");

a=[Link]();

[Link](a);

[Link]("Enter no. of external inquiries(EQ):");

a=[Link]();

[Link](a);

[Link]("Enter no. of internal files(ILF):");

a=[Link]();

[Link](a);

[Link]("Enter no of external interfaces(EIF):");

a=[Link]();

[Link](a);

[Link]("Enter processing complexity factors:");

for(int k=0;k<14; k++)

{
CF[k] = [Link]([Link]());

[Link](CF);

String ans = (String)([Link]());

[Link](ans);

[Link]();

Output-

You might also like