简体   繁体   English

Java和C#应用程序之间的套接字通信

[英]Socket communicating between java and c# applications

I have two applications, one written in Java and the other in C#. 我有两个应用程序,一个用Java编写,另一个用C#编写。 I am trying to send a string from the Java app to C# app. 我正在尝试从Java应用程序向C#应用程序发送字符串。

My java code for sending the string is as follows: 我的用于发送字符串的Java代码如下:

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: 我用于接收数据的C#代码如下:

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. 我在C#应用程序中接收数据时遇到问题。 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: 当我在Java应用程序中发送字符串“ Data”并尝试使用Console.WriteLine("Received: {0}", data)打印C#应用程序接收到的Console.WriteLine("Received: {0}", data) ,输出为:

Received: D
Received: ata

If I use Console.WriteLine("Received: ", data) , the output is: 如果我使用Console.WriteLine("Received: ", data) ,则输出为:

Received: 
Received: 

I want my C# application to receive the full string that is sent by the Java application. 我希望我的C#应用​​程序接收Java应用程序发送的完整字符串。 I tried to increase the buffer byte array size to 1000 but it doesn't help. 我试图将缓冲区字节数组的大小增加到1000,但这没有帮助。 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" “我希望我的C#应用​​程序接收完整的字符串”

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? 也许您希望\\ n表示消息结束? 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. 如果终止符确实是换行符,则可能需要考虑将NetworkStream传递给StreamReader并在其上调用ReadLine Now, the StreamReader will keep reading from the stream until it hits a newline, then hand you the line. 现在, StreamReader将继续从流中读取数据,直到碰到换行符,然后将其交给您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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