0

我的客户端-服务器应用程序有问题。程序正确运行,直到客户端第二次必须输入数据。在调试代码时,客户端似乎总是在linesIn = din.readInt();循环的第二次迭代中崩溃。

正如建议的那样,我在整数解析周围添加了一个 try catch 块,它似乎有助于将程序进一步移动,但现在运行时,程序只会向用户提供一次正确的输出,之后将显示可用选项列表两次然后不接受任何进一步的输入

客户端中显示以下输出:

    The address and hostname are: BOO-PC/192.168.0.8
The Hostname: BOO-PC
The Address only: 192.168.0.8
Server: Welcome please make your selection from the options below 
Server:  1: make a booking 
Server:  2: cancel a booking 
Server:  3: view available films 
Server:  4: logout
Client: 
3
Server: Title :Film1
Server:  Time : 10.3
Server:  Date : 10 7 2013
Server:  Price : 5.0
Server:  Referance Number : 55
Server: 
Client: 

Server:

和服务器上的这个错误

Exception in thread "serverThread" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:481)
at java.lang.Integer.parseInt(Integer.java:527)
at systemssoftwarebookingsystem.serverThread.run(serverThread.java:45)

服务器线程代码:

package systemssoftwarebookingsystem;

import java.net.*;
import java.io.*;
import java.util.Scanner;

public class serverThread extends Thread
{

    private Socket socket = null;
    private PrintWriter write = null;
    private BufferedReader read = null;

    public serverThread(Socket socket)
    {
        super("serverThread");
        this.socket = socket;
    }

    public void run()
    {
        Scanner in;
        in = new Scanner(System.in);
        char[] input;
        int inputReferance = 0;
        int inputSeatAmount = 0;
        boolean filmFound = false;
        boolean exit = false;
        try
        {
            read = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            write = new PrintWriter(new DataOutputStream(socket.getOutputStream()));
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            int choice = 0;

            while (exit == false)
            {
                choice = 0;
                out.writeInt(5);
                write.println("Welcome please make your selection from the options below \n 1: make a booking \n 2: cancel a booking \n 3: view available films \n 4: logout");
                write.flush();
                input = new char[1];

                read.read(input);
                choice = Integer.parseInt(new String(input));

                switch (choice)
                {
                    case 1:
                    {
                        Film tempFilm = null;
                        out.writeInt(1);
                        write.println("Please enter the referance number of the film you wish to watch");
                        write.flush();

                        read.read(input);
                        inputReferance = Integer.parseInt(new String(input));

                        for (Film s : server.filmList)
                        {
                            if (s.referanceNumber == inputReferance)
                            {
                                filmFound = true;
                                tempFilm = s;
                            }
                        }

                        if (filmFound == false)
                        {
                            out.writeInt(1);
                            write.println("Sorry.  That referance number was not recognised.  Please try again");
                            write.flush();
                            break;
                        }

                        if (filmFound == true)
                        {
                            out.writeInt(1);
                            write.println("Please enter the numbe rof seats you wish to book");
                            write.flush();
                            inputSeatAmount = read.read(input);
                            inputSeatAmount = Integer.parseInt(new String(input));

                            if (tempFilm.seatsAvailable < inputSeatAmount)
                            {
                                out.writeInt(2);
                                write.println("Sorry.  There are not enough seats available for that film \n Only " + tempFilm.seatsAvailable + "seats are available");
                                write.flush();
                                break;
                            }

                            server.bookings.add(new Booking(inputReferance, server.bookingNumber, inputSeatAmount));
                            server.bookingNumber++;

                            out.writeInt(1);
                            write.println("booking placed successfully");
                            write.flush();
                            break;
                        }
                    }

                    case 2:
                    {
                        out.writeInt(1);
                        write.println("Please enter the booking number of the booking you wish to cancel");
                        write.flush();
                        inputReferance = read.read(input);
                        inputReferance = Integer.parseInt(new String(input));

                        for (Booking s : server.bookings)
                        {
                            if (s.bookingNumber == inputReferance)
                            {
                                filmFound = true;
                                break;
                            }
                        }

                        if (filmFound == false)
                        {
                            out.writeInt(1);
                            write.println("Sorry.  That booking number was not recognised.  Please try again");
                            write.flush();
                            break;
                        }

                        server.bookings.remove(inputReferance);

                        out.writeInt(1);
                        write.println("booking cancelled");
                        write.flush();
                        break;
                    }

                    case 3:
                    {
                        for (Film s : server.filmList)
                        {
                            out.writeInt(6);
                            write.println("Title :" + s.name + "\n Time : " + s.showingTime + "\n Date : " + s.day + " " + s.month + " " + s.year + "\n Price : " + s.price + "\n Referance Number : " + s.referanceNumber + "\n");
                            write.flush();
                        }
                        out.writeInt(1);
                        write.println("end of list");
                        write.flush();
                        break;
                    }

                    case 4:
                    {
                        exit = true;
                        out.writeInt(1);
                        write.println("Bye.");
                        write.flush();
                        break;
                    }
                }
            }
            socket.close();

        } catch (IOException e)
        {
            e.printStackTrace();
        }

    }
}

客户端代码:

package syssoftclient;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class Syssoftclient
{

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

        try
        {
            InetAddress internetAddress = InetAddress.getLocalHost();
            System.out.println("The address and hostname are: " + internetAddress);
            System.out.println("The Hostname: " + internetAddress.getHostName());
            System.out.println("The Address only: " + internetAddress.getHostAddress());

        } catch (UnknownHostException e)
        {
            System.err.println("Connection problem " + e);
        }

        Socket SocketC = null;
        PrintWriter out = null;
        BufferedReader in = null;

        boolean exit = false;

        try
        {
            SocketC = new Socket(InetAddress.getLocalHost().getHostName(), 8888);
            out = new PrintWriter(SocketC.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(SocketC.getInputStream()));

        } catch (UnknownHostException e)
        {
            System.err.println("Host not recognised");
            System.exit(1);
        } catch (IOException e)
        {
            System.err.println("Couldn't get I/O for the connection");
            System.exit(1);
        }

        String fromServer = null;
        String fromUser = null;
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

        DataInputStream din;
        din = new DataInputStream(SocketC.getInputStream());

        int lineCount = 0;
        int linesIn = 0;

        while (exit == false)
        {
            lineCount = 0;
            linesIn = din.readInt();

            while (lineCount < linesIn)
            {
                fromServer = in.readLine();
                System.out.println("Server: " + fromServer);
                lineCount++;
            }

            if ("Bye.".equals(fromServer))
            {
                System.out.println("Quitting");
                exit = true;
                break;
            }

            System.out.println("Client: ");

            fromUser = stdIn.readLine(); //reads userinput

            if (fromUser != null)
            {
                out.println(fromUser); //sends user input to the server
                out.flush();
            }

            if ("exit".equals(fromUser))
            {
                exit = true;
            }
        }

        out.close();
        in.close();
        stdIn.close();
        SocketC.close(); //close everything before ending program

    }
}
4

1 回答 1

0

根据您更新的问题,我认为这里有错误:

choice = Integer.parseInt(new String(input));

我不确定,因为我不确切知道第 45 行是什么。

您正在尝试将换行符解析为整数。这会引发异常,导致服务器退出其 while 循环,并关闭程序。

因此,将您的解析例程包装在 try/catch 块中并处理它们。错误应该会消失。

于 2013-08-21T15:42:16.243 回答