0% found this document useful (0 votes)
23 views

Practicequestion

The document discusses socket programming to develop client-server applications for calculating area of a circle and finding the factorial of a number. It includes code for a client and server that can communicate to calculate these values and return the results to the client.

Uploaded by

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

Practicequestion

The document discusses socket programming to develop client-server applications for calculating area of a circle and finding the factorial of a number. It includes code for a client and server that can communicate to calculate these values and return the results to the client.

Uploaded by

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

Dr.

Bharti Sharma, SOC, Networking

1. Write a program using socket programming to develop the client/ server application to calculate the Area of the
circle.

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

class client
{
public static void main(String[] args)
{
try
{
System.out.println("Client is started ");
Socket soc= new Socket ("localhost",9800) ;
DataOutputStream toServer = new DataOutputStream(soc.getOutputStream());
System.out.println("enter the radius");
Scanner sc= new Scanner(System.in);
double radius = sc.nextDouble();
toServer.writeDouble(radius);
DataInputStream fromServer = new DataInputStream(soc.getInputStream());
double area= fromServer.readDouble();
System.out.println(area);

}
Dr. Bharti Sharma, SOC, Networking

catch(IOException e)
{

}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class server


{

public static void main(String[] args)


{
// TODO Auto-generated method stub
try
{
System.out.println("Waiting for client");
ServerSocket ss= new ServerSocket(9800);
Socket soc=ss.accept();
System.out.println("Connection is established ");
DataInputStream inputFromClient = new DataInputStream(soc.getInputStream());
Dr. Bharti Sharma, SOC, Networking

double radius = inputFromClient.readDouble();


double area = radius * radius * Math.PI; // Compute area
DataOutputStream outputToClient = new DataOutputStream(soc.getOutputStream());
outputToClient.writeDouble(area);

}
catch (IOException e)
{

2. Write a program using socket programming to develop the client/ server application to find the factorial of the
given number

import java.io.IOException;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
Dr. Bharti Sharma, SOC, Networking

class client2
{
public static void main(String[] args)
{
try
{
System.out.println("Client is started ");
Socket soc= new Socket ("localhost",9800) ;
DataOutputStream toServer = new DataOutputStream(soc.getOutputStream());
System.out.println("enter the number");
Scanner sc= new Scanner(System.in);
int number = sc.nextInt();
toServer.writeInt(number);
DataInputStream fromServer = new DataInputStream(soc.getInputStream());
int f= fromServer.readInt();
System.out.println("factorial of the given number is ");
System.out.println(f);
Dr. Bharti Sharma, SOC, Networking

}
catch(IOException e)
{
}
}
}
package socket;
import java.io.IOException;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class server2
{
public static void main(String[] args)
{
Dr. Bharti Sharma, SOC, Networking

int f=1;
try
{
System.out.println("Waiting for client");
ServerSocket ss= new ServerSocket(9800);
Socket soc=ss.accept();
System.out.println("Connection is established ");
DataInputStream inputFromClient = new DataInputStream(soc.getInputStream());
int number = inputFromClient.readInt();
for (int i=1; i<=number; i++)
{
f=f*i;
}
DataOutputStream outputToClient = new DataOutputStream (soc. getOutputStream());
outputToClient.writeInt(f);
}
catch(IOException e)
{
Dr. Bharti Sharma, SOC, Networking

}
Dr. Bharti Sharma, SOC, Networking

package socket;
import java.io.IOException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.io.*;
import java.net.Socket;
class A extends JFrame implements ActionListener
{
JButton jb1;
JTextField jt1, jt2;
JLabel lbl;
Socket soc;
Dr. Bharti Sharma, SOC, Networking

DataOutputStream toServer;
DataInputStream fromServer;
A()
{
jt1 = new JTextField();
jt1.setBounds(90, 50, 150, 30);
add(jt1);
jt2 = new JTextField();
jt2.setBounds(90, 80, 150, 30);
add(jt2);
lbl = new JLabel("Result :");
lbl.setBounds(90, 140, 150, 30);
add(lbl);
jb1 = new JButton("+");
jb1.setBounds(90, 200, 100, 30);
add(jb1);
Dr. Bharti Sharma, SOC, Networking

jb1.addActionListener(this);
setLayout(null);
setSize(600, 400);
setVisible(true);

try
{
System.out.println("Client is started ");
soc= new Socket ("localhost",9800) ;
toServer = new DataOutputStream(soc.getOutputStream());
fromServer = new DataInputStream(soc.getInputStream());

}
catch(Exception e)
{

}
Dr. Bharti Sharma, SOC, Networking

}
public void actionPerformed(ActionEvent e)
{
try
{
toServer.writeUTF(jt1.getText());
System.out.println(jt1.getText())
toServer.writeUTF(jt2.getText());
System.out.println(jt2.getText());
int c= fromServer.readInt();
System.out.println(c);
if (e.getSource().equals(jb1))
{
lbl.setText(Integer.toString(c));
}
}
catch(Exception e1)
Dr. Bharti Sharma, SOC, Networking

}
}
class client4
{
public static void main(String[] args)
{
new A();
}
}

Output :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy