Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2010
    Thanked 0 Times in 0 Posts

    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


    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;
        // 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);
            catch (IOException e)

    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
    //Instantiates a new socket with the destination IP address and the listening port number
                cdShopSocket = new Socket("", 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
                in = new BufferedReader(new InputStreamReader(
    //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
    //Writes the received "echo" line to the screen
                    System.out.println("User Input: " + in.readLine());
    //Close all the input and output streams
            catch(Exception e)
                System.out.println("Error: "+e.toString());
        }//end main
    }//end cdShopClient

  2. #2
    Regular Coder cs_student's Avatar
    Join Date
    Oct 2009
    Thanked 22 Times in 22 Posts
    It looks as though you have found some sort of skeleton program for echoing a statement from a server back to a client.

    However, I don't see any attempt to actually solve the problem you have presented.

    Furthermore, your specifications which you have provided us are quite ambiguous which does not allow me to definitively decide how I could help you.

    Does everything need to be stored in a file (and updated in said file). Or can you have a Inventory class which keeps track of everything and writes to the file when all transactions are done.

    If you have an Inventory class you can simply serialize it at and send it to the client to use initially. You should then be able to update it from the client and relay those updates to the server.




Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts