jansma jansma - 4 months ago 100
Swift Question

iOS - Use GCDAsyncSocket to send something to server have some error

I have two client ,one is iOS(Client A) ,another is Jave (Client B) , Client A use for sender and the Client B is receive the data from Client A and print it in console , now I send some String(String and JsonString) to client B , Client B can receieve the message form Client A ,but the console print incomplete

This is my step

1、I define a NSDictionary in iOS

let dict:NSDictionary = ["type":type,"x":point.x,"y":point.y]


2、convent it to json string and append some string in json String

var error:NSError?
let data = NSJSONSerialization.dataWithJSONObject(dict,options:NSJSONWritingOptions.PrettyPrinted, error: &error)
var str = NSString(data: data!, encoding: NSUTF8StringEncoding)
var newStr = return "\(toPort)#\(str!)"


then I print it in iOS console ,It works fine

3、 I convert the string to NSData

let data = newStr.dataUsingEncoding(NSUTF8StringEncoding)


4、send the NSData to server

self.socket!.writeData(data!, withTimeout: self.timeout, tag: 0)


5、only have
{
displayed in client B ,what happen?

then I try to use jave to achieve the client A , and it works fine in client B

Answer

I solved it

1、remove all \n in str

2、use

var newStr = return "\(toPort)#\(str!)\n"

instand of

var newStr = return "\(toPort)#\(str!)"