Giter VIP home page Giter VIP logo

Comments (6)

dannys42 avatar dannys42 commented on June 14, 2024

I think the "Socket.create" line may not be needed in your attach() call? It's certainly overriding your socket.listen().

from bluesocket.

kkharji avatar kkharji commented on June 14, 2024

🤔 indeed must copied here by mistake.

as a workaround I opt out to nc

    let pipe = Pipe()
    let echo = Process()
    echo.launchPath = "/usr/bin/env"
    echo.arguments = ["echo"] + args.components(separatedBy: " ")
    echo.standardOutput = pipe

    let task = Process()
    task.launchPath = "/usr/bin/env"
    task.arguments = ["nc", "-U", socketPath]
    task.standardInput = pipe
    echo.launch()
    task.launch()

    task.waitUntilExit()

    if task.terminationStatus != 0 {
      throw Error.processFailed(args)
    }

My attach function is now

func attach() async {
    do {
      let socket = try Socket.create(family: .unix, type: .stream, proto: .unix)
      try socket.listen(on: socketPath)
      log.info("Listening on \(socketPath)")

      while true {
        let conn = try socket.acceptClientConnection()
        let msg = try conn.readString()?.trimmingCharacters(in: .whitespacesAndNewlines)
        conn.close()
        await processMessage(msg)
        if stop || restart {
          log.info("No longer accepting connection to \(socketPath)")
          break
        }
      }
    } catch {
      log.error("Socket error: \(error)")
    }

    if restart {
      restart = false
      await attach()
    }
  }

from bluesocket.

Kirow avatar Kirow commented on June 14, 2024

I had same error but in different context.

do {
   let socket = try Socket.create(family: .inet, type: .datagram, proto: .udp)
   let destination = Socket.createAddress(for: "localhost", on: 50000)
   _ = try socket.write(from: Data("Message".utf8), to: destination!)
   print("Sent message to 50000")
} catch {
   print("Failed to send message to 50000 - \(error)")
}

It worked few days ago, but now its not and that makes me crazy... the problem in localhost...if I change it to 127.0.0.1 it start working again. But Im pretty sure it worked with localhost before. Any ideas why it stop working?

from bluesocket.

dannys42 avatar dannys42 commented on June 14, 2024

If you're on macOS, I think localhost is defined for both ipv4 and ipv6, which I think can sometimes cause confusion. Is it more reliable if you use 127.0.0.1? Also how is your listening side declared?

from bluesocket.

Kirow avatar Kirow commented on June 14, 2024

@dannys42 yes, im on macOS. no listener in this example. it just works w/o error if I change to 127.0.0.1. But I'm very curious about the reason why localhost stopped working (because it worked few days ago and I have not change source code). So it more likely the issue with my host machine.
/etc/hosts does have 127.0.0.1 localhost and ping localhost in terminal works...

from bluesocket.

Kirow avatar Kirow commented on June 14, 2024

/etc/hosts also has ::1 localhost. After I removed it - socket start working again. So yes, it were related to ipv4/ipv6. Not sure why it were recognised as AF_INET before and now as AF_INET6, but yes - it will be more reliable to use 127.0.0.1

from bluesocket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.