2

I am really new to this networking concept, but I have to stream a audio file using the UDP socket, and after searching for a week I have not really got anything useful. I have used CocoaAsyncSocket, and tried to send a string from the sender app to the receiver app and it's not receiving anything.

import Foundation
import CocoaAsyncSocket

class OutSocket : NSObject, GCDAsyncUdpSocketDelegate {

var hostIP : String = "255.255.255.255"
var port : UInt16 = 50008
var socket : GCDAsyncUdpSocket!

override init() {
    super.init()

    setUpConnection()
}

func setUpConnection() {
    socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: DispatchQueue.main)
    do {
        try socket.connect(toHost: hostIP, onPort: port)
    } catch let err{
        print("Error Connecting, ", err)
    }
}

func sendData(message : String) {
    guard let data = message.data(using: .utf8) else { return }
    let str = String(data: data, encoding: .utf8)
    print(str!)
    socket.send(data, withTimeout: 1000, tag: 0)
}

func udpSocket(_ sock: GCDAsyncUdpSocket, didConnectToAddress address: Data) {
    print("didConnectToAddress")
}

func udpSocket(_ sock: GCDAsyncUdpSocket, didNotConnect error: Error?) {
    print("didNotConnect \(String(describing: error))")
}

func udpSocket(_ sock: GCDAsyncUdpSocket, didSendDataWithTag tag: Int) {
    print("didSendDataWithTag")
}

func udpSocket(_ sock: GCDAsyncUdpSocket, didNotSendDataWithTag tag: Int, dueToError error: Error?) {
    print("didNotSendDataWithTag")
    print(error!)
}
}
import Foundation
import CocoaAsyncSocket
class InSocket: NSObject, GCDAsyncUdpSocketDelegate {

var hostIP : String = "255.255.255.255"
var port : UInt16 = 50008
var socket : GCDAsyncUdpSocket!

override init() {
    super.init()

    setUpConnection()
}

func setUpConnection() {
    socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: DispatchQueue.main)

    do {
        try socket.connect(toHost: hostIP, onPort: port)
        try socket.bind(toPort: port)
        try socket.enableBroadcast(true)
        try socket.enableReusePort(true)
        try socket.beginReceiving()
    } catch let err {
        print(err)
    }

    send(message: "Received")
}

func send(message:String){
    guard let data = message.data(using: .utf8) else { return }
    socket.send(data, withTimeout: 2, tag: 0)
}

And in the Listener View controller I have implemented the didReceive delegate method, but its not receiving anything. Can anyone please help me at least with the string part so that I can send a string from the sender app and can receive in the receiver app?

K. Mitra
  • 483
  • 7
  • 17

1 Answers1

0

For receive data you have to write this method beginReceiving() by which you will get the response from server if server sends you something. This method start the receiving process by which whenever server send any thing you will receive the data in didReceiveData delegate of GCDAsyncUdpSocket.

  let socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: DispatchQueue.main)
        do {
            try socket.connect(toHost: hostIP, onPort: port)
            try socket.beginReceiving()
        } catch let err{
            print("Error Connecting, ", err)
        }