TCP Client/ Server Program for CD Shop

TCP Client/ Server Program for a CD shop with 5 inventory

Page 1 of 1

1 Replies - 2624 Views - Last Post: 08 January 2010 - 06:18 PM Rate Topic: -----

#1 larrybalz4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-January 10

TCP Client/ Server Program for CD Shop

Posted 08 January 2010 - 01:28 AM

Kinda Stuck!! :crazy:
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
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
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






Is This A Good Question/Topic? 0
  • +

Replies To: TCP Client/ Server Program for CD Shop

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: TCP Client/ Server Program for CD Shop

Posted 08 January 2010 - 06:18 PM

There are people who really like to complicate their life
If both your Client and Server are in Java written by the same coder coming from the same application
just use ObjectInput/Output Stream oin your socket and simply transfer the and exchange CdObject from the Client and Server
and actually you can also store the CdObject as object into your file using an ObjectReader/Writer
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1