Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problems receiving/interpreting JSON data on TCP Client

  1. #1
    Junior Member
    Join Date
    Jul 2023
    Thanked 0 Times in 0 Posts

    Default Problems receiving/interpreting JSON data on TCP Client

    I use following code to send and receive JSON data to and from a PHP Swoole Server.

    I see the response from the server coming in (on Wireshark) but not interpreted by the Java TCP Client.

    Does anyone know how to read incoming JSON data properly?

    Here is the code:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;

    public class TcpClient {

    public static void main(String[] args) {
    String serverAddress = ""; // Replace with the server IP address
    int serverPort = 9000; // Replace with the server port

    try {
    // Create a socket and connect to the server
    Socket socket = new Socket(serverAddress, serverPort);

    // Get the input and output streams
    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);

    // Send a message to the server
    String rootDir = ".";
    String JSON_DATA = "{\"messagetype\":\"DirectoryInfoRequest\",\"curre ntdir\":\"" + rootDir + "\"}";

    // String message = JSON_DATA;

    // Receive the response from the server
    String line;
    StringBuilder responseBuilder = new StringBuilder();

    >>>>> while ((line = reader.readLine()) != null) { <<<<< hanging!
    System.out.println("Response from server: " + responseBuilder.toString());

    // Close the streams and socket
    } catch (IOException e) {

    The application is hanging on the readLine() command.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,710 Times in 2,660 Posts

    Default Re: Problems receiving/interpreting JSON data on TCP Client

    For testing, try reading the data at the char level:
                int ch;
                while((ch = in.read()) != -1) {

    Please edit your post and wrap your code with code tags:


    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. DatagramChannel - UDP receiving OK, but unable to send data :(
    By cr0ck3t in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: April 29th, 2014, 04:18 PM
  2. [Asking] Sending and Receiving Data via Serial
    By ardisamudra in forum Java Networking
    Replies: 5
    Last Post: January 23rd, 2013, 06:24 AM
  3. Reg - inserting json data to postgres db.
    By ramya in forum JDBC & Databases
    Replies: 2
    Last Post: August 27th, 2012, 01:34 PM
  4. Reg - inserting json data to postgres db using java
    By ramya in forum JDBC & Databases
    Replies: 2
    Last Post: August 27th, 2012, 12:50 PM
  5. servlet not receiving any data
    By vishal21 in forum Java Servlet
    Replies: 0
    Last Post: March 14th, 2011, 01:01 PM