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

Create an application that uses a socket connection to allow a client to specify

ID: 3679097 • Letter: C

Question

Create an application that uses a socket connection to allow a client to specify a file name of a text file and have the server send the contents of the file or indicate the file does not exist. The server must contain a text based password file ("user name" & "password"). The client must pass a valid username and password to establish a connection with the Server (see Note 1).

Notes:

1. A much better approach would be to encrypt the password file. The Java Cryptographic Extension (available since JDK 1.4) provides an API.

Java Cryptography Architecture Reference Guide (JCA Guide) - http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#Introduction (Links to an external site.)
Introduction, Code Examples - Computing a Message Digest Object (i.e. one-way hash; using SHA-1)

Explanation / Answer

Application that uses a socket connection:

1.Client Class:

import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.util.Formatter;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class
Client extends JFrame implements ActionListener
{
private JTextField enterField;
private JTextArea displayArea;
private Scanner input;
private Formatter output;
private Socket connection;
private JPanel panel;
private JLabel label;
private JScrollPane scroller;
public Client()
{
label = new JLabel ( " Enter the FILE NAME to retrieve: ");
panel = new JPanel();
panel.setLayout ( new GridLayout (1, 2, 0, 0));
panel.add( label );
enterField = new JTextField();
enterField.addActionListener( this );
panel.add( enterField );
displayArea = new JTextArea();
scroller = new JScrollPane( displayArea );
setLayout( new BorderLayout() );
add( panel, BorderLayout.NORTH);
add( scroller, BorderLayout.CENTER);
try
{
connection = new Socket( InetAddress.getLocalHost(), 7985 );
output = new Formatter(connection.getOutputStream());
output.flush();
input = new Scanner( connection.getInputStream() );
}
catch( IOException ioexception)
{
ioexception.printStackTrace();
}
setSize(400, 200);
setVisible( true );
}
public void actionPerformed(ActionEvent event)
{
try
{
String fileName = event.getActionCommand() + " ";   
output.format( fileName );
output.flush(); // send to server   
String inputLine = input.nextLine();
displayArea.setText( inputLine );
if (inputLine.equals( "The file contains:"))   
{
while ( input.hasNextLine() );

{
inputLine = input.nextLine();
displayArea.append(inputLine + " ");
}
}
enterField.setEditable( true );
enterField.setBackground( Color.red );
}
finally
{   
try
{
input.close();   
output.close();
connection.close();
}
catch (IOException ioexception)   
{   
ioexception.printStackTrace();
System.exit(1);
}
}
}
}

2.Server Class:

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.File;
import java.io.FileReader;
import java.util.Formatter;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Server extends JFrame
{
private Formatter output;
private Scanner input;
private ServerSocket server;
private Socket connection;

public Server()
{
try
{   
server = new ServerSocket( 7985, 10 );

}   
catch (IOException exception)
{   
exception.printStackTrace();   
System.exit(0);
}
}
public void runServer()
{   
try
process connections   
{
connection = server.accept();
output = new Formatter(connection.getOutputStream());
output.flush();   
input = new Scanner(connection.getInputStream());   
File inputFile = new File(input.nextLine());
String result;
if ( inputFile.exists())
{
Scanner fileInput = new Scanner( inputFile );
output.format("The file contains:" );
output.flush();
while (fileInput.hasNextLine() )   
{
result = fileInput.nextLine();
output.format( result );
output.flush();
}
}
else
{
result = inputFile.getName() + " does not exist! ";
output.format( result );   
output.flush();
}
}
catch (IOException ioException)   
{
ioException.printStackTrace();
System.exit(0);   
}
finally   
{   
try   
{   
output.close();   
input.close();
connection.close();   
}

catch (IOException ioException)   
{   
ioException.printStackTrace();   
System.exit(0);   
}

}
}
}

3...Client test class:

import javax.swing.JFrame;
public class ClientTest
{
public static void main( String[] args )
{   
Client application = new Client();

application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}

4.Server test class:

import javax.swing.JFrame;
public class ServerTest
{
public static void main( String[] args )
{   
Server application = new Server();
application.runServer();
}
}

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