多线程客户端/服务器,套接字异常

时间:2014-04-27 23:38:59

标签: java sockets client-server

应该是一个我无法纠正的简单修复。我在服务器和客户端之间返回总计算,价格*数量。但是,我收到了java.net.SocketException: Connection Reset。我已经插入了ComputerServer类w / class HandleAClientComputerClient类和Computer类。我欢迎任何帮助。谢谢!

import java.io.*;
import java.util.*;
import java.net.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;

public class ComputerServer 
{
    public static void main(String args[])
    {
        ServerSocket serverSocket;
        Socket connection;

        ObjectInputStream input;
        ObjectOutputStream output;

        Computer c = null;

        Object obj;

        double totalCharge;

        try
        {
            serverSocket = new ServerSocket(8000);
            System.out.println("Waiting for Client");

            int clientNo = 1;

            ExecutorService threadExecutor = Executors.newCachedThreadPool();

            while(true)//runs indefinitely
            {
                connection = serverSocket.accept();

                input = new ObjectInputStream(connection.getInputStream());
                output = new ObjectOutputStream(connection.getOutputStream());

                obj = input.readObject();

                System.out.println("Object Received from client:\n"+obj);

                if(obj instanceof Computer)
                {
                    totalCharge = ((Computer)obj).getPrice()*((Computer)obj).getQuantity();

                    HandleAClient thread = new HandleAClient(connection, clientNo, totalCharge);

                    threadExecutor.execute(thread);

                    output.writeObject(totalCharge);
                    output.flush();
                }





                clientNo++;
            }

        }
        catch(ClassNotFoundException cnfe)
        {
            cnfe.printStackTrace();
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }

    }//end of main
}

class HandleAClient implements Runnable
{
    //**SHOULD i do object...
    //Scanner input;
    //Formatter output;
    Object obj;

    ObjectOutputStream output;
    ObjectInputStream input;

    Socket connection;
    ServerSocket serverSocket;

    int clientNo;

    //variables for calculation
    //variables for calculation
    double price;
    double totalCharge;


    public HandleAClient(Socket connection, int clientNo, double totalCharge)
    {
        this.connection = connection;
        this.clientNo = clientNo;
        this.totalCharge = totalCharge;
    }

    public void run()
    {
        //ArrayList<Computer> cList = new ArrayList<Computer>();

        try
        {


            input = new ObjectInputStream(connection.getInputStream());
            output = new ObjectOutputStream(connection.getOutputStream());


            /*while(input.hasNext())
            {
                //variable = input.next....
                //print out calculation
                price = input.nextDouble();
                System.out.println("Price received from client:\t"+clientNo+"is"+price);

                //DO CALCULATION, STORE IT

                for(Computer c: cList)//**TRYING a for loop
                {
                    totalCharge = ((Computer)c).getPrice() * ((Computer)c).getQuantity();

                    output.format("%.2f\n", totalCharge);

                    //output.flush();
                }
            //}*/

            System.out.println("TotalCharge"+totalCharge);
            System.out.println("Thread"+"\t"+clientNo+"\t"+"ended");
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
    }
}

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

public class ComputerClient 
{
    public static void main(String args[])
    {
        Socket connection;

        ObjectOutputStream output;
        ObjectInputStream input;

        Object obj;

        Computer c = new Computer("Asus",1189.99,4);

        try
        {
            connection = new Socket("localhost",8000);

            output = new ObjectOutputStream(connection.getOutputStream());
            input = new ObjectInputStream(connection.getInputStream());

            output.writeObject(c);
            output.flush();

            //read back:

            obj=(Object)input.readObject();

            System.out.println(obj.toString());
        }
        catch(ClassNotFoundException cnfe)
        {
            cnfe.printStackTrace();
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
    }
}
    import java.io.Serializable;

public class Computer implements Serializable 
{
    private String brand;
    private double price;
    private int quantity;

    public Computer()
    {
        setBrand("");
        setPrice(0.0);
        setQuantity(0);
    }

    public Computer(String b, double p, int q)
    {
        setBrand(b);
        setPrice(p);
        setQuantity(q);
    }

    public String getBrand()
    {
        return brand;
    }

    public double getPrice()
    {
        return price;
    }

    public int getQuantity()
    {
        return quantity;
    }

    public void setBrand(String b)
    {
        brand = b;
    }

    public void setPrice(double p)
    {
        price = p;
    }

    public void setQuantity(int q)
    {
        quantity = q;
    }

    public String toString()
    {
        return("Brand: "+brand+"\t"+"Price: "+price+"\t"+"Quantity: "+quantity);
    }
}

1 个答案:

答案 0 :(得分:0)

&#39;连接重置&#39;通常意味着您已写入已被对等方关闭的连接。换句话说,应用程序协议错误。你已经写了一些同行没有读过的东西。下一个I / O操作或后续的I / O操作将取决于缓冲,将重置&#39;。

在这种情况下,服务器正在编写totalCharge并初始化两个ObjectOutputStreams,它们都会向流写入标头。客户端只创建一个ObjectInputStream,,它读取一个头,并读取一个对象,然后关闭连接。因此,其他标头无处写入并导致异常。

你不能这样做。你不能在同一个套接字上使用多个ObjectOutputStreams,至少在没有特别注意的情况下,这里并不明显。你也不应该在接受循环中做任何I / O.将所有客户端处理移动到HandleAClient类,这毕竟是它的所在,除了接受连接和启动线程之外,不要在接受循环中做任何事情。

您的服务器和客户端也都没有关闭连接。服务器只是泄漏套接字,客户端正在退出。在客户端案例中,操作系统正在为您关闭它,但这种做法很糟糕。关闭你的连接。在这种情况下,您应该关闭ObjectOutputStream.