简体   繁体   中英

Socket communicating between java and c# applications

I have two applications, one written in Java and the other in C#. I am trying to send a string from the Java app to C# app.

My java code for sending the string is as follows:

String response;
try {
    DataOutputStream outToServer =
        new DataOutputStream(outGoingSocket.getOutputStream());
    BufferedReader inFromServer =
        new BufferedReader(new InputStreamReader(outGoingSocket.getInputStream()));

    outToServer.writeBytes(message + '\n');
    outToServer.flush();

    response = inFromServer.readLine();

    System.out.println("Received: " + response);        
} catch (Exception ex) {
    System.err.println("Exception in incoming socket: " + ex.getMessage());
}

My C# code for receiving the data is as follows:

Byte[] bytes = new Byte[1000];
String data = null;
try {
    Console.Write("Waiting for a connection... ");
    TcpClient client = incomingSocket.AcceptTcpClient();
    Console.WriteLine("Connected!");

    data = null;
    NetworkStream stream = client.GetStream();

    int i;
    while (true) {
        while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) {
            data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
            Console.WriteLine("Received:", data);
            processReceivedMessage(data);
            ackSocket(stream, "OK");
        }
    }
} catch (Exception ex) {
    Console.WriteLine("Exception: ", ex);
}

I have a problem with receiving the data in the C# application. When I send the string "Data" in the Java app, and try to print the data received by the C# application using Console.WriteLine("Received: {0}", data) , the output is:

Received: D
Received: ata

If I use Console.WriteLine("Received: ", data) , the output is:

Received: 
Received: 

I want my C# application to receive the full string that is sent by the Java application. I tried to increase the buffer byte array size to 1000 but it doesn't help. I don't have experience using sockets, can someone show me what I did wrong?

So, as you can see, the receiving end picks up a response in chunks that might be considerably smaller than the total message.

You shouldn't be seeking to change this behaviour... it's a fact of network programming. It's your job to glue them back together again.

"I want my c# application receive the full string"

So, how is your receiving app meant to know that it has received the full string? Did you send a length field to indicate how much data is coming? Perhaps you expect \\n to indicate the end of message? A zero byte?

If your terminator is indeed a newline, you might want to consider passing your NetworkStream to a StreamReader and calling ReadLine on it. Now, the StreamReader will keep reading from the stream until it hits a newline, then hand you the line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM