2

首先,我绝对不是网络程序员。我尝试做的是在 Java 服务器和 C# 客户端之间进行非常简单的 TCP/IP 通信。

Java服务器:

 public void run(){   
try {
       // Open server socket
       _server = new ServerSocket(SERVER_PORT);
       _client = _server.accept();
                System.out.println("ComInterface: client connected.");
                // Wait for a client data output stream
                while(true){

                    // Receive message from client
                    BufferedReader is =
                            new BufferedReader(new InputStreamReader(_client.getInputStream()));
                    msg = is.readLine();

                    // Process message
                    if(msg!=null){
                        System.out.println("ComInterface: Message Received : <" + msg + ">.");
                        processMessage(msg); // Independant method
                    }
                    else{
                        System.out.println("ComInterface: client closed connection.");
                        _client.close();
                        _client = _server.accept();
                        System.out.println("ComInterface: client connected.");
                    }

                }

            } catch (IOException e) {
                e.printStackTrace();
            }
}

public void sendMessage(String msg){
        try {

            // Out stream
            DataOutputStream os = new DataOutputStream(_client.getOutputStream());

            os.writeBytes((String)(msg+"\n"+(char)13));
            os.flush();
            System.out.println("ComInterface: Message <" + msg + "> sent");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }

这是 C# 客户端:

public class ComInterface : MonoBehaviour
    {
        public const String SERVER_IP = "127.0.0.1"; // Localhost
        public const int PORT = 1100; // Default port 
        public const int READ_BUFFER_SIZE = 5000; // 4.8828125 kilobytes

        private TcpClient _client;
        private ASCIIEncoding _asen;
        private byte[] _readBuffer;
        private String _msg;

        public Boolean connected { get; internal set; } // setter is for internal use only

        /**
         * Initialize internal variables (buffer, socket...)
         */
        public ComInterface()
        {
            connected = false;
            _client = new TcpClient();
            _asen = new ASCIIEncoding();
            _readBuffer = new Byte[READ_BUFFER_SIZE];
            _msg = String.Empty;
        }

        /**
         * Connect to server at SERVER_IP:PORT
         * Return true if connection was a success, or false if failure.
         */
        public Boolean Connect()
        {
            try
            {

                _client.Connect(SERVER_IP, PORT);
                connected = true;
                Array.Clear(_readBuffer, 0, _readBuffer.Length);
                Debug.Log("TCPClient: <Connect> Connected to the server");
                // Start an asynchronous read invoking ReceiveComMessage
                _client.GetStream().BeginRead(_readBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(ReceiveComMessage), _client.GetStream());
            }
            catch (Exception ex)
            {
                Debug.Log("TCPClient: <Connect> Cannot connect to the server - " + ex.Message);
                connected = false;
            }
            // Return connection state
            return connected;
        }

 /**
         * Received a message from Communicator
         */
        private void ReceiveComMessage(IAsyncResult ar)
        {
            int BytesRead;
            String msg;
            try
            {
                BytesRead = _client.GetStream().EndRead(ar);
                if (BytesRead < 1)
                {
                    // if no bytes were read server has close.  
                    Debug.Log("TCPClient: <ReceiveComMessage> The server has closed (BytesRead<1)");
                    this.Disconnect();
                    return;
                }
                // Convert the byte array the message was saved into,
                msg = Encoding.ASCII.GetString(_readBuffer);
                Debug.Log("C# Message: \"" + msg + "\""); // Output example in log below
                BytesRead = 0;
                Array.Clear(_readBuffer, 0, _readBuffer.Length);

                // Start a new asynchronous read into readBuffer.
                _client.GetStream().BeginRead(_readBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(ReceiveComMessage), _readBuffer);

            }
            catch (Exception ex)
            {
                Debug.Log("TCPClient: <ReceiveComMessage> The server has closed (Exception):" + ex.Message + " see " + ex.StackTrace);
                this.Disconnect();
            }

主要问题是所有消息都到达不完整。这是日志跟踪:

C#: Message "{
C#: Message ""sender":"Bob"",
C#: Message ""recipient":",
etc...

而不是例如

C#: Message "{"sender":"Bob","recipient":[1,2,3]}"

我有点困惑,我需要一些帮助来解决这个问题。非常感谢你!

4

3 回答 3

3

TCP是面向流的连接,而不是面向消息的连接。它没有消息的概念。当你写出你的序列化字符串时,它只会看到一个毫无意义的字节序列。TCP 可以自由地将该流分解为多个片段,并且它们将在客户端以这些片段大小的块的形式接收。您可以在另一端重建整个消息。

在您的场景中,通常会发送消息长度前缀。这样,客户端首先读取长度前缀,然后它就可以知道传入消息应该有多大。

我会认真考虑使用 Google 的协议缓冲区之类的东西作为声明消息然后使用大小前缀选项流式传输它们的好方法。好处是您定义了一组消息,然后使用可用的工具从消息定义中自动生成 C++、Java、C# 等代码。这将有助于拥有在不同语言之间工作的一致消息集。

于 2012-06-28T03:19:19.113 回答
2

一条消息(我的意思是任何数据)在通过套接字发送时,被分成几个数据包。打印每个收到的数据包时,您看不到整个消息。

您应该定义消息字符串的结尾(类似于“.#.”)。在您收到此序列之前,您会一直连接收到的消息。

这就是会话协议(即在 TCP 之上运行的协议)所做的事情。

希望这可以帮助。

问候, 卡里尔

于 2012-06-28T02:52:59.997 回答
0

看看这个例子……

Java TCP 服务器...

import java.net.*;
import java.io.*;

public class TcpServer
{
    public static void main(String h[])
    {
        try
        {
            ServerSocket serverSocket = new ServerSocket(1100);
            Socket socket = serverSocket.accept();
            System.out.println("Client Accepted");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            System.out.println("Received: " + bufferedReader.readLine());
            PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
            printWriter.println("Hello Theo. Welcome to socket programming.");
        } catch (Exception e)
        {
            System.out.println(e);
        }
    }
}

C# TCP 客户端...

using System;
using System.IO;
using System.Net.Sockets;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            var client = new TcpClient("localhost", 1100);
            var stream = client.GetStream();
            var streamWriter = new StreamWriter(stream);
            streamWriter.WriteLine("My name is Theo");
            streamWriter.Flush();
            var streamReader = new StreamReader(stream);
            Console.WriteLine("Received: " + streamReader.ReadLine());
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }

        Console.WriteLine("Press a key to continue.");
        Console.ReadKey();
    }
}
于 2012-06-28T02:56:48.383 回答