I have a problem with sockets.
I am using this code in java as the client:

class Javaclient {

    private static DataInputStream input;
    private static PrintStream output;

    public static void main(String argv[]) throws Exception {
        int PortNumber = 4321;
        String line;
        BufferedReader in;
        Socket MyClient = null;
        try {
            MyClient = new Socket("localhost", PortNumber);
        } catch (IOException e) {
            System.out.println(e);
        }
        in = new BufferedReader(new InputStreamReader(MyClient.getInputStream()));
        line = in.readLine();
        System.out.println("data " + line);
       
    }
}

I am sending from the socket server(in c) data to this java client.
My problem is, that in Java my data is not printed until I stop the c server.
So when I do stop it it prints everything i have been sending.
This is my c code after getting a connection:

while (1) {
            printf("\n SEND : ");
            gets(send_data);
            send(connected, send_data, strlen(send_data), 0);

            fflush(stdout);
        }
    

    close(sock);
    return 0;

Can someone help me on how I can get the data immediately after sending, and without stopping the c server?

The readLine() method is for reading lines. A line ends with an line end character.
The readLine method will continue reading until it gets a lineend.

Some solutions:
Use the read method and read the data into an array as it comes
or have the sender add line end characters

commented: Fast and clear answer +1

Thanks!
Adding the endline did the trick.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.