【问题标题】:Communicate with java server over tcp from c#从 c# 通过 tcp 与 java 服务器通信
【发布时间】:2015-11-25 11:29:58
【问题描述】:

我正在玩一些 TCP 服务器和客户端,我试图通过 c# 应用程序与 Java 服务器通信,但我无法让它工作。

我正在为我的 Java 服务器使用多线程服务器(线程连接)。 我使用 DataInputStream 和 DataOutputStream 读取输入流并写入输出流。这是我用来接收传入包的代码

DataInputStream ois = null;
DataOutputStream oos = null;


ois  = new DataInputStream(clientSocket.getInputStream());
oos = new DataOutputStream(clientSocket.getOutputStream());


        while(clientSocket.isConnected())
        {
            while(ois.available() > 0)
            {
                byte id = ois.readByte();

                Package p = MultiThreadedServer.getPackageHandler().getPackage(id);

                if(p != null)
                {
                    p.handle(ois, oos);
                    System.out.println("Request processed with id: " + id);
                }

              }
          }

当我通过 java 连接到这个服务器时,我曾经这样发送数据:

DataOutputStream oos = new DataOutputStream(socket.getOutputStream());
oos.writeByte(5);
oos.writeByte(3); // user_ID

oos.flush();

然后我通过搜索与发送的传入字节具有相同 id 的包来读取服务器中的输入 oos.writeByte(5); 这是我的包类

    public abstract class Package
{
    private int id = 0;

    public Package(int id){ this.id = id; }

    public abstract void handle(DataInputStream ois, DataOutputStream oos) throws Exception;

    public int getID()
    {
        return id;
    }
}

我如何读取传入数据的示例:

@Override
                    public void handle(DataInputStream ois, DataOutputStream oos) throws Exception
                    {

                        int user_id = ois.readByte();
                        System.out.println("Handle package 1 " + user_id);

                        ResultSet rs = cm.execute("SELECT TOP 1 [id] ,[username] FROM [Database].[dbo].[users] WHERE [id] = '"+ user_id +"'");

                        while (rs.next())
                        {
                            oos.writeByte((getID() + (byte)999));
                            oos.writeUTF(rs.getString(2));
                            oos.flush();
                        }

                    }

这仅在 Java 中运行良好,但如何使用 c# 发送这样的数据? 我需要一个可以做到这一点的函数:

DataOutputStream oos = new DataOutputStream(socket.getOutputStream());
    oos.writeByte(5);
    oos.writeByte(3); // user_ID

    oos.flush();

或者其他东西,但是在 c# 中,但是 c# 中的 NetworkStream 类不支持这个,所以我该怎么做呢?

【问题讨论】:

    标签: java c# multithreading sockets tcp


    【解决方案1】:

    我看不出 C# 中的 NetworkStream 不支持什么。您在构造函数中传递您的套接字并使用WriteByte() 发送数据。也可以使用Flush()

    所以它看起来像这样:

    var oos = new NetworkStream(socket);
    oos.WriteByte(5);
    oos.WriteByte(3); // user_ID
    oos.Flush();
    

    要编写浮点数或双精度数,您必须自己转换它们:

    double a = 1.1;
    long a_as_long = BitConverter.DoubleToInt64Bits(a);
    byte[] a_as_bytes = BitConverter.GetBytes(a_as_long);
    oos.Write(a_as_bytes, 0, a_as_bytes.Length)
    

    【讨论】:

    • 好吧,可能我表述有误。 WriteByte 功能实际上没问题。但我还需要 writeDouble 、 writeFloat 和 writeUTF 之类的函数。而那些在 Networkstream 类中不存在。我的例子不是最好的;)或者每次都可以使用 writeByte 并在服务器中将其转换为 double ?我没有这方面的经验
    • 否,但可以读取匹配的字节数。然后您可以将它们转换为 int 或 long,然后使用它(stackoverflow.com/questions/6816691/… 或它的对等等效项)将其转换为 double。
    • 通过向流中写入双精度来扩展答案
    猜你喜欢
    • 2021-12-29
    • 2011-10-25
    • 2011-09-23
    • 2019-04-15
    • 2014-10-24
    • 2021-07-29
    • 2012-06-29
    • 2013-07-01
    • 1970-01-01
    相关资源
    最近更新 更多