HI everyone I have a little problem with an exercise I have been given for homework.

We were given sample server, client, compressedmessage, encryptedmessage documents to build upon.

Now what is meant to happen is the server listens on port 2000 for incoming client requests, once a client makes a request they are moved to a new socket to communicate and the server resumes listening on port 2000.

The client then proceeds to login using a username and password...the client encrypts these using a vignere cipher word. These are then decrypted on the server side and if they are correct the user is allowed to login if not they must try again.

Once the client logs in they ask for a document to be sent to them but before it is sent it is compressed and then uncompressed on the client side.

The server must be multithreaded to allow multiple clients to do the same thing.

I have to incorporate try/catch as well, which I'm new to.

My server file builds but doesn't display anything and just says "Press any key to continue..."

Any help would be appreciated thanks

import java.net.*;
import java.io.*;
import java.util.ArrayList;


public class P7_8_Server
{

	public static void main(String args[])
	{
		P7_8_Server server = new P7_8_Server();
	}

	public void processConnectionRequests()
	{
		int clientCount = 0;

		try
		{
			ServerSocket serverSocket = new ServerSocket(2000);

			while (true)
			{
				//create a socket to communicate with this client
				Socket client = serverSocket.accept();

				P7_8_ServerThread thread = new P7_8_ServerThread(client, clientCount);
				thread.start();
				clientCount ++;

			}

		}

		catch (UnknownHostException e)
		{
			System.err.println("Unknown host " + e);
		}
		catch (IOException e)
		{
			System.err.println("I/O problem " + e);
		}

	}

	class P7_8_ServerThread extends Thread
	{
		Socket threadSocket;
		int clientNumber;

		public P7_8_ServerThread(Socket c,int n)
		{
			threadSocket = c;
			clientNumber = n;
		}

		String getCompressedMessage(String compressText)
        {   // create & return a compressed message
        	CompressedMessage cm = new CompressedMessage(compressText);
            cm.compress();
            return cm.getMessage();
        }

		String getDecryptedMessage(String txt,String key)
        {   // create & return a decrypted message
        	EncryptedMessage em = new EncryptedMessage(txt,key);
            em.decrypt();
            return em.getMessage();
        }


		public void run()
		{

			String document[] =
			{
				"The old Clogher Valley railway joined the longer Great Northern railway at either end. The engines on the Clogher Valley line were mostly steam. For the longer trains two engines were needed. The line of the old railway can still be identified.",
		 		"We didn't have much and didn't expect much in the forties. The small cottage had a living-room and two bedrooms. The living-room was lit by an oil lamp. Candles lit the two small bedrooms. We didn't have mains water in the cottage so water came from the spring well. The water from the well was the best spring water in the area.",
		 		"The bus journey from Enniskillen to Belfast was very tedious. The bus was slow and there was no motorway. It was a two hour journey from Enniskillen to Armagh. Then there was a half hour wait at Armagh. Then another two hour journey to the Belfast bus station. So the complete Enniskillen to Belfast journey was four and a half hours, very different from the modern Enniskillen to Belfast bus via the motorway.",
		 		"My first job was doing farmwork. The wages were one pound per week - ten shillings for the house and ten shillings for me. The hours in summer for farmwork were longer but the wages were still one pound per week as in winter. My next job was in the forestry commission with wages ten shillings more. The hours for the forestry commission job were also better."
			};

			String username[] = {"mongroupa","mongroupb","tuegroupa","tuegroupb"};
			String password[] = {"bernardc","crosslandb","bcrossland","cbernard"};


			{
				try
				{
					DataInputStream serverInputStream = new DataInputStream(threadSocket.getInputStream());

					DataOutputStream serverOutputStream = new DataOutputStream(threadSocket.getOutputStream());

					//process logon request
					int logonSuccessful;
					do
					{
						//wait for client to send encrypted username and password
						String encryptedUname = serverInputStream.readUTF();
						String encryptedPword = serverInputStream.readUTF();

						System.out.println("Encrypted username received from client " + clientNumber + ": " + encryptedUname);
						System.out.println("Encrypted password received from client " + clientNumber + ": " + encryptedPword);

						//decrypt username and password
						String uname = getDecryptedMessage(encryptedUname, "mon");
						String pword = getDecryptedMessage(encryptedPword, "tue");

						System.out.println("Client " + clientNumber + " decrypted username: " + uname);
						System.out.println("Client " + clientNumber + " decrypted username: " + pword);

						//search list of valid usernames and passwords
						logonSuccessful = 0;
						for (int i=0;i<username.length;i++)
						{
							if (username[i].equals(uname))
							{
								if (password[i].equals(pword))
								{
									logonSuccessful = 1;
								}
								break;
							}
						}

						//inform client whether logon was successful
						serverOutputStream.writeInt(logonSuccessful);
						System.out.println("Valiue " + logonSuccessful + " sent to client");

					}while (logonSuccessful == 0);

					while (true)
					{
						//receive document number from client
						int i = serverInputStream.readInt();
						System.out.println("Value " + i + " received from client " + clientNumber);

						if (i==4) break;

						//compress document
						String compressedDocument = getCompressedMessage(document[i]);
						System.out.println("Document " + i + " compressed");

						System.out.println("Size of original document: " + document[i].length() + " characters");
						System.out.println("Size of compressed document: " + compressedDocument.length() + " characters");

						int compressionRatio = compressedDocument.length() * 100 / document[i].length();
						System.out.println("Compression ratio: " + compressionRatio + " percent");

						//send document to client
						serverOutputStream.writeUTF(compressedDocument);
						System.out.println("Compressed document " + i + " sent to client");
					}

					serverInputStream.close();
					serverOutputStream.close();
				}
				catch (IOException e)
				{
					System.err.println("I/O problem " + e);
				}

			//	client.close();
			//	serverSocket.close();
				System.out.println("Server closing down");
			}
		}

	}
}

Ok i've spotted my error i forgot to set off the processConnectionRequests method in the main

silly me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.