I have two service class here. One is Host service and another is Join service class. From the HostService I am publishing NetService. In the JoinService I am using NetServiceBrowser for getting published service. Then successfully connected to that hosted service.
HostService and JoinService both contains different GCDAsyncSocket objects. Both sockets are ininilized with the main queue as below.
socket = GCDAsyncSocket(delegate: self, delegateQueue: .main)
When connection accepted by host from join both sockets are successfully connected. So now I am able to sending data to each other.
Then I am sending data from any of one socket. Consider here sending from the host socket. I am using the following code for writing data. In the HostService class.
func sendValue(str: String) {
let size = UInt(MemoryLayout<UInt64>.size)
let data = Data(str.utf8)
hostSocket.write(data, withTimeout: -1.0, tag: 0)
hostSocket.readData(toLength: size, withTimeout: -1.0, tag: 0)
}
Then I am getting call in sock:didWriteDataWithTag:tag which is GCDAsyncDelegate’s method in the HostService.
func socket(_ sock: GCDAsyncSocket, didWriteDataWithTag tag: Int) {
let astr = "Host did write data on join port: \(sock.connectedPort)"
print(astr)
socketStatusCompletionBlock(astr,sock)
}
Now I am expecting that I should get call simultaneously in below method implemented in JoinService’s GCDAsyncSocketDelegate method.
func socket(_ sock: GCDAsyncSocket, didRead data: Data, withTag tag: Int) {
let str = String(decoding: data, as: UTF8.self)
let astr = "Join did read \(str) with host port: \(sock.connectedPort)"
print(astr)
socketStatusCompletionBlock(astr, sock)
inComingValueCompletionBlock(str)
}
But I am not getting call simultaneously.
Then when I am sending data from the JoinService same as HostService I am getting call first in “func socket(_ sock: GCDAsyncSocket, didRead data: Data, withTag tag: Int)” and got data which was sent from HostService before.
So in summary I am getting the data sent from the other socket in didRead method when I am writing data from the current socket.
In edition it seems like same problem from reading it. But i am not getting it. GCDAsyncSocket Client not reading until write