FAQ
Hi there,

i have written a socket c++ server, which listens for clients and
understands some commands.
here the structs of the server commands:


enum command { SET_PRIO, CLEAR, SEND_STRING, SET_CURSOR, SHOW_CURSOR };

struct string_packet_t
{
char str[DATA_LENGTH];
};

struct int_packet_t
{
int data;
};


struct cmd_packet_t
{
command type;
union
{
int_packet_t int_pkg;
string_packet_t str_pkg;
};
};

a c++ client would now send following command over the socket to the the
server:

cmd_packet_t command;
command.type=SEND_STRING;
strcpy(command.str_pkg.str, "Hello World");
write(fd, &command, sizeof(command);

with c/c++ all is working fine.

The problem is the perl client. after setting up the socket i tried:

print SOCKET sprintf(%u%d, 0, 2)
# 0 For the first command == SET_PRIO, 2 for the argument
but the server receives no useable data.
("12848" for the command value, instead of 0 and
"1" for the integer argument)

any idea?

thank,

Simon Funke

--
Sparen beginnt mit GMX DSL: http://www.gmx.net/de/go/dsl

Search Discussions

  • Zentara at Apr 6, 2005 at 2:00 pm

    On Wed, 6 Apr 2005 00:15:57 +0200 (MEST), news-funsi@gmx.de wrote:
    i have written a socket c++ server, which listens for clients and
    understands some commands.
    here the structs of the server commands:
    a c++ client would now send following command over the socket to the the
    server:
    strcpy(command.str_pkg.str, "Hello World");
    write(fd, &command, sizeof(command);
    with c/c++ all is working fine.

    The problem is the perl client. after setting up the socket i tried:

    print SOCKET sprintf(%u%d, 0, 2)
    # 0 For the first command == SET_PRIO, 2 for the argument
    but the server receives no useable data.
    ("12848" for the command value, instead of 0 and
    "1" for the integer argument)

    any idea?
    I think you are getting too much of your C++ syntax into
    the perl code.
    Instead of

    print SOCKET sprintf(%u%d, 0, 2)
    try
    print SOCKET "Hello World\n";


    If you care to post your server code, so we can test it, you
    may get better answers.


    --
    I'm not really a human, but I play one on earth.
    http://zentara.net/japh.html
  • News-funsi at Apr 6, 2005 at 5:41 pm
    thanks zentara for the answer, but unfortunately this does not work.

    I've uploaded the the C++ server/client and the testclient for perl.

    after unpacking and compiling the fundaemon, the server executable is
    src/fundaemon, which will create a file called /tmp/fundaemon_socket and
    listens for clients.
    the c++ testclient is called "testclient", and has a small menu to control
    the server.

    C++ Server and Client
    www.honktown.de/funk/fundaemon.tar.bz2


    Perl Client:
    http://www.honktown.de/funk/fundaemon_client.pm

    greetings,

    Simon Funke

    --
    Handyrechnung zu hoch? Tipp: SMS und MMS mit GMX
    Seien Sie so frei: Alle Infos unter http://www.gmx.net/de/go/freesms
  • Jay Savage at Apr 6, 2005 at 6:23 pm

    On Apr 6, 2005 1:41 PM, news-funsi@gmx.de wrote:
    thanks zentara for the answer, but unfortunately this does not work.

    I've uploaded the the C++ server/client and the testclient for perl.

    after unpacking and compiling the fundaemon, the server executable is
    src/fundaemon, which will create a file called /tmp/fundaemon_socket and
    listens for clients.
    the c++ testclient is called "testclient", and has a small menu to control
    the server.

    C++ Server and Client
    www.honktown.de/funk/fundaemon.tar.bz2

    Perl Client:
    http://www.honktown.de/funk/fundaemon_client.pm

    greetings,

    Simon Funke
    <code>
    #!/usr/bin/perl

    use IO::Socket;
    #use strict;
    use Socket;

    #use strict;
    my $client;

    onLoad();
    onUnLoad();

    sub onLoad {
    $client = new IO::Socket::UNIX (
    Type => SOCK_STREAM,
    Peer => "/tmp/fundaemon_socket",
    Timeout => 5,
    )
    or die $@;
    sleep(1);
    print $client sprintf("%u%d", 0,2);
    sleep(1);
    print $client sprintf("%u%d", 0,2);
    sleep(1);
    print $client sprintf("%c", 0);
    sleep(1);
    print $client sprintf("%d\n", 05);

    #socket($socket, PF_UNIX, SOCK_STREAM, 0);
    #$client = sockaddr_un('/tmp/fundaemon_socket');
    #connect($socket, $client);

    sleep (5);
    #print $socket '02fewfewfewfewggggggggggggggggggggggggggg';
    #print $socket sprintf( "%d%d", 0, 2);

    #sleep 10;

    }


    sub onUnLoad {
    my ($self)=@_;
    close ($client);
    }

    </code>

    Simon,

    Firt things first. Why did you comment out use strict? That's never
    a good sign. also, loading both IO::Socket::Unix and Socket probably
    isn't the best idea. the main problem here, though would seem to be
    mixing your syntax. You're calling the object-oriented IO::Socket
    interface, but you're using it like you would the builtin functions.
    You're also using incorect argument names. Take another look at the
    docs for IO::Socket. To get you started, though, see if this doesn't
    work better for you:

    #!/usr/bin/perl

    use IO::Socket;
    use strict;
    use warnings;

    my $client = IO::Socket::UNIX->new (
    Type => SOCK_STREAM,
    PeerAddr => "/tmp/fundaemon_socket",
    Timeout => 5,
    )
    or die $@;
    sleep(1);
    print $client sprintf("%u%d", 0,2);
    # better yet, make sure that buffering is handled correctly:
    $client->send($msg) ;

    HTH,

    --jay
  • News-funsi at Apr 7, 2005 at 6:00 pm
    Hi Jay,

    thanks for the answer.
    Firt things first. Why did you comment out use strict? That's never
    a good sign. also, loading both IO::Socket::Unix and Socket probably
    isn't the best idea. the main problem here, though would seem to be
    mixing your syntax. You're calling the object-oriented IO::Socket
    interface, but you're using it like you would the builtin functions.
    You're also using incorect argument names.
    Your right. It was a dirty hack, and because it did not work with
    IO::Socket::Unix, i tried the Socket library.

    i'm playing with your version
    (http://www.honktown.de/funk/fundaemon_client.pm), but i have the same
    problem as before :(

    Simon Funke


    --
    Sparen beginnt mit GMX DSL: http://www.gmx.net/de/go/dsl
  • Jay Savage at Apr 7, 2005 at 6:40 pm

    On Apr 7, 2005 2:00 PM, news-funsi@gmx.de wrote:
    Hi Jay,

    thanks for the answer.
    Firt things first. Why did you comment out use strict? That's never
    a good sign. also, loading both IO::Socket::Unix and Socket probably
    isn't the best idea. the main problem here, though would seem to be
    mixing your syntax. You're calling the object-oriented IO::Socket
    interface, but you're using it like you would the builtin functions.
    You're also using incorect argument names.
    Your right. It was a dirty hack, and because it did not work with
    IO::Socket::Unix, i tried the Socket library.

    i'm playing with your version
    (http://www.honktown.de/funk/fundaemon_client.pm), but i have the same
    problem as before :(

    Simon Funke
    You're still not creating the object correctly:

    my $client = IO::Socket::UNIX->new (

    #NOT my $client = new IO::Socket::UNIX (

    and the argument is PeerAddr, not Peer

    As for the error about global symbol...you need to get in the habit of
    using my and our to scope your variables in perl. If you haven't
    already, read perldoc perlintro. Also take a look at perldoc
    perlboot, etc., on object-oriented perl. Also read the docs for
    IO::Socket, and ake sure you're reading the documentation for
    IO::Socket, not Socket, or the built-in functions. They're all very
    different things.

    HTH, --jay

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedApr 5, '05 at 10:16p
activeApr 7, '05 at 6:40p
posts6
users3
websiteperl.org

3 users in discussion

News-funsi: 3 posts Jay Savage: 2 posts Zentara: 1 post

People

Translate

site design / logo © 2022 Grokbase