0

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?

2
Contributors
2
Replies
6
Views
5 Years
Discussion Span
Last Post by Jnk
2

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

Votes + Comments
Fast and clear answer
0

Thanks!
Adding the endline did the trick.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.