Simple Socket Guidance

linux

    Next

  • 1. What is the best OS for a server
    Hi all what is the best operating system for a server? it has to be able to be as a web server and use SSH (secure shell) and used for backup of data. I know of fedora11 and centos but I dont know of the best os to use. Regards GK
  • 2. HOW2 linux/gmail/send *.pdf ?
    I thought I had posted this already ?! I've got web-based gmail as my backup email system, and I hate attatchments, which I never use, but someone asked me to mail him a *.pdf. Now I can't see on my gmail 'reply mode page' where/how to attatch. I'd expect to be able to just enter the path/file to be attatched. My connection costs are high, and I don't want to 'drink the beer while the fridge door remains open'. I want to open the door and remove the beer and close the door in 2 seconds, and drink at my leisure - if you get me. == TIA.
  • 3. blog CAPTCHA
    How would you feel if you had graduated in 1976: B.Sc. majoring in Comptr.Sc. & maths, and weren't too happy the way blogs had crowded out usenet, and prefer to use lynx instead of a 'full featured browser', and had to use opera to handle the CAPTCHA, which clearly shows as "MK75" with possible spaces, which can't be determined because the chars are offset and randomly tilted, and you tried: "75MK", "7 5MK", "75 MK", "7 5 MK", and the system still gives an error, after you've spent time composing an important query? Does the CAPTCHA remain valid for the page on your browser until it's 'returned/submitted', or is there a time-out? == TIA. PS.I suppose you already know that the blog comments are completely illegible, unless cutNpasted to a separate file?

Simple Socket Guidance

Postby polar » Tue, 28 Nov 2006 23:51:54 GMT

Hi,

I'm currently working on a project in which I need to run a process on
one machine that can invoke a process on another machine over the
internet. For example, pressing [enter] on my machine will run a script
on another machine (with static IP) arbitrarily far away.

I'm new to this sort of coding, so I just wanted to get a rough idea
what I should be diving into. This would be the job for a socket,
correct? Or servlet? Am I right in looking at Ruby and Python? If you
could just steer me in the right direction, I can manage. Thanks a lot.


Re: Simple Socket Guidance

Postby ipnwsec » Thu, 30 Nov 2006 08:02:11 GMT

Hi,

what u need to do is write a client server program.
Server runs on a remote machine on which u need to invoke a process.
and client runs on your machine.

You can implement these client servrers using sockets based on tcp.

When ever you press ENTER on client machine send a message on the tcp
connection from client to server. On server decode the message and
invoke a process using fork..etc.

If you want any more info please feel free to contact

you can post your further questions regarding this on this group :
 XXXX@XXXXX.COM 


Thanks,
Sunil
 http://www.**--****.com/ 





Similar Threads:

1.Newbie: Could use tutorial guidance of experienced TCP/IP sockets programmer

2.Newbie: Could use tutorial guidance of experienced TCP/IP sockets programmer

3.Need help with a simple UNIX sockets server based on IO::Socket::UNIX

Hi. I've tried to create a simple client + server that communicate
through a unix socket.

As with all socket servers, it has a loop where it waits for
connections:

while ($client = $sock->accept()) {
  # handle client here
}

The problem is that $sock->accept() is returning undef on each
alternate client connection with error "No child processes".

I made this temporary workaround that does work but of course I must be
screwing up somewhere else:

while ($client = $sock->accept() || $client = $sock->accept()) {
  # handle client here
}

I hope someone can help or provide an example. I've included the client
+ server code below for those who are interrested in taking a peek.

Thanks,
Craig Manley

############ client ############
#!/usr/bin/perl -w
use strict;
use IO::Socket;

my $sockname = 'mysocket';

my $client = IO::Socket::UNIX->new('Peer' => $sockname,
                                   'Type' => SOCK_STREAM,
                                   'Timeout' => 50) or die "$0: error
connecting to '$sockname': $ [at] \n";
my $pid = fork();
unless(defined($pid)) {
  die("Fork this! I cannot forking fork!\n");
}
if ($pid) {
  write_sock();
  waitpid($pid, 0);
}
else {
  read_sock();
}

sub write_sock {
  for (1..10) {
    print $client "testline number $_\n"; # print to socket
  }
  print $client "\n"; # empty line causes server to terminate
connection
  print "Done writing.\n"; # (goes to stdout, not socket)
}

sub read_sock {
  while (my $line = <$client>) {
    print $line; # report to stdout
    # simulate someone reading slooowly (50ms/line):
    select(undef, undef, undef, 0.05);
  }
}




########### server ############
#!/usr/bin/perl -w
use strict;
use IO::Socket;
use POSIX ":sys_wait_h"; # (for WNOHANG)
use Data::Dumper qw(Dumper);

# example using unix domain socks
# once the file is created as a socket, any client can
# interact with it

my $sockname = 'mysocket';


service_clients( get_sock() ); # wait for incoming requests


sub get_sock {
  unlink $sockname;
  my $sock = IO::Socket::UNIX->new('Local'  => $sockname,
                                   'Type'   => SOCK_STREAM,
                                   'Listen' => SOMAXCONN) || die "$0:
error starting daemon on '$sockname': $ [at] \n";
  # you might want to change permissions and ownership, e.g.:
  #chmod 0600, $sockname;
  #chown scalar getpwnam('nobody'), 0, $sockname;
  return $sock;
}

sub service_clients {
  my $sock = shift;
  $SIG{CHLD} = \&reaper;
  my $client;
  while ($client = $sock->accept()) { # Why the hell does it return
undef on the next iteration?
  # while ($client = $sock->accept() || $client = $sock->accept()) { #
This strangely enough does work.

    # fork yet another process to prevent buffer deadlock. one proc
writes to
    # the sock, the other reads the deamons response
    my $pid = fork();
    unless(defined($pid)) {
      die("Fork this! I cannot forking fork!\n");
    }
    if ($pid) { # parent
      print "$pid $$: I'm the parent and am going to wait for another
client.\n";
      close($client); # no use to parent
      next; # be ready for another client
    }
    print "$pid $$: I'm the child and I'm going to service the
client.\n";
    # child
    close($sock); # no use to child
    process_requests($client);
    print "$pid $$: I'm the child and I'm going to exit.\n";
    exit; # terminate child
  }
  print "Damnit! I'm the server and I finished unexpectedly: $!\n";
}

sub process_requests {
  my $client = shift;
  $0 = "unixsockd: handling requests...";
  # read from client until empty line which causes it to close
connection
  while ( my $line = <$client> ) { # read line from socket
    if ($line =~ /^\s$/) {
      last; # exit on empty line
    }
    chomp($line);
    # put some more useful code here to read each line or whatever...
    printf $client "%s: %s, handled by PID %d\n",
    scalar localtime(time), $line, $$;
    # return something to client
  }
}

sub reaper {
  while (waitpid(-1,WNOHANG) > 0) {}
  $SIG{CHLD} = \&reaper;
}

4.Newbie: Could use tutorial guidance of experienced TCP/IP sockets programmer

5.Newbie: Could use tutorial guidance of experienced TCP/IP sockets programmer

6. Simple client/server socket program works on one Linux machine, it doesnot work on other machine

7. Simple Socket program hangs system on recv()

8. Help needed in a simple UDP socket program



Return to linux

 

Who is online

Users browsing this forum: No registered users and 72 guest