Pregunta

i'm creating a program that reads from file then change it to an arraylist of item and send it to to a client. A client chooses an item by id and enter amount needed. then it gets updated in the server. the server runs multithreading. when another client invoke the server, updated amount will be given to the client.

i'm having problem with the codes at the client side. what happen was i can't enter the id and the amt because the program close before i can enter the values.

public class ListClient {

public static void main(String[] args) throws ClassNotFoundException, IOException {

    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;
    try {
        int id = 0;
        int amt = 0;
        Socket s1 = new Socket("localhost", 2001);
        ois = new ObjectInputStream((s1.getInputStream()));

        System.out.println("Connected to test server");
        ListFacade2 lf = new ListFacade2();

        List<Item> lm = (ArrayList<Item>) ois.readObject();
        lf.setItemList(lm);
       Item it = lf.pickItem();
        System.out.println("Item " + it.getId() + " Amount " + it.getQty_left());
        if(it.getQty_left()>0){
        try (DataOutputStream dos = new DataOutputStream(s1.getOutputStream())) {
            System.out.println("Enter the item id that you want:\n");
            dos.writeInt(id);
            System.out.println("Enter the amount of item that you want:\n");
            dos.writeInt(amt);
            dos.flush();
        }}
        else 
            System.out.println("Item out of stock");
    } finally {
        try {
            if (ois != null) {
                ois.close();
            }
            if (oos != null) {
                oos.close();
            }
        } catch (IOException ex) {
        }
    }
}  // end main
}

this is the code from the server side

public class ListServer {

public static void main(String[] args) {

    try {
        ServerSocket sSoc1 = new ServerSocket(2001);
        while (true) {
            Socket inSoc1 = sSoc1.accept();
            ListThread lt = new ListThread(inSoc1);
            lt.start();
        }
    } catch (Exception e) {
        System.out.println("Oh Dear! " + e.toString());
    }
}
}

class ListThread extends Thread {

Socket threadSoc1;
ListFacade lf = new ListFacade();

ListThread(Socket inSoc1) throws IOException, ClassNotFoundException {
    threadSoc1 = inSoc1;
    lf.readItemList();
}

@Override
public void run() {
    try {

        ObjectOutputStream oos = new ObjectOutputStream((threadSoc1.getOutputStream()));
        System.out.println("server Soc1ket runs");

        oos.writeObject(lf.getListItem());
        DataInputStream dis = new DataInputStream(threadSoc1.getInputStream());
        int id = dis.readInt();
        int amt = dis.readInt();
        lf.updateItem(id, amt);
        lf.displayItem();

    } catch (Exception e) {
        System.out.println("Whoops! " + e.toString());
    }

    try {
        threadSoc1.close();
    } catch (Exception e) {
        System.out.println("Oh no! " + e.toString());
    }
}
}

is it because of return null in the pickItem() ?

public Item pickItem() throws IOException, ClassNotFoundException {
    displayItem();
    System.out.print("Please Key in item id number from above list:  ");
    BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
    Integer itno = Integer.parseInt(b.readLine());
    for(int i = 0; i < im.size();i++){
        if(im.get(i).id.equals(itno))    

            return im.get(i);

        else
            System.out.println("No such item");
    }
    return null;
}
¿Fue útil?

Solución

See first example here : Scanner

If you want to query the user for input, you'll have to read from System.in (do it with a Scanner or the like).

Like this ...

Scanner fromUser = new Scanner(System.in);
try (DataOutputStream dos = new DataOutputStream(s1.getOutputStream())) {
        System.out.println("Enter the item id that you want:\n");
        id = fromUser.nextInt();
        dos.writeInt(id);
        System.out.println("Enter the amount of item that you want:\n");
        amt = fromUser.nextInt();
        dos.writeInt(amt);
        dos.flush();
    }

Of course, there are more ways to achieve the same goal. That's just one ...

Otros consejos

Don't mix different types of stream on the same socket. Use the ObjectInputStream and ObjectOutputStream for everything, at both ends. They have all the methods you need. If you mix, you will run into buffering issues where one steals data from the other.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top