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 914705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:45:44+00:00 2026-05-15T17:45:44+00:00

i’ve a minimal server which wait until a client connect ,then he start a

  • 0

i’ve a minimal server which wait until a client connect ,then he start a thread which will send a reply back to the client, the problem is the reply.

This is the code of the server:

 int port = 1234;
 ServerSocket servSock = null;
 servSock = new ServerSocket(port);       
  while (true) {
   Socket link = servSock.accept();
   serverThread st = new serverThread(link);
   st.start();
  }

This is the code of the run() method of the thread,the one which send the answer back, sk is the socket “link” passed by parameter in the server code

 public void run() {
    String dato = "";
    InputStream i = null;
    try {
        i = sk.getInputStream();
    } catch (IOException ex) {
        Logger.getLogger(serverThread.class.getName()).log(Level.SEVERE, null, ex);
    }
        Scanner input = new Scanner(i);
     //i receive the data sent
        while (input.hasNext()) {
            dato += input.nextLine();
        }   
    // then i send a reply
    DataOutputStream outputStream=new DataOutputStream(sk.getOutputStream());
    outputStream.writeInt(1);
    outputStream.close();

Client side ( only the code which should receive the answer from the server) :

        Socket link;
        int valid = 0;
        String url="localhost";
        int port=1234;
        link = new Socket(InetAddress.getByName(url), port);

        //i've to send some data to the server
        PrintWriter output = new PrintWriter(link.getOutputStream(), true);
        String a = new String(Base64.encode(mex));
        output.println(createXml(tag, a));
        output.flush();


        //then i need to receive an answer from the server
        DataInputStream answerI = new DataInputStream(link.getInputStream());

        while(answerI.available()!=0)// but answerI.available() is always equal 0
           valid = answerI.readInt();

        answerI.close();
        output.close (); 
        link.close();

With this code the istruction valid = answerI.readInt(); is not reached.

Without the while cycle, the client freeze at line : valid = answerI.readInt();

Can anyone help me?

Thank you in advance

  • 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-15T17:45:45+00:00Added an answer on May 15, 2026 at 5:45 pm

    I’m guessing that the server is blocked in a call to input.hasNext(). This will return false when the socket is closed, and its InputStream returns -1 to signal the end of the stream. However, the socket is still open. The client can send another line; the Scanner is blocking to see what the client’s next move will be.

    There are ways to shutdown “half” of a socket, so that the server can tell that the client has closed its sending channel but can still receive a response.

    However, this approach is complicated. I suggest a change to the protocol so that the server can determine when it is allowed to respond.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from
I need a function that will clean a strings' special characters. I do NOT

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.