Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7672605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:19:28+00:00 2026-05-31T16:19:28+00:00

i have this client/server application, and the client application sometimes completely freezes when i

  • 0

i have this client/server application, and the client application sometimes completely freezes when i try send object via sockets

public Client client = new Client();
petriNetList = client.query(client.actionLoadPetriNetList, MainWindow.loginUsername);

here is the code of the client

 public class Client {
    public static Socket kkSocket = null;
    public static PrintWriter out = null;
    public static BufferedReader in = null;
    public static BufferedReader stdIn = null;
    public static OutputStream outputStream = null ;
    public static ObjectOutputStream  objectOutputStream = null ; 
    public static InputStream inputStream = null ; 
    public static ObjectInputStream  objectInputStream = null ;  


    public String actionSavePetriNet = "SAVE PETRI NET START";



    /*
     * Save petri net to server
     * 
     * @param action identifies query
     * @param petriName name of the petri net
     * @param username username
     * @param xml content of the petri net
     * 
     * @return int response form server
     */
public int query (String action,String petriName,String username,String xml) throws IOException {



    int size ;
    int result = 0;
    connect();
    if (action.equals(actionSavePetriNet)) {

        out.println(action); // save petri net
        out.println(petriName); //petri net name
        out.println(username); //username
        System.out.println("(Client:)" + xml);
        objectOutputStream.writeObject(xml); //send object over the network
        System.out.println("Dostali sme sa sem ?");
        result = Integer.parseInt(in.readLine()); //read response from server
    }

       disconnect();
        return result;
}    


    /*
     * connect to server
     * TODO: ADD hostname and port as parameter
     */
    public static void connect() throws IOException {
    try {
            kkSocket = new Socket("osiris-PC", 4444);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: taranis.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: taranis.");
            System.exit(1);
        }

         stdIn = new BufferedReader(new InputStreamReader(System.in));

          outputStream = kkSocket.getOutputStream();  
          objectOutputStream = new ObjectOutputStream(outputStream);  

          inputStream = kkSocket.getInputStream();
          objectInputStream = new ObjectInputStream(inputStream);

    }

    /*
     * Disconnect from server
     * close all input/output streams
     * 
     */
    public static void disconnect() throws IOException {
        out.close();
        in.close();
        stdIn.close();
        objectOutputStream.close();
        outputStream.close();
        objectInputStream.close();
        inputStream.close();
        kkSocket.close();

    }
}

and this is the code of server

public class PetriServer {
    public static Protocol kkp = new Protocol();

    public static   InputStream inputStream = null ;
    public static   ObjectInputStream objectInputStream = null ; 
    public static   OutputStream outputStream = null ;
    public static   ObjectOutputStream objectOutputStream = null ;



    public static void main(String[] args) throws IOException, ClassNotFoundException {
        kkp.loadUsers();

     while(true) {
         ServerSocket serverSocket = null;

            inputStream = null ;
            objectInputStream = null ; 
            outputStream = null ;
            objectOutputStream = null ;

        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                clientSocket.getInputStream()));

        inputStream = clientSocket.getInputStream();  
       objectInputStream = new ObjectInputStream(inputStream);  

        outputStream = clientSocket.getOutputStream();
       objectOutputStream = new ObjectOutputStream(outputStream);
       inputLine = in.readLine();

        String inputLine;
 if (inputLine.equals("SAVE PETRI NET START")) {

            String petriName;
            String username;
            String xml ;
            String input;
            int result;
            int size ;

            petriName = in.readLine(); //read petri name

            System.out.println("(Server): prijali sme poziadavok na ulozenie " + petriName );


            username = in.readLine(); //read username
            System.out.println("(Server): poziadavok ide od usera: " + username);

          while(true) {

//this is the line where is occasionally freezes 
                xml = (String)objectInputStream.readObject(); //read object over the network
           if (!xml.isEmpty()) break; 
          }


          System.out.println("(Server):" + xml);

            result = kkp.savePetrinet(username, petriName,xml); //save it to the file
            out.println(result);

        }

        out.close();
        in.close();
        objectInputStream.close();
        inputStream.close();
        objectOutputStream.close();
        outputStream.close();
        clientSocket.close();
        serverSocket.close();


    }
    }
}

Does anybody know what can be problem ?
cheers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:19:29+00:00Added an answer on May 31, 2026 at 4:19 pm

    You have the Socket InputStream on the server attached to two different inputs, ObjectInputStream and BufferedReader. InputStreams are not intended to be used this way, and doing so can cause a lot of problems. BufferedReaders, by their nature, are going to be pulling more data off the InputStream than you are actually reading. If they happen to buffer data that makes up an object, then your subsequent attempt to read the object off the ObjectInputStream will block, because the data has already been removed.

    You need to pick one method or the other to read data. If you need to be able to read both Strings and Objects off the Socket, then you will have to go to a byte-oriented mode of operation where you read the bytes off into a byte array, then process the byte array yourself to insure that you don’t loose any data.

    Edit based on @Dunes comment below

    Seems your best bet is to stick entirely with ObjectInputStream and make use of its other methods that allow you to read arbitrary primitive types from the stream. And, if you don’t mind the deprecated call, it even has a readLine method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have an application that operations like this.. Client <----> Server <----> Monitor
I have an client server application on Android.And I have to send data from
I have client server application using distributed object. some time my code is working
we have this scenario: A server which contains needed data and client component which
We have a desktop client application developed in Swing. This application interacts with backend
In general chat application, client's browser always poll to server to check for new
We have application that involves C++ socket server, Flash client that communicates with C++
I have a client/server application built using DataAbstract and RemObjects where the client is
My requirement is I have server J2EE web application and client J2EE web application.
We have an application server developed with Delphi 2010 and Indy 10. This server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.