Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localhost Server - client : readData(), readString() not working on iOS. #179

Open
jaideep824 opened this issue Sep 23, 2019 · 0 comments
Open

Comments

@jaideep824
Copy link

Hi all,

Currently i am working on two applications which will installed on same device and communicate over socket. So for that we need a local socket server and local socket client.

So i have created server which has signature:
Optional(Signature: family: inet6, type: stream, protocol: tcp, address: Optional(Socket.Socket.Address.ipv6(__C.sockaddr_in6(sin6_len: 28, sin6_family: 30, sin6_port: 34084, sin6_flowinfo: 0, sin6_addr: __C.in6_addr(__u6_addr: __C.in6_addr.__Unnamed_union___u6_addr()), sin6_scope_id: 0))), hostname: Optional("::"), port: 9349, path: nil, bound: true, secure: false)

it start listening on port 9349. After some time it received client connection and it accept the connection.
client signature is:
(Signature: family: inet6, type: stream, protocol: tcp, address: Optional(Socket.Socket.Address.ipv6(__C.sockaddr_in6(sin6_len: 28, sin6_family: 30, sin6_port: 61890, sin6_flowinfo: 0, sin6_addr: __C.in6_addr(__u6_addr: __C.in6_addr.__Unnamed_union___u6_addr()), sin6_scope_id: 0))), hostname: Optional("::1"), port: 49905, path: nil, bound: false, secure: false)

Now when i start reading the data/string i received nothing. we have implemented the same architecture on android which is working fine.

My reading code is:

func addNewConnection(socket: Socket) {

    self.connectedSockets[socket.socketfd] = socket
    try? socket.setReadTimeout(value: 10000)
    try? socket.setWriteTimeout(value: 10000)
    let queue = DispatchQueue.global(qos: .default)
    
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {
        print("sending message ")
        do{
            let reply = "Server from response"
            let written = try socket.write(from: reply)
            print("sent to client is: \(written)")
        }catch let error{
            print("error in replying for connection \(error) and \(error.localizedDescription)")
        }
    })
    
    func readNewData(){
        print("reading once again")
        var readData = Data()
        do{
            //let stringRead = try socket.readString()
            let bytesRead = try listenSocket?.read(into: &readData) ?? 0//socket.read(into: &readData)
            if bytesRead  > 0 {
                guard let response = String(data: readData, encoding: .utf8) else {
                    print("Error decoding response...")
                    return
                    //break
                }
                print("data string read in socket are \(response)")
            }
            
        }catch let error{
            print("error reading data in socket \(error) and \(error.localizedDescription)")
        }
    }
    
    timer.eventHandler = {
        readNewData()
    }
    timer.resume()
}

am i doing anything wrong here or my implementation itself is wrong? please help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant