Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programing Assignment: Client & Server Socket programming -Use the code provided below for creating your java classes and added it to your development environment. -

Programing Assignment: Client & Server Socket programming

-Use the code provided below for creating your java classes and added it to your development environment.

- Use of Eclipse development tool is optional, you may use any editor of your choice such as notepade++, etc.

- Refer to instructions below to compile and execute the program.

- There are "To Do" placeholders within each program where you are required to add the correct code and complete the program.

4- Upload your working server.java and client.java code under this assignment. Your code must compile and run without any errors or pointe will be deducted.

References: java docs reference API for socket classes: https://docs.oracle.com/javase/8/docs/api/index.htmlLinks to an external site.

Instruction for Eclipse IDE:

Create the classes in your workspace as was demonstrated in the class.

Right click on each class (server 1st) Run As Java Application

Instruction for java execution from the command line:

Include the Java Binary directory (bin) in the PATH environment variable.

Suppose that your java SDK is installed in directory " C:\Program Files\Java\jre1.8.0xxxx ". From "Control Panel" (Optional) System and Security System Advanced System Settings "Advanced" tab Environment Variables System Variables Select variable named "PATH" Edit Add "c:\java\bin;" in front of the existing PATH entry. Note that the semi-colon serves as the directory separator to separate java from the rest of directory paths.

Copy the code below in the directory of your choice.

Complete the to-do sections in the code.

Open 2 command prompts.

Go to the directory where you saved the .java codes.

Type javac server.java and javac client.java

In each command prompt type java server and java client.

Server.java

package serverSide;

import java.io.IOException;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.Date;

/**

* A TCP server that runs on port 7070. When a client connects, it

* sends the client the current date and time, then closes the

* connection with that client.

*/

public class Serveroriginal {

/**

* Runs the server.

*/

public static void main(String[] args) throws IOException {

/* Create the socket listner

* using ServerSocket Java class

* for Listening on a socket.

* Use port 7070*/

/* TO DO here */

System.out.println("ServerSocket running on port 7070");

try {

while (true) {

/** To Do

* Accept client connection here

*/

/* TO DO here*/

try {

System.out.println("Now listening on port 9090");

PrintWriter out =

new PrintWriter(socket.getOutputStream(), true);

out.println(new Date().toString());

} finally {

System.out.println("terminated running on port 9090");

/* close the communication channel here

*/

/* To Do Here */

}

}

}

finally {

/* close the communication channel here

*/

/* To Do Here */

}

}

}

-----------------------------------------------------------------------------------------------------------------------------------

Client.java

package clientSide;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.InetSocketAddress;

import java.net.Socket;

import javax.swing.JOptionPane;

/**

* client for the date server.

*/

public class Client {

/**

* Runs the client application to read date and time from the server side..

*/

public static void main(String[] args) throws IOException {

String serverAddress = "";

/** To Do

* Create a socket and connects it to the specified

* port number you created in the server side.

* Hint: Use the constructor with host name and port as parameters

*/

/* TO DO Here*/

BufferedReader input =

new BufferedReader(new InputStreamReader(s.getInputStream()));

String serverTime = input.readLine();

/** To Do

* Write code to disconnect Here.

*/

/* TO DO Here*/

System.exit(0);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Sham Navathe

4th Edition

0321122267, 978-0321122261

More Books

Students also viewed these Databases questions