简体   繁体   English

通过 Socket Outputstream 从 Swift 客户端向 Java 服务器发送消息

[英]Send message through Socket Outputstream from Swift Client to Java Server

As the title says, I'm having some problems connecting my Swift Client to a Java Server by using only native libraries.正如标题所说,我在仅使用本机库将 Swift 客户端连接到 Java 服务器时遇到了一些问题。

Right now I have a Java(client) to Java(server) connection, which works flawlessly, and a Swift(client) to Java(server) connection.现在我有一个 Java(客户端)到 Java(服务器)的连接,它可以完美地工作,还有一个 Swift(客户端)到 Java(服务器)的连接。 My Swift client does connect to the Java server (it shows on the console), but the readFully of my server hangs - the only thing I see is the enormous message length on the console and that's pretty much it.我的 Swift 客户端确实连接到 Java 服务器(它显示在控制台上),但是我的服务器的 readFully 挂起 - 我唯一看到的是控制台上的巨大消息长度,仅此而已。

Swift Code (send): Swift 代码(发送):

func send(message: String) {
  let data = "msg:\(message)".data(using: .utf8)!

    data.withUnsafeBytes {
    guard let pointer = $0.baseAddress?.assumingMemoryBound(to: UInt8.self) else {
      print("Error joining chat")
      return
    }
    out!.write(pointer, maxLength: data.count)
  }
}

Java Server (read): Java 服务器(读取):

int lenght = in.readInt();
System.out.println("Message: " + lenght);
if(length>0){
 byte[] message = new byte[length];
 in.readFully(message), 0, message.length); //This is where the problem is
}

So, basically the Java server hangs on the readFully method.所以,基本上 Java 服务器挂在 readFully 方法上。 Any ideas why that is?任何想法为什么会这样? Whether I send a simple String or a Byte array, it always hangs, and for some reason the length is extremely big.无论我发送一个简单的字符串还是字节数组,它总是挂起,并且由于某种原因长度非常大。 Can someone help me understand what I'm doing wrong because I've around this for days and still didn't understand it (still pretty new to Swift).有人可以帮我理解我做错了什么,因为我已经在这个问题上做了好几天但仍然不理解它(对 Swift 来说仍然很新)。

Thank you very much!非常感谢!

In your Java code, you expect the first 4 bytes to be the length of the message, but in the Swift code, you didn't send the length of the message first.在您的 Java 代码中,您希望前 4 个字节是消息的长度,但在 Swift 代码中,您没有先发送消息的长度。 You sent the message itself directly.您直接发送了消息本身。 So now the Java code is left confused as to why the message is shorter than it expected, and waiting for more data to read.因此,现在 Java 代码对于为什么消息比预期短,并等待读取更多数据感到困惑。

You can try sending the number of bytes in data first:您可以尝试先发送data中的字节数:

let byteCountAsByteArray = withUnsafeBytes(of: Int32(data.count).bigEndian, Array.init)
out!.write(byteCountAsByteArray, maxLength: 4)
out!.write(pointer, maxLength: data.count)

I suspect that you are using a DataInputStream in your Java code, hence .bigEndian .我怀疑您在 Java 代码中使用了DataInputStream ,因此.bigEndian But if you are reading this in a little endian way, use .littleEndian instead.但是,如果您以小端方式阅读本文,请改用.littleEndian

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

相关问题 将Meteor中的消息服务器套接字发送到Java中的客户端 - Send message server socket in Meteor to client in Java Java套接字将数据从客户端发送到服务器 - Java socket send data from client to server 如何从客户端读取并通过Java中的服务器套接字发送到另一个客户端 - How to Read from a client and send to another client through a server socket in Java (Java 服务器套接字)每分钟向客户端发送消息 - (Java server socket) send message every minute to a client 如何使用 Socket.IO-client Java 从 Android 向服务器发送 String 消息? - How send String message to server from Android with Socket.IO-client Java? 接收对象时发生java.io.EOFException。 通过套接字将文件从客户端发送到服务器 - java.io.EOFException while reciving a object. send file from client to server through socket 一个简单的客户端服务器套接字程序的PrintWriter和OutputStream - PrintWriter and OutputStream for a simple client server socket program 无法通过文本字段将消息从服​​务器发送到android中的客户端 - Unable to send message from server to the client in android through text field 使用套接字将服务器JAVA的图像发送到android客户端 - send image from server JAVA to android client using socket 无法将数据从Java Socket Server发送到NodeJS客户端 - Can't send data from Java Socket Server to NodeJS client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM