Simple Socket Guidance
by 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
by 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