In Client :
I will call send() function to pass int number and string in swift client.
The int number has already transformed to [UInt8] type.
func send(number: [UInt8], string: String) {
var buf = Array(repeating: UInt8(0), count: 1024)
let data = string.data(using: .utf8)!
data.copyBytes(to: &buf, count: data.count)
outputStream?.write(number, maxLength: number.count) // send int number
outputStream?.write(buf, maxLength: buf.count) // send JSON string
}
In Server:
I want to send data to c# server.
dataReceive() function will receive the data sent by client.
private void dataReceive() {
if(reader != null) { // reader is a BEBinaryReader
try {
int number = reader.ReadInt32();
Console.WriteLine(number);
...
String str = reader.ReadString();
Console.WriteLine(str);
} catch(Exception e) {
Console.WriteLine(e.Getype());
}
}
}
The server can successfully receive int number. However, it will occur exception when receiving string.
The exception is OutOfMemory exception, but the string data I send is just a simple Json string like {"id": 0, "name": "Andy"}
Hope anyone who understands how to send data between swift and c# can help me. Thanks!