-2

Developing a C++ Server with Java platform. Stack with some little problem. Hope anyone can help here

C++ Server code fragment

// Sample send a socket
sned(ConnectedSocket, classroomlist, strlen(classromlist), 0);

In Java Client

// Catch the socket
InputStreamReader isr = new InputStreamReader(socket.getInputStream());

char[] buffer = new char[3];

And the problem come here

  1. If I use BufferedReader, Nothing read out.

  2. And I try to use

    while((x = inr.read(buffer)) != -1){}

, stack into the while loop while reading. ( Discover why no output for BufferedReader )

  1. And I try to while(true){ SYstem.out.println(inr.read(buffer)) } print out all the length of each round of reading. Problem found! Output Like this vvvvvvvvvvv

    05-31 11:35:09.934: D/@@@(3264): 3 05-31 11:35:09.934: D/@@@(3264): 3 05-31 11:35:09.934: D/@@@(3264): 3 05-31 11:35:09.934: D/@@@(3264): 2

InputStremaReader.read() suppose return -1 if the tcp package reach the end. But the function just stack beforn the end and waiting waiting waitinggggggg.

6
  • question is not clear Commented May 31, 2013 at 4:16
  • Want to get the response text in Java client :D The problem with 'But the function just stack beforn the end and waiting waiting waitinggggggg.' Commented May 31, 2013 at 4:19
  • If you are using a socket won't it only return -1 when the connection is closed and 0 if no data was read? Does the socket block on the read? Commented May 31, 2013 at 4:30
  • This is a continuation of this question: stackoverflow.com/q/16848880/1667977 You need to simply close the socket as you were already told. Commented May 31, 2013 at 5:30
  • But in Java to Java Socket programming, socket dont need to close to finish the package sent. You can use socket.getInputStream() and socket.getOutputStream() many time Commented May 31, 2013 at 5:41

1 Answer 1

1

If the sender never closes the socket, the receiver will never get the end-of-stream condition.

Conversely, if the receiver never gets the end-of-stream condition, the conclusion must be that the sender never closes the socket.

Sign up to request clarification or add additional context in comments.

1 Comment

but a socket connect can have many data package send??? I found that using datainputstream can solve the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.