Practicequestion
Practicequestion
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;
}
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 :