View Single Post
Old 01-08-2010, 05:29 PM   PM User | #1
larrybalz4life
New to the CF scene

 
Join Date: Jan 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
larrybalz4life is an unknown quantity at this point
TCP Client/ Server Program for CD Shop

I am working on a CD Shop programme with 5 inventory that are to be read from a file. The file inventory is seperated by tab. TCP Client/ Server Program for a CD shop with 5 inventory (Item code, CD name, Artist Name, Price, and Quantity in Stock which would be read from a file seperated by tabs.)

I have sucessfully connected to the tcp client and server program but i cannot make the following changes sucessfully.
-The server waits for client requests and transmits the inventory if an "I" is received.
-If a "B" followed by an item code is received, the quantity in stock should be updated to reflect that an item has been bought.
-A client program should connect to the server, download the inventory and display it to the customer.
-The customer should be able to buy any item by typing in the item's code.
-A shopping trolley should keep track of the items selected by the customer and give a total price for the whole transaction.
- No other input should work once connected except the following key letters above.
Any help would be greatly appreciated

cdShopServer.java

Code:
import java.io.*;
 
import java.net.*;
 
 
 
public class cdShopServer{
 
    public static void main(String [] args){
 
//Server socket (can have a listening port to receive connections)
 
        ServerSocket cdShopServer;
 
//Declares an InputStreamReader object to read from the client socket
 
        InputStreamReader r;
 
//Declares a BufferedReader to read from InputStreamReader
 
        BufferedReader br;
 
//Declares a PrintWriter object to write to the client socket
 
        PrintWriter os;
 
        Socket clientSocket;
 
        // Try to open a server socket on port 1234
 
        // port less than 1023 cnt be used reserved privileged users (root)
 
        String line;
 
        try
 
        {
 
    // Create a socket object from the ServerSocket to listen and accept
 
    // connections.
 
    // Open input and output streams
 
            cdShopServer=new ServerSocket(1234);
 
 
 
            clientSocket = cdShopServer.accept();
            System.out.print("Server has been connected!\n");
 
            r=new InputStreamReader(clientSocket.getInputStream());
 
            br=new BufferedReader(r);
 
            os = new PrintWriter(clientSocket.getOutputStream(),true);
 
        // As long as we receive data, echo that data back to the client.
 
            while (true)    //This server stays active for ever
 
            {
 
                line = br.readLine();
 
                System.out.println("Received: "+line);
 
                os.println(line);
 
            }
 
        }
 
        catch (IOException e)
 
        {
 
            System.out.println(e);
 
        }
 
    }
 
}
cdShopClient

Code:
import java.io.*;    //Imports java input-output libraries (for StreamReaders)
 
import java.net.*;    //Imports java network libraries (for sockets)
 
 
 
public class cdShopClient
 
{
 
    public static void main(String[] args)
 
    {
 
 
 
            Socket cdShopSocket;    //Declares a Socket
 
            PrintWriter out;    //Declares a PrintWriter object. This will be used
 
            //to write to the socket
 
            BufferedReader in;    //Declares a Buffered reader. This object will be
 
            //used to read from the socket
 
 
 
            try
 
        {
 
 
 
//Instantiates a new socket with the destination IP address and the listening port number
 
            cdShopSocket = new Socket("136.147.107.117", 1234);
 
 
 
//Creates a new output stream in order to write to the socket
 
            out = new PrintWriter(cdShopSocket.getOutputStream(),true);
 
 
 
//Input from the socket with a bufferedreader
4
            in = new BufferedReader(new InputStreamReader(
 
                cdShopSocket.getInputStream()));
 
 
 
//Input from the keyboard with a bufferedreader
 
            BufferedReader stdIn = new BufferedReader(
 
                new InputStreamReader(System.in));
 
 
 
            String userInput;
 
            while ((userInput = stdIn.readLine()) != null)
 
            {
 
 
 
//Writes the user input into the socket for transmission
 
                out.println(userInput);
 
 
 
//Writes the received "echo" line to the screen
 
                System.out.println("User Input: " + in.readLine());
 
            }
 
 
 
//Close all the input and output streams
 
            out.close();
 
            in.close();
 
            stdIn.close();
 
            cdShopSocket.close();
 
        }
 
        catch(Exception e)
 
        {
 
            System.out.println("Error: "+e.toString());
 
            System.exit(-1);
 
        }
 
    }//end main
 
}//end cdShopClient
larrybalz4life is offline   Reply With Quote