Skip to content

Async socket with threading #10

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Async_socket_read_and_write/Sender.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import socket
import threading
import ctypes
import time
import subprocess
import os
print(os.getpid())

IPV6=subprocess.getoutput("curl -6 icanhazip.com").split()[-1]


soc=socket.socket(socket.AF_INET6,socket.SOCK_DGRAM)
soc.bind((IPV6,6969))
soc.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
soc.settimeout(0.1)

flag=[False] # Item to be sent ready
flag_id=id(flag)

def serve():
global flag
try:
while True:
assert flag!=[None]
try:
if(flag==[False]):
data,addr=soc.recvfrom(1024)
print(data)
except:
pass

except AssertionError:
pass


thread=threading.Thread(target=serve)
thread.start()


while True:
try:
if(flag==[False]):
flag[0]=True
soc.sendto(b"Data",(IPV6,8080))
flag[0]=False
time.sleep(0.1)
except:
print("INTERRUPT")
flag[0]=None
break



16 changes: 16 additions & 0 deletions Async_socket_read_and_write/display.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import socket
import subprocess

IPV6=subprocess.getoutput("curl -6 icanhazip.com").split()[-1]

soc=socket.socket(socket.AF_INET6,socket.SOCK_DGRAM)
soc.bind((IPV6,8080))
count=0
while True:
try:
data,addr=soc.recvfrom(1024)
count+=1
print(data.decode(),addr,count,sep="\t",end="\r")
except Exception as e:
print(e)
break
14 changes: 14 additions & 0 deletions Async_socket_read_and_write/prompter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import socket
import subprocess


IPV6=subprocess.getoutput("curl -6 icanhazip.com").split()[-1]

soc=socket.socket(socket.AF_INET6,socket.SOCK_DGRAM)

while True:
try:
input("SEnd prompt : ")
soc.sendto(b"PROMPT",(IPV6,6969))
except:
break