Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 01-09-2010, 01:01 AM   PM User | #2
cs_student
Regular Coder

 
cs_student's Avatar
 
Join Date: Oct 2009
Location: ~/
Posts: 195
Thanks: 2
Thanked 22 Times in 22 Posts
cs_student is an unknown quantity at this point
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.

Regards,


cs_student
__________________
Get GNU/Linux - Play Ogg - Vim
Using Arch Linux x86_64, Xorg + xmonad
cs_student is offline   Reply With Quote
Reply

Bookmarks

Tags
client, program, server, shop, tcp

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:02 PM.


Advertisement
Log in to turn off these ads.