digitalmars.D.learn - How do you connect Python with D via socket, I'm still getting
- Enjoys Math (116/116) May 03 2018 Error
- Andy Smith (7/10) May 04 2018 Haven't run it, but two things to try...
- Enjoys Math (2/13) May 10 2018 That got it to work, nvm.
Error ----- builtins.ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it Python Side ----------- from PyQt5.QtWidgets import QApplication, QMainWindow import sys import socket import settings if __name__ == "__main__": app = QApplication([]) window = QMainWindow() window.show() host = socket.gethostname() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) available port print(host) sock.connect((host, settings.debugPort)) try: message = 'This is the message. It will be repeated.' print(sys.stderr, 'sending "%s"' % message) sock.sendall(message.encode()) amount_received = 0 amount_expected = len(message) while amount_received < amount_expected: data = sock.recv(16) amount_received += len(data) print(sys.stderr, 'received "%s"' % data) finally: print(sys.stderr, 'closing socket') sock.close() sys.exit(app.exec_()) And the D Side -------------- module server; import core.thread; import std.socket; import std.experimental.logger; class Server : Thread { private: Socket listener; int backlog; string address; ushort port; SocketSet sockSet; Socket[] clients; bool running; public: this(ushort port, string address="") { super(& run); if (address == "") address = "127.0.0.1"; this.port = port; this.address = address; backlog = int.max; listener = null; running = false; } bool setupSocket() { try { listener = new Socket(AddressFamily.INET, SocketType.STREAM); auto address_list = getAddress(address, port); if (address_list.length) { listener.bind(address_list[0]); sockSet = new SocketSet(); } } catch (Exception e) { log(LogLevel.critical, e.toString()); return false; } return true; } void start() { if (listener is null) { if (! setupSocket()) return; } running = true; if (! isRunning) super.start(); } void stop() { running = false; } private: void run() { char[1024] buffer; while(running) { sockSet.reset(); sockSet.add(listener); foreach (client; clients) sockSet.add(client); if (Socket.select(sockSet, null, null)) { foreach (client; clients) { if (sockSet.isSet(client)) { auto got = client.receive(buffer); client.send(buffer[0 .. got]); } } if (sockSet.isSet(listener)) { auto newSocket = listener.accept(); newSocket.send("Hello!\n"); clients ~= newSocket; } } } } }
May 03 2018
On Thursday, 3 May 2018 at 23:58:24 UTC, Enjoys Math wrote:Error ----- [...]Haven't run it, but two things to try... On D side.... try adding listen after bind. On python side. Don't think you need to call bind the client socket ( this may cause problems). Cheers, A.
May 04 2018
On Friday, 4 May 2018 at 13:52:29 UTC, Andy Smith wrote:On Thursday, 3 May 2018 at 23:58:24 UTC, Enjoys Math wrote:That got it to work, nvm.Error ----- [...]Haven't run it, but two things to try... On D side.... try adding listen after bind. On python side. Don't think you need to call bind the client socket ( this may cause problems). Cheers, A.
May 10 2018