Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

java - Server and client connect but the service is not completed and no errors

I have created a server, server thread, client, data structure and a object.

This is homework but I can not see (or maybe I am not understanding correctly) the problem I am having in the console window you will see the messages being printed to the console of which relates to where I am in the code.

The Object (Car) implements the Serializable and just has some standard methods (getters, toString() ...) so I will not put this in the question unless someone who can help requests it as it will just take up room the same goes for the data structure I am using which is an ArrayBlockingQueue class all are in the same folder.

Main(Server)

public class Main {
    final static int CAR_PORT = 2001;   

    public static void main(String[] args) {
        DataStructure data = new DataStructure();
        
        try {
            ServerSocket serverSocket = new ServerSocket(CAR_PORT); //Server socket to listen for clients requests
            
            while(true) {
                System.out.println("Running");  
                Socket socket = serverSocket.accept();
                System.out.println("Got Request");
                
                new ServerThread(socket,data).start();              
                System.out.println("Request Processed");
            }           
        }catch (IOException e) {e.printStackTrace();}
    }   
}

enter image description here

Server Thread

public class ServerThread extends Thread {
    Socket socket;
    DataStructure array;
    
    public ServerThread(Socket socket, DataStructure array) {
        this.socket = socket;
        this.array = array;
    }
    
    public void run() {
        try {
            
             Car car = new Car();
             System.out.println("I am here");
             ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
             ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                        
             car = (Car) in.readObject();   // Read object from client
             array.add(car);                // Add car to data structure    
             
             out.writeBoolean(true);        // Send boolean to confirm
             
             socket.close();
    
        }catch (Exception e) {}
    }
}

Client

public class ClientSalesPerson{
    final static int CAR_PORT = 2001;   

    public static void main(String[] args) {    
        System.out.println("Adding Car");
        write(new Car("A111", "Volkswagen", 100, 10000, true));
        System.out.println("I never get here!!");
    }
    
    static void write(Car car) {
        try {
            System.out.println("But i do get here");
            Socket socket = new Socket(InetAddress.getLocalHost(), CAR_PORT);
            
            ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
            ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
            
            out.writeObject(car);               // Write to server          
            boolean added = in.readBoolean();   // Read boolean from server
            
            // Check if added successfully 
            if(!added)
                System.out.println("Not Added");
            else
                System.out.println("Car Added" + car.getMake());
                    
            socket.close();                     // Close socket
            
        }catch (IOException e) {System.out.println(e);}
    }
}

Once I run the client the messages appear in both console but boolean read by the client is never processed meaning the car object is not added to the ArrayBlockingQueue.

I have done some research into ObjectInputStream/Out and I seem to be implementing it correctly no errors are shown it just pauses on the consoles below

enter image description here

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...