Skip to content

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

@jaideep824

Description

@jaideep824

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions