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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:30:57+00:00 2026-06-03T17:30:57+00:00

I am writing a server part of a large Java application for communicating with

  • 0

I am writing a server part of a large Java application for communicating with client over TCP/IP using Java sockets. The client (written in PHP) connects to the server, sends a query in XML format, then the server sends back a response. The query-response can repeat multiple times in a single connection.

The server side is quite simple. It’s supposed to allow multiple client connections, so there’s a thread listening and spawning a session for each accepted connection. The session consists of an object containing two LinkedBlockingQueues for transmit and receive, two threads for transmitting and receiving messages using those queues and a processing thread.

The problem is that any messages are actually transmited only after the socket is closed. Response messages find their way into the message queue and PrintStream.println() method without a problem, but wireshark reports the transmission only when the client closes the connection on its side. Creating PrintStream with auto flush enabled or using flush() don’t work. Closing the socket on the server side does not work too, the server still functions and receives messages.

Also with current implementation of the client receiving the queries on the server side works fine, the same goes with echo -e "test" | socat - TCP4:192.168.37.1:1337 from a local Linux virtual machine, but when I telnet to the server and try to send something then server does not receive anything until I close the telnet client, the same problem as described above.

The relevant server code (the entire application is too big to paste everything and I’m working with a lot of other people’s code):

package Logic.XMLInterfaceForClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.HashSet;
import java.util.concurrent.LinkedBlockingQueue;

import Data.Config;
import Logic.Log;

public class ClientSession {

    /**
     * @author McMonster
     * 
     */
    public class MessageTransmitter extends Thread {

        private final Socket socket;
        private final ClientSession parent;

        private PrintStream out;

        /**
         * @param socket
         * @param parent
         */
        public MessageTransmitter(Socket socket, ClientSession parent) {
            this.socket = socket;
            this.parent = parent;
        }

        /*
         * (non-Javadoc)
         * 
         * @see java.lang.Runnable#run()
         */
        @Override
        public void run() {
            try {
                out = new PrintStream(socket.getOutputStream(), true);

                while (!socket.isClosed()) {
                    try {
                        String msg = parent.transmit.take();
                        // System.out.println(msg);
                        out.println(msg);
                        out.flush();
                    }
                    catch(InterruptedException e) {
                        // INFO: purposefully left empty to suppress spurious
                        // wakeups
                    }
                }

            }
            catch(IOException e) {
                parent.fail(e);
            }

        }

    }

    /**
     * @author McMonster
     * 
     */
    public class MessageReceiver extends Thread {

        private final Socket socket;
        private final ClientSession parent;

        private BufferedReader in;

        /**
         * @param socket
         * @param parent
         */
        public MessageReceiver(Socket socket, ClientSession parent) {
            this.socket = socket;
            this.parent = parent;
        }

        /*
         * (non-Javadoc)
         * 
         * @see java.lang.Runnable#run()
         */
        @Override
        public void run() {
            try {
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                while (!socket.isClosed()) {
                    String message = "";
                    String line;

                    while ((line = in.readLine()) != null) {
                        message = message + line + "\n";
                    }

                    if(message != "") {
                        parent.receive.offer(message.toString());
                    }
                }
            }
            catch(IOException e) {
                parent.fail(e);
            }

        }
    }

    public final LinkedBlockingQueue<String> transmit = new LinkedBlockingQueue<>();
    public final LinkedBlockingQueue<String> receive = new LinkedBlockingQueue<>();

    private final XMLQueryHandler xqh;
    private final Socket socket;

    private String user = null;
    private HashSet<String> privileges = null;

    /**
     * @param socket
     * @param config
     * @throws IOException
     * @throws IllegalArgumentException
     */
    public ClientSession(Socket socket, Config config)
            throws IOException,
            IllegalArgumentException {
        // to avoid client session without the client
        if(socket == null) throw new IllegalArgumentException("Socket can't be null.");

        this.socket = socket;

        // we do not need to keep track of the two following threads since I/O
        // operations are currently blocking, closing the sockets will cause
        // them to shut down
        new MessageReceiver(socket, this).start();
        new MessageTransmitter(socket, this).start();

        xqh = new XMLQueryHandler(config, this);
        xqh.start();
    }

    public void triggerTopologyRefresh() {
        xqh.setRefresh(true);
    }

    public void closeSession() {
        try {
            xqh.setFinished(true);
            socket.close();
        }
        catch(IOException e) {
            e.printStackTrace();
            Log.write(e.getMessage());
        }
    }

    /**
     * Used for reporting failures in any of the session processing threads.
     * Handles logging of what happened and shuts down all session threads.
     * 
     * @param t
     *            cause of the failure
     */
    synchronized void fail(Throwable t) {
        t.printStackTrace();
        Log.write(t.getMessage());
        closeSession();
    }

    synchronized boolean userLogin(String login, HashSet<String> privileges) {
        boolean success = false;

        if(!privileges.isEmpty()) {
            user = login;
            this.privileges = privileges;
            success = true;
        }

        return success;
    }

    public synchronized boolean isLoggedIn() {
        return user != null;
    }

    /**
     * @return the privileges
     */
    public HashSet<String> getPrivileges() {
        return privileges;
    }
}
  • 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-06-03T17:31:00+00:00Added an answer on June 3, 2026 at 5:31 pm

    I can understand why no messages are received at the server until the socket is closed – that seems to be as designed. in.readLine() will only return null when the end of the stream is reached which, with TCP socket stream, means when the socket is closed. If you want your readLine() loop to return before that, the code in the loop will have to detect the end of your message using whatever protocol you are using on top of TCP to define messages.

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

Sidebar

Related Questions

I'm writing a multithreaded C++ server and a Java client using TCP. It works
I'm writing fairly simple application which connects to server through SSH (using paramiko ),
I am writing a little application in android which would have a server part
I am writing a server application which has a large amount of source code.
I am writing a pyQt client-server application which restarts/shutdowns PCs remotely. The receivers are
my django application involves writing many data files to the server, part of my
I am writing a Java client that communicates with a remote server via HTTP/XML.
Im writing a server based application and i would like eclipse to be able
I am writing a server in PHP where I want to log all database
I am writing http server that can serve big files to client. While writing

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.