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

General assignment Create a client application that connects to the Bushey serve

ID: 3572727 • Letter: G

Question

General assignment Create a client application that connects to the Bushey server on the POLY ember network. Once connected you should ask the server for a favorite color. When the server responds, you should pop up a frame that has that color. Details Write a client application (2 java classes in a package) that connects to my Bushey server running on SERVER: fa16-cop3330.hpc.lab 2016 Port: When connected, query the server about a favorite color. It will respond with a string-red, green, or blue. You should popup a frame with that color. Example screen shots: Client What is your favorite color Attempting connection Connected to fa16-cop3330 hpclab CLIENT What is your favorite color SERVER Red

Explanation / Answer

Please find the programs for Server and Client below:

Execution Steps (after compiling):

1. execute server as - java Server 2016

2. execute client as - java Client localhost 2016.

Each time you need different color to be displayed, change accordingly in Server class and compile and execute.

Please give colors in Server class as - Red/Blue/Green.

Server Code:

// Server Program

import java.net.*;
import java.io.*;

public class Server extends Thread {

private ServerSocket serverSocket;

public Server(int port) throws IOException {
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(10000);
}

public void run() {
while(true) {
try {
System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
// Creating server
           Socket server = serverSocket.accept();
  
System.out.println("Connected to " + server.getRemoteSocketAddress());
DataInputStream in = new DataInputStream(server.getInputStream());
  
System.out.println(in.readUTF());
// Creating output stream
           DataOutputStream out = new DataOutputStream(server.getOutputStream());
  
           // Sending Red color from server.
           out.writeUTF("Red");
server.close();
  
}catch(SocketTimeoutException s) {
System.out.println("Socket timed out!");
break;
}catch(IOException e) {
e.printStackTrace();
break;
}
}
}

public static void main(String [] args) {
int port = Integer.parseInt(args[0]);
try {
       // Creating Thread to wait for Client to connect.
Thread t = new Server(port);
t.start();
}catch(IOException e) {
e.printStackTrace();
}
}
}

Client Code:

// Client program

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Client {

   public static void main(String [] args) {
       // Frame for Popup window
       JFrame mainFrame;
       String serverName = args[0];
      
       int port = Integer.parseInt(args[1]);
       try {
System.out.println("Connecting to " + serverName + " on port " + port);
Socket client = new Socket(serverName, port);

System.out.println("CLIENT >>> What is your favourite color");
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);

out.writeUTF("Hello from " + client.getLocalSocketAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);

String clr = in.readUTF();
System.out.println("Server >>> " + clr);
Color c = null;

switch(clr){
case "Red": c = Color.RED;
                break;
case "Green": c = Color.GREEN;
                break;
case "Blue" : c = Color.BLUE;
                break;
}

mainFrame = new JFrame();
mainFrame.getContentPane().setBackground(c);
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
  
mainFrame.setVisible(true);
client.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote