从第二个客户端连接上的服务器输出的图像图标输出

时间:2016-04-19 14:13:59

标签: java image sockets server client

尝试将图像从简单的服务器应用程序输出到客户端时遇到问题。当第一个客户端尝试连接时,会抛出一个错误,指出它们无法连接并显示以下错误:

Exception in thread "Thread-2" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at ClientHandler.run(AuctionServer.java:456)
java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
    at java.io.ObjectOutputStream$BlockDataOutputStream.writeByte(ObjectOutputStream.java:1915)
    at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1576)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:351)
    at AuctionServer.main(AuctionServer.java:71)

我认为这是在对象输出和通过客户端接收期间引起的,但我不确定。我提供了相信相关的代码行:

服务器端:

    public static void main(final String[] args)
                            throws IOException
    {
        ServerSocket serverSocket = null;
        final int PORT = 1234;
        Socket client = null;
        final ClientHandler handler = null;
        Scanner getCurrentTime;

        try
        {
            serverSocket = new ServerSocket(PORT);
        }
        catch (final IOException ioEx)
        {
            System.out.println("\nPort unavailable");
            System.exit(1);
        }

        System.out.println("\nThe server connection has been established\n");

        String timeNow = null;
        getCurrentTime = new Scanner(System.in);

        System.out.println("\nPlease set the end time for Item1");
        timeNow = getCurrentTime.nextLine();
        final Auction itemOne = new Auction("123456", "Gold watch",timeNow,
                "Antique watch, gold as new");

        System.out.println("\nPlease set the end time for Item2");
        timeNow = getCurrentTime.nextLine();
        final Auction itemTwo = new Auction("223344", "Football ticket",timeNow,
                "1966 World Cup Final Signed Ticket");

        itemOne.start();
        itemTwo.start();


        final Users user = new Users(itemOne,itemTwo);
        final ArrayList<ClientHandler> users =
                                new ArrayList<ClientHandler>();
        final ArrayList<String> bidders = 
                                new ArrayList<String>();


        do
        {
            client = serverSocket.accept();
            System.out.println("Please enter your name: ");
            bidders.add(getCurrentTime.nextLine());
            bidders.add(client.getInetAddress().toString());
            Bidder bidder = new Bidder(bidders);
            System.out.println("\nUser" + (user.getNumOfUsers()+1)
                                            + " has been accepted to the auction system.\n");
            user.incUsers(users, client, handler);
            System.out.println(bidder.returnUsers());
            //maybe insert a try block
            try
            {

            ObjectOutputStream outStream =
                    new ObjectOutputStream(client.getOutputStream());

            outStream.writeObject(new ImageIcon("beesting.jpg"));

            outStream.flush();
            }
            catch(IOException ioEx)
            {
                ioEx.printStackTrace();
            }

        }while (true);
    }
}

客户方:

private static void sendMessages()
{
    String serverResponse;

    try
    {
        socket = new Socket(host,PORT);
        netOutput = new PrintWriter(
                socket.getOutputStream(),true);
        netInput = new Scanner (socket.getInputStream());
        serverResponse = netInput.nextLine();
        itemArray[0]=messageReceived(0,serverResponse);
        itemArray[1]=messageReceived(2,serverResponse);
        comboBox.addItem(itemArray[0]);     //Adds items to drop down.
        comboBox.addItem(itemArray[1]);
        comboBox.removeItemAt(0);
        comboBox.removeItemAt(0);
        itemInformation.append("\n" + netInput.nextLine()); //edits the text area to show bids etc.
        itemInformation.append("\n" + netInput.nextLine());
        itemInformation.append("\n" + netInput.nextLine());
        itemInformation.append("\n" + netInput.nextLine());
        itemInformation.append("\n" + netInput.nextLine());
        itemInformation.append("\n" + netInput.nextLine());

        ObjectInputStream inStream = new ObjectInputStream(socket.getInputStream());
        image = (ImageIcon)inStream.readObject();

    }
    catch(final IOException ioEx)
    {
        JOptionPane.showMessageDialog(frame,
                "Sorry we couldn't connect you! Please try again.");
        System.exit(1);
    }
    catch(ClassNotFoundException cnfEx)
    {
        cnfEx.printStackTrace();
    }
    JImage.setIcon(image);
}

0 个答案:

没有答案