在Ruby 1.9.3 C gem中打开文件(套接字)时关闭流(IOError)

时间:2012-05-30 00:18:22

标签: c ruby file io gem

以下是代码。它最初是为1.8.7编写的,自2006年以来没有被触及。我已经成功地获得了一个姐妹函数bt_rfcomm_socket_connect(),它不使用GetOpenFile(),但是如果可能的话我想用Ruby Way(tm)。

263 static VALUE
264 bt_l2cap_socket_connect(VALUE self, VALUE host, VALUE port)
265 {
266   fprintf(stderr, "connecting!\n");
267     rb_io_t *fptr;
268     int fd;
269 
270     VALUE tmp = rb_check_convert_type(self, T_FILE, "IO", "to_io");
271 
272     fprintf(stderr,"Getting open file\n");
273     GetOpenFile(tmp, fptr);
274 
275     fprintf(stderr, "got open file\n");
276     fd = fptr->fd;
277 
278     fprintf(stderr, "structing\n");
279     struct sockaddr_l2 addr = { 0 };
280     char *dest = STR2CSTR(host);
281 
282     fprintf(stderr, "setting addr info\n");
283     // set the connection parameters (who to connect to)
284     addr.l2_family = AF_BLUETOOTH;
285     addr.l2_psm = (uint8_t) FIX2UINT(port);
286     str2ba( dest, &addr.l2_bdaddr );
287 
288     // connect to server
289     if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
290       fprintf(stderr, "connected!\n");
291         rb_sys_fail("connect(2)");
292     }
293 
294     return INT2FIX(0);
295 }

即使从root运行,也会发生以下情况:

bperry@bperry:~/work/rubytooth$ rvmsudo ruby sample_program.rb 
6
Doing something with a port: 4097
connecting!
Getting open file
sample_program.rb:20:in `connect': closed stream (IOError)
    from sample_program.rb:20:in `<main>'
bperry@bperry:~/work/rubytooth$ 

0 个答案:

没有答案