最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

gjs - GIO Socket class - Stack Overflow

programmeradmin2浏览0评论

I tried to create a listening unix domain socket. I have two functions.
The first one called create_unixSocket_listener creates a socket over Gio.Socket, binds to the unix socket address, listens, and accept an incoming remote_socket.
The second one called create_socketListener creates a Gio.SocketListener instance, adds the unix socket address, and accept an incoming connection

import GLib from 'gi://GLib';
import Gio from 'gi://Gio';

const loop = new GLib.MainLoop(null, false),
      pwd= GLib.get_current_dir();

let socket;
function create_unixSocket_listener (fileName)
   {
   let socket_address, remote_socket, file;

   socket=Gio.Socket.new (Gio.SocketFamily.UNIX, Gio.SocketType.STREAM, Gio.SocketProtocol.DEFAULT);
   socket.init(null);
   
   socket_address=Gio.UnixSocketAddress.new (pwd+"/"+fileName);
   file=Gio.File.new_for_path (socket_address.get_path());
   if (file.query_exists (null)) file.delete (null);
   
   socket.set_blocking (true);
   socket.set_timeout (0);
   socket.set_keepalive(true);
   socket.bind (socket_address, true);
   socket.listen ();
   remote_socket=socket.accept (null);
   log (remote_socket);
   }

let socketListener;
function create_socketListener (fileName)
   {
   let socket_address, connection, file;
   socketListener=Gio.SocketListener.new ();
   
   socket_address=Gio.UnixSocketAddress.new (pwd+"/"+fileName);   
   file=Gio.File.new_for_path (socket_address.get_path());
   if (file.query_exists (null)) file.delete (null);
   
   socketListener.add_address (socket_address, Gio.SocketType.STREAM, Gio.SocketProtocol.DEFAULT, null);
   connection=socketListener.accept (null);
   log (connection);
   }

create_socketListener ("unixSocket");

await loop.runAsync();

When i start create_socketListener the "unixSocket" file is created, and the socket is listening for an incoming connection.
When i open terminal and enter nc -U unixSocket, then an error shows up JS ERROR: TypeError: value is null
The return value of the instance method socketListener.accept is null.
I cant retrieve any connection


When i use create_unixSocket_listener and open a terminal with nc -U unixSocket, i get the remote_socket --> success, but then after 5 seconds nc is closing, altough, the socket variable is global, and i set keepalive, and timeout to 0..


So both functions are useless, seems the whole Gio Socket class and the other classes which are for socket connections, do not work in GJS. Is that right?

on the remote_socket, which i get back with success, i cant use the receive function and create_source function. I have to use a UnixInputStream and set the file descriptor manually, to receive or write anything back, but after 5 seconds nc closes. -why?

if socketListener doesnt work, i cant use Gio.UnixConnection and i still dont know, if im able to use Gio.TlsConnection for my manually set up unix input and output streams

Do you have working examples?

I tried to create a listening unix domain socket. I have two functions.
The first one called create_unixSocket_listener creates a socket over Gio.Socket, binds to the unix socket address, listens, and accept an incoming remote_socket.
The second one called create_socketListener creates a Gio.SocketListener instance, adds the unix socket address, and accept an incoming connection

import GLib from 'gi://GLib';
import Gio from 'gi://Gio';

const loop = new GLib.MainLoop(null, false),
      pwd= GLib.get_current_dir();

let socket;
function create_unixSocket_listener (fileName)
   {
   let socket_address, remote_socket, file;

   socket=Gio.Socket.new (Gio.SocketFamily.UNIX, Gio.SocketType.STREAM, Gio.SocketProtocol.DEFAULT);
   socket.init(null);
   
   socket_address=Gio.UnixSocketAddress.new (pwd+"/"+fileName);
   file=Gio.File.new_for_path (socket_address.get_path());
   if (file.query_exists (null)) file.delete (null);
   
   socket.set_blocking (true);
   socket.set_timeout (0);
   socket.set_keepalive(true);
   socket.bind (socket_address, true);
   socket.listen ();
   remote_socket=socket.accept (null);
   log (remote_socket);
   }

let socketListener;
function create_socketListener (fileName)
   {
   let socket_address, connection, file;
   socketListener=Gio.SocketListener.new ();
   
   socket_address=Gio.UnixSocketAddress.new (pwd+"/"+fileName);   
   file=Gio.File.new_for_path (socket_address.get_path());
   if (file.query_exists (null)) file.delete (null);
   
   socketListener.add_address (socket_address, Gio.SocketType.STREAM, Gio.SocketProtocol.DEFAULT, null);
   connection=socketListener.accept (null);
   log (connection);
   }

create_socketListener ("unixSocket");

await loop.runAsync();

When i start create_socketListener the "unixSocket" file is created, and the socket is listening for an incoming connection.
When i open terminal and enter nc -U unixSocket, then an error shows up JS ERROR: TypeError: value is null
The return value of the instance method socketListener.accept is null.
I cant retrieve any connection


When i use create_unixSocket_listener and open a terminal with nc -U unixSocket, i get the remote_socket --> success, but then after 5 seconds nc is closing, altough, the socket variable is global, and i set keepalive, and timeout to 0..


So both functions are useless, seems the whole Gio Socket class and the other classes which are for socket connections, do not work in GJS. Is that right?

on the remote_socket, which i get back with success, i cant use the receive function and create_source function. I have to use a UnixInputStream and set the file descriptor manually, to receive or write anything back, but after 5 seconds nc closes. -why?

if socketListener doesnt work, i cant use Gio.UnixConnection and i still dont know, if im able to use Gio.TlsConnection for my manually set up unix input and output streams

Do you have working examples?

Share edited Apr 6 at 21:38 Schmaehgrunza asked Mar 7 at 11:27 SchmaehgrunzaSchmaehgrunza 2841 silver badge6 bronze badges 8
  • Hi, can you please check the syntax and spelling of you code? It seems like you have some typos like Gio.SocketProtocol.UNKOWN . It's also generally recommended to use the constructor functions like Gio.Socket.new() if possible, or calling Gio.Initable.init() immediately after construction, rather than at a later arbitrary time. – andy.holmes Commented Mar 7 at 19:13
  • @andy.holmes I changed that to Gio.SocketProtocol.UNKNOWN, then i get the <b>error: unable to create socket: Unknown protocol was specified</b>. Changed it to DEFAULT, then i get again value is null, when nc is accepted. I can not get back a connection von socketListener. – Schmaehgrunza Commented Mar 8 at 11:50
  • I changed all you mentioned, above... nothing changed... I solved the problem, why nc is closing after 5 seconds, when using create_unixSocket_listener. After stracing, i saw that gjs(Gio.Socket) is closing the file descriptor for my remote socket, thats why nc is exiting. After accepting,i have to call immediately socket.connection_factory_create_connection () and save the returned UnixConnection to a global variable, that prohibits gjs (Gio.Socket) to close the file descriptor – Schmaehgrunza Commented Mar 8 at 12:09
  • but i cant get a accepted connection or an accepted socket from <b>Class SocketListener</b> . This problem still exists – Schmaehgrunza Commented Mar 8 at 12:10
  • 1 @andy.holmes the error managment of gjs and cjs is difficult to understand. I did a larger application. Sometimes an error is logged, but gjs dont logs the line number. and sometimes it logs the line number. Its hard to find errors, if no line number shows up. Its impossible to program larger applications with that error handling. ive codium running, which shows up the line number, when it finds an error. Without codium it would be hard. Thx for your time – Schmaehgrunza Commented Apr 4 at 23:29
 |  Show 3 more comments

1 Answer 1

Reset to default 0

Question is answered by andy holmes. see comments

发布评论

评论列表(0)

  1. 暂无评论