Unable to read Beyond the end of the stream

Asked

Viewed 95 times

0

I was trying to create a socket connection between Java and C#, Java being the client and C# the server.

But when I use Binaryreader to read the data, it returns an error:

Unable to read Beyond the end of the stream.

Excerpt from the C# code that reads:

Console.WriteLine("Iniciando...");
IPEndPoint ipp = new IPEndPoint(IPAddress.Parse("192.168.173.1"), 5000);
tccp=new TcpListener(ipp);
tccp.Start();
Console.WriteLine("Conexão TCP iniciada, ip : 127.0.0.1, porta: 4567");

doidaco = tccp.AcceptSocket();
newton = new NetworkStream(doidaco);
rr = new BinaryReader(newton);

string mensagem="";
do{
     mensagem = rr.ReadString();
    Console.WriteLine("\nMensagem do cliente: "+mensagem);

}while(doidaco.Connected);

Complete code C#: http://pastebin.com/m2Vpznts

The Java code that is sending the message through the socket is as follows:

public static void main(String[] args) throws IOException {
    socket = new Socket("192.168.173.1", 5000);
    Scanner sca = new Scanner(socket.getInputStream());
    PrintWriter ee = new PrintWriter(socket.getOutputStream());
    ee.println("Hello!");
    System.out.println("Mensagem enviada.");
    ee.close();
    System.out.println("Conexão encerrada.");
    sca.close();
    socket.close();
}

Java code: http://pastebin.com/2ewZtxVk

  • I have an example of code that only uses Sockets. Does it fit you? Can I put this in the answer?

  • Well, I would like to send a message socket 'Hello' from the java client (Android), to a C server. If you have any relation would be a favor.

  • I think the answer below has already worked! : ) Still want the example only with Socket server side, without TcpListener?

1 answer

3


Your problem is that the format of the written message on socket (java code) is different from the format expected in the reader (code in C#). The call to ee.println("Hello!"); will write the bytes relative to the string "Hello!" (0x48 0x65 0x6C 0x6F 0x21) followed by the line feed (0x0A or 0x0D).

The call to BinaryReader.ReadString, however, expect the first byte of the message to indicate the size of the string (see MSDN documentation for this method). So when the BinaryReader starts reading the string, it sees the "size" of 0x48 and tries to read more 0x48 (72) bytes from the stream - and there is this amount of bytes, which is why you get the error you mentioned.

If you want to use the println to write the string on the java side, consider reading the bytes on the receiver until you find the end of the line (0x0A / 0x0D). An alternative is to use a StreamReader and the method ReadToEnd() to read everything the customer sent.

var clientSocket = tccp.AcceptSocket();
var clientSocketStream = new NetworkStream(clientSocket);
var streamReader = new StreamReader(clientSocketStream);

string mensagem = streamReader.ReadToEnd();
Console.WriteLine("Mensagem do cliente: " + mensagem);
  • I don’t understand these things much, I would have some way to solve?

  • I edited the answer with an option on how to solve.

  • It worked! I would like to know if there is any method for it to keep updating, that is to say, every message the customer sends it write in the chat.

  • Instead of using the ReadToEnd take a look at the other methods of the class StreamReader; perhaps the ReadLine do what you need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.