Created
June 22, 2023 13:26
-
-
Save ppsdatta/1bd80803ef82baafa7cb70d4a196bfd1 to your computer and use it in GitHub Desktop.
Message passing between computers with sockets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import time | |
import re | |
import os | |
import sys | |
HOST = sys.argv[1] | |
PORT = int(sys.argv[2]) | |
sleep_time = 1 | |
while True: | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
s.connect((HOST, PORT)) | |
s.sendall(b"get\n\r") | |
data = s.recv(1024) | |
response = data.decode("utf-8") | |
response = re.sub("[\\r\\n]", "", response) | |
if response == "none": | |
sleep_time = 15 | |
else: | |
message = response.split(",")[0] | |
print(message) | |
if message.startswith("http"): | |
os.system("open {}".format(message)) | |
sleep_time = 60 * 5 | |
print("Next ping after {} seconds".format(sleep_time)) | |
time.sleep(sleep_time) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$serverIP = Read-Host -Prompt 'Input IP' | |
$message = Read-Host -Prompt 'Enter the message to send' | |
$tcpConnection = New-Object System.Net.Sockets.TcpClient("192.168.0.100", 8000) | |
$tcpStream = $tcpConnection.GetStream() | |
$writer = New-Object System.IO.StreamWriter($tcpStream) | |
$writer.AutoFlush = $true | |
$writer.WriteLine("put: $message") | Out-Null | |
$writer.Close() | |
$tcpConnection.Close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use IO::Socket; | |
my $message = ""; | |
my $timestamp = ""; | |
sub handle { | |
my $c = shift; | |
my $command = <$c>; | |
$command = "" unless $command; | |
chomp $command; | |
$command =~ s/[\r\n]//g; | |
if ($command =~ /^put: (.*)$/) { | |
print "PUT]\n"; | |
$message = $1; | |
$timestamp = time(); | |
print $c "OK\n"; | |
} | |
elsif ($command =~ /^get$/) { | |
print "GET]\n"; | |
my $cur = time(); | |
if (($timestamp ne "") && ($cur - $timestamp < 60)) { | |
print $c "$message,$timestamp\n"; | |
} | |
else { | |
print $c "none\n"; | |
} | |
} | |
close $c; | |
} | |
my $server = IO::Socket::INET->new( | |
LocalPort => 8000, | |
Type => SOCK_STREAM, | |
Reuse => 1, | |
Listen => 10 | |
) or die "Could not create the server\n"; | |
my $out = `ifconfig | grep 192.168*`; | |
print $out, "\n"; | |
while (my $client = $server->accept) { | |
handle $client; | |
} | |
close $server; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment