Please do not attempt this question if you are not sure about it. Please send me
ID: 3678333 • Letter: P
Question
Please do not attempt this question if you are not sure about it. Please send me the complete Java code with OUTPUT(screen shots) only if it is working at your end.
QUESTION: Complete the following client-server program in JAVA to do the following:
a) The server sends 1, or 2 to the client.
b) If the client receives 1, it draws a rectangle. If the client receives 2, it draws a circle.
Please use the follwoing sample code and provide me the complete solution :
Server Code:
===================================
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class MyServer {
public static void main(String[] args){
//Complete this method.
}
}
Client Code:
==========================================
import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.net.*;
public class MyClient extends Applet {
private int s;
public void init(){
//Complete this method.
setSize(400, 400);
try{
//You may need repaint();
repaint();
}catch(Exception e){
System.out.println("Error: " + e);
System.exit(0);
}
}
public void paint(Graphics page){
//Complete this method.
}
}
Explanation / Answer
Server code:
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e){System.out.println(e);}
}
}
Client Code:
import java.awt.*;
class GraphicsProgram extends Canvas{
public GraphicsProgram(){
setSize(400, 400);
setBackground(Color.white);
}
public static void main(String[] argS){
//GraphicsProgram class is now a type of canvas
//since it extends the Canvas class
//lets instantiate it
GraphicsProgram GP = new GraphicsProgram();
//create a new frame to which we will add a canvas
Frame aFrame = new Frame();
aFrame.setSize(400, 400);
//add the canvas
aFrame.add(GP);
aFrame.setVisible(true);
}
public void paint(Graphics g){
g.setColor(Color.blue);
g.drawRect(20, 150, 100, 100);
g.drawoval(50, 50, 100, 100);
Image img1 = Toolkit.getDefaultToolkit().getImage("example.jpg");
g.drawImage(img1, 140, 140, this);
}
}