View topic - how can i create the Socket connection using Perl?

how can i create the Socket connection using Perl?

For the old QNX2 and QNX4 RTOS

how can i create the Socket connection using Perl?

Postby roy.wanglifeng » Sat Jan 06, 2007 7:26 am

hi,
there are some test code write by Perl. the OS is Qnx 4.25.
my probelm is , i can not create the socket connection, i do not why, becase this code can run at any other OS.

this is the Perl code :
Code: Select all
#!/usr/bin/perl

package sock;

;# USAGE:
;# ======
;#
;# To open a connection to a socket:
;#
;#        $handle = &sock'open($hostname, $port) || die $!;
;#        # hostname & port can each be either a name or a number
;#
;# Read and write the same as with any other file handle:
;#
;#        print $handle "hello, socket\n";
;#        $response = <$handle>;
;#
;# To close cleanly:
;#
;#        &sock'close($handle);
;#
;# To close all open sockets, in case of an emergency exit:
;#
;#        &sock'close_all;
;#
;# AUTHOR:        David Noble (dnoble@ufo.jpl.nasa.gov)
;# DATE:        11 Feb 1993
;#
;# Modify and use as you see fit, but please leave my name on
;# it as long as it still resembles the original code.
;#
;#############################################################################

;# Get system-specific socket parameters, make assumptions if necessary.
$sockaddr_t = 'S n a4 x8';
#eval "require 'sys/socket.ph'";
#eval <<'END_SOCKET_DEFINITIONS' if $@;
#  sub AF_INET                { 2; }
#  sub SOCK_STREAM        { 1; }
#  sub SOL_SOCKET        { 65535; }
#  sub SO_REUSEADDR        { 4; }
#END_SOCKET_DEFINITIONS

$AF_INET=2;
$SOCK_STREAM=1;
$SOL_SOCKET=65535;
$SO_REUSEADDR=4;
;# Seed the generation of names for file handles.
$latest_handle = 'sock0000000001';

sub open {
  local ($remote_host, $remote_port) = @_;
  if (!$remote_port) {
    $! = "bad arguments to sock'open()";
    return 0;
  }
  $sock = ++$latest_handle;

  ;# Look up the port if it was specified by name instead of by number.
  if ($remote_port =~ /\D/o) {
    ($name,$aliases,$remote_port) = getservbyname($remote_port,'tcp');
  }

  ;# Look up the address if it was specified by name instead of by number.
  if ($remote_host =~ /\D/o) {
    ($name,$aliases,$type,$len,$remote_addr) = gethostbyname($remote_host);
  } else {
    $remote_addr = $remote_host;
  }

  ;# Make the socket structures.
  #$this = pack($sockaddr_t, &AF_INET, 0, "\0\0\0\0");
  #$this = pack($sockaddr_t, 2, 0, "\0\0\0\0");#***************************
  $this = pack($sockaddr_t, $AF_INET, 0, "\0\0\0\0");
  #$remote_sock = pack($sockaddr_t, &AF_INET, $remote_port, $remote_addr);#**************************
  #$remote_sock = pack($sockaddr_t, 2, $remote_port, $remote_addr);
  $remote_sock = pack($sockaddr_t, $AF_INET, $remote_port, $remote_addr);

  ;# Make the socket filehandle.
  ($name,$aliases,$proto) = getprotobyname('tcp');
  #socket($sock, &AF_INET, &SOCK_STREAM, $proto) || return 0; #*******************************
  #socket($sock, 2, 1, $proto) || return 0;
  socket($sock, $AF_INET, $SOCK_STREAM, $proto) || return 0;

  ;# Set up the port so it's freed as soon as we're done.
  #setsockopt($sock, &SOL_SOCKET, &SO_REUSEADDR, 1);  #*********************************
  #setsockopt($sock, 65535, 4, 1);
  setsockopt($sock, $SOL_SOCKET, $SO_REUSEADDR, 1);

  ;# Bind this socket to an address.
  bind($sock, $this) || return 0;

  ;# Call up the remote socket.
  [b]connect($sock,$remote_sock)|| return 0;[/b]

  $handles{$sock} = 1;
  $oldfh = select($sock); $| = 1; select($oldfh);
  return "sock'" . $sock;
}

sub close {
  local ($sock) = shift(@_) || return 0;
  shutdown ($sock, 2);
  delete $handles{$sock};
}

sub close_all {
  for $sock (keys %handles) {
    shutdown ($sock, 2);
    delete $handles{$sock};
  }
}



just the "connect($sock,$remote_sock)|| return 0" , it always can not connect.
why?

thanks.
roy.wanglifeng
New Member
 
Posts: 4
Joined: Sat Jan 06, 2007 7:21 am

RE: how can i create the Socket connection using Perl?

Postby maschoen » Sun Jan 07, 2007 2:12 am

This suggests that the port of Perl may be the problem. Assuming you have TCP/IP running on QNX 4 and working properly of course.
maschoen
QNX Master
 
Posts: 2715
Joined: Wed Jun 25, 2003 5:18 pm

RE: how can i create the Socket connection using Perl?

Postby roy.wanglifeng » Tue Jan 09, 2007 2:27 am

what's that mean? is there any port for Perl? how can i check whick port is it?

thanks .
roy.wanglifeng
New Member
 
Posts: 4
Joined: Sat Jan 06, 2007 7:21 am

Re: RE: how can i create the Socket connection using Perl?

Postby maschoen » Tue Jan 09, 2007 2:41 am

roy.wanglifeng wrote:what's that mean? is there any port for Perl? how can i check whick port is it?

thanks .


This is a funny question. If you are using perl, you got it from somewhere, so there is a a port for Perl. I don't know how you would check "which port" it is. Your choices I think are as follows. Find the source for the port that you are using and try to fix it, or get an unported version, and port it yourself.
maschoen
QNX Master
 
Posts: 2715
Joined: Wed Jun 25, 2003 5:18 pm

RE: Re: RE: how can i create the Socket connection using Per

Postby roy.wanglifeng » Tue Jan 09, 2007 2:59 am

sorry.
maybe you mean this one ? this is the code how to use it.

$handle2 = &sock'open("localhost", "21") or die('can not connect'); #port 21.

but i have try lots of ports, 1234,4321,........, all can not.

thanks very much for your reply.
roy.wanglifeng
New Member
 
Posts: 4
Joined: Sat Jan 06, 2007 7:21 am

RE: Re: RE: how can i create the Socket connection using Per

Postby mario » Tue Jan 09, 2007 3:24 am

Seems to be a misunderstanding here Roy. Maschoen is using the word port to describe the process of taking sources code for a program written for a different OS and PORTING it to your os of choice, in this case QNX.

You have done the porting process yourself or used somebody elses port ;-) This has nothing to do with the TCP/IP port number.

In the process of porting Perl to QNX the person that did the port may or may not have put in TCP/IP features.
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

RE: Re: RE: how can i create the Socket connection using Per

Postby roy.wanglifeng » Tue Jan 09, 2007 3:37 am

quite confused.........

$sockaddr_t = 'S n a4 x8';
$AF_INET=2;
$SOCK_STREAM=1;
$SOL_SOCKET=65535;
$SO_REUSEADDR=4;

these code above shold be no problem? or can i say the code here is no probelm , i just need a setting in the Qux?

sorry, this is the first program i do in Qnx by Perl..
roy.wanglifeng
New Member
 
Posts: 4
Joined: Sat Jan 06, 2007 7:21 am


Return to QNX2 and QNX4

Who is online

Users browsing this forum: No registered users and 3 guests