Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is my 3rd attempt to get the correct answer for the same question, since ot

ID: 3679300 • Letter: T

Question

This is my 3rd attempt to get the correct answer for the same question, since other two people didn't even understand the question, but sent me wrong answer.

Please do not attempt this question if you are not sure about it. Please send me the complete Java code(both server and client) with OUTPUT(screen shots) only if it is working at your end.

QUESTION: Complete the below client-server program in JAVA to do the following. (You'll need to fill in the gaps where it says " //Complete this method." and also, make sure you are extending Applet for the client program.)

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 code and provide me the complete solution after writing and testing all the methods: All you need to do is, complete the methods where it says " //Complete this method.", run it at your end to make sure If the client receives 1 from Server, it draws a rectangle and If the client receives 2, it draws a circle. Then, send me the complete code along with screen shots for the outputs.

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.net.*;
import java.io.*;
import java.util.*;
public class MyServer   
{
public static void main(String args[]) throws Exception   
{
ServerSocket sersock = new ServerSocket(7000);
System.out.println("Server ready........");
Socket sock = sersock.accept( );
   
OutputStream ostream = sock.getOutputStream();
BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(ostream));
   System.out.println("ENter Number");
   Scanner s = new Scanner(System.in);
   String s2 = s.nextLine();
   bw1.write(s2);

bw1.close(); ostream.close(); sock.close(); sersock.close( );
}
}

Client Code

import java.io.*;
import java.net.*;   
import java.applet.*;
import java.awt.*;
/*<applet code = "MyClient" width = "500" height = "500">
</applet>*/
public class MyClient extends Applet
{
   int x;
public void init()
{

   try
   {
       Socket sock = new Socket("localhost", 7000);
       InputStream istream = sock.getInputStream();
       BufferedReader br1 = new BufferedReader(new InputStreamReader(istream));

       String s1 = br1.readLine();
       System.out.println(s1);
       processMessage(s1);

       br1.close(); istream.close(); sock.close( );
   }
   catch(Exception e)
       {
           System.out.println(e.getMessage());
       }
}

private void processMessage(String message) throws IOException
{
if (message.equals("1") )
   x = 1;
   else
       x = 2;
   repaint();
}

public void paint(Graphics g)
   {
       if(x==1)
           g.drawRect(100,100,50,30);
       if(x==2)
           g.drawOval(100,100,100,100);
   }      
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote