×

Discussion Board

Results 1 to 2 of 2

Thread: Sockets

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    10

    Sockets

    I am trying yo run the sockets example that comes with the sdk but i read that you need to have a working server for the example to connect to.

    Can anyone tell me where to get the code for a simple echo server so that i can work with the example?

    I would preferr something outside the emulator but if i could get ANY server, it would be great.

    Hope u can help me.
    And thanks

  2. #2
    Registered User
    Join Date
    Aug 2003
    Location
    Oulu, Finland
    Posts
    1,122
    Here's a simple perl script that listens on a port and prints out any data it reads from the socket. I originally adapted it from the perlipc examples. You can easily modify it to print data to the client socket too.

    Code:
    #!perl
    
    use Socket;
    my $EOL = "\015\012";
    
    sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }
    
    my $port = shift || 1234;
    my $proto = getprotobyname('tcp');
    
    ($port) = $port =~ /^(\d+)$/ or die "invalid port";
    
    socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
    setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!";
    bind(Server, sockaddr_in($port, INADDR_ANY)) || die "bind: $!";
    listen(Server, SOMAXCONN) || die "listen: $!";
    
    logmsg "server started on port $port";
    
    my $paddr;
    
    for ( ; $paddr = accept(Client, Server); close Client) {
        my($port,$iaddr) = sockaddr_in($paddr);
        my $name = gethostbyaddr($iaddr,AF_INET);
    
        logmsg "connection from $name [", inet_ntoa($iaddr), "] at port $port";
    
        while (<Client>) {
            print;
        }
    }
    Lauri

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •