[RC1] Pratica06 (guide only)
Signed-off-by: TiagoRG <tiago.rgarcia@ua.pt>
This commit is contained in:
parent
d49ba87135
commit
7423b92a6e
Binary file not shown.
|
@ -0,0 +1,31 @@
|
|||
import socket
|
||||
import signal
|
||||
import sys
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
print('\nDone!')
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
print('Press Ctrl+C to exit...')
|
||||
|
||||
##
|
||||
|
||||
ip_addr = "127.0.0.1"
|
||||
tcp_port = 5005
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
sock.connect((ip_addr, tcp_port))
|
||||
|
||||
while True:
|
||||
try:
|
||||
message=input("Message to send? ").encode()
|
||||
if len(message)>1:
|
||||
sock.send(message)
|
||||
response = sock.recv(4096).decode()
|
||||
print('Server response: {}'.format(response))
|
||||
except (socket.timeout, socket.error):
|
||||
print('Server error. Done!')
|
||||
sys.exit(0)
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
import socket
|
||||
import signal
|
||||
import sys
|
||||
import datetime
|
||||
import struct
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
print('\nDone!')
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
print('Press Ctrl+C to exit...')
|
||||
|
||||
##
|
||||
|
||||
ip_addr = "127.0.0.1"
|
||||
tcp_port = 5005
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
sock.connect((ip_addr, tcp_port))
|
||||
|
||||
order=0
|
||||
while True:
|
||||
try:
|
||||
message=input("Message to send [max 20 chars]? ")
|
||||
version=1
|
||||
order+=1
|
||||
size=len(message)
|
||||
if size<20:
|
||||
message+='-'*(20-size)
|
||||
pkt=struct.pack('!BLL20s',version,order,size,message[:20].encode())
|
||||
sock.send(pkt)
|
||||
except (socket.timeout, socket.error):
|
||||
print('Server error. Done!')
|
||||
sys.exit(0)
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
import socket
|
||||
import signal
|
||||
import sys
|
||||
import datetime
|
||||
import struct
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
print('\nDone!')
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
print('Press Ctrl+C to exit...')
|
||||
|
||||
##
|
||||
|
||||
ip_addr = "127.0.0.1"
|
||||
tcp_port = 5005
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
sock.connect((ip_addr, tcp_port))
|
||||
|
||||
order=0
|
||||
while True:
|
||||
try:
|
||||
message=input("Message to send? ").encode()
|
||||
version=1
|
||||
order+=1
|
||||
size=len(message)
|
||||
pkt=struct.pack('!BLL{}s'.format(size),version,size,order,message)
|
||||
sock.send(pkt)
|
||||
except (socket.timeout, socket.error):
|
||||
print('Server error. Done!')
|
||||
sys.exit(0)
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
import socket
|
||||
import signal
|
||||
import sys
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
print('\nDone!')
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
print('Press Ctrl+C to exit...')
|
||||
|
||||
##
|
||||
|
||||
ip_addr = "127.0.0.1"
|
||||
udp_port = 5005
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
while True:
|
||||
message=input("Message to send? ").encode()
|
||||
if len(message)>0:
|
||||
sock.sendto(message, (ip_addr, udp_port))
|
|
@ -0,0 +1,44 @@
|
|||
import socket
|
||||
import threading
|
||||
import signal
|
||||
import sys
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
print('\nDone!')
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
print('Press Ctrl+C to exit...')
|
||||
|
||||
##
|
||||
|
||||
def handle_client_connection(client_socket,address):
|
||||
print('Accepted connection from {}:{}'.format(address[0], address[1]))
|
||||
try:
|
||||
while True:
|
||||
request = client_socket.recv(1024)
|
||||
if not request:
|
||||
client_socket.close()
|
||||
else:
|
||||
msg=request.decode()
|
||||
print('Received {}'.format(msg))
|
||||
msg=("ECHO: "+msg).encode()
|
||||
client_socket.send(msg)
|
||||
except (socket.timeout, socket.error):
|
||||
print('Client {} error. Done!'.format(address))
|
||||
|
||||
ip_addr = "0.0.0.0"
|
||||
tcp_port = 5005
|
||||
|
||||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
server.bind((ip_addr, tcp_port))
|
||||
server.listen(5) # max backlog of connections
|
||||
|
||||
print('Listening on {}:{}'.format(ip_addr, tcp_port))
|
||||
|
||||
while True:
|
||||
client_sock, address = server.accept()
|
||||
client_handler = threading.Thread(target=handle_client_connection,args=(client_sock,address),daemon=True)
|
||||
client_handler.start()
|
||||
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
import socket
|
||||
import selectors
|
||||
import signal
|
||||
import sys
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
print('\nDone!')
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
print('Press Ctrl+C to exit...')
|
||||
|
||||
##
|
||||
|
||||
def handle_client_connection(server_socket):
|
||||
client_socket, address = server_socket.accept()
|
||||
print('Accepted connection from {}:{}'.format(address[0], address[1]))
|
||||
client_socket.setblocking(False)
|
||||
sel.register(client_socket, selectors.EVENT_READ, handle_data)
|
||||
|
||||
def handle_data(client_socket):
|
||||
address=client_socket.getpeername()
|
||||
try:
|
||||
request = client_socket.recv(1024)
|
||||
if not request:
|
||||
sel.unregister(client_socket)
|
||||
client_socket.close()
|
||||
else:
|
||||
msg=request.decode()
|
||||
print('Received {}'.format(msg))
|
||||
msg=("ECHO: "+msg).encode()
|
||||
client_socket.send(msg)
|
||||
except (socket.timeout,socket.error):
|
||||
print('Client {} error. Done!'.format(address))
|
||||
|
||||
ip_addr = "0.0.0.0"
|
||||
tcp_port = 5005
|
||||
|
||||
sel = selectors.DefaultSelector()
|
||||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
server.bind((ip_addr, tcp_port))
|
||||
server.listen(5) # max backlog of connections
|
||||
|
||||
server.setblocking(False) #Set to non-blocking
|
||||
sel.register(server, selectors.EVENT_READ, handle_client_connection)
|
||||
|
||||
print('Listening on {}:{}'.format(ip_addr, tcp_port))
|
||||
|
||||
while True:
|
||||
events = sel.select()
|
||||
for key, mask in events:
|
||||
print(key)
|
||||
callback = key.data
|
||||
selsocket=key.fileobj
|
||||
callback(selsocket)
|
|
@ -0,0 +1,44 @@
|
|||
import socket
|
||||
import threading
|
||||
import signal
|
||||
import sys
|
||||
import struct
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
print('\nDone!')
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
print('Press Ctrl+C to exit...')
|
||||
|
||||
##
|
||||
|
||||
def handle_client_connection(client_socket,address):
|
||||
print('Accepted connection from {}:{}'.format(address[0], address[1]))
|
||||
try:
|
||||
while True:
|
||||
request = client_socket.recv(29)
|
||||
if not request:
|
||||
client_socket.close()
|
||||
else:
|
||||
pkt=struct.unpack('!BLL20s',request)
|
||||
print(pkt)
|
||||
print('Received ver: {}, order: {}, size: {} -> {}'.format(pkt[0],pkt[1],pkt[2],pkt[3].decode()))
|
||||
except (socket.timeout, socket.error):
|
||||
print('Client {} error. Done!'.format(address))
|
||||
|
||||
ip_addr = "0.0.0.0"
|
||||
tcp_port = 5005
|
||||
|
||||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
server.bind((ip_addr, tcp_port))
|
||||
server.listen(5) # max backlog of connections
|
||||
|
||||
print('Listening on {}:{}'.format(ip_addr, tcp_port))
|
||||
|
||||
while True:
|
||||
client_sock, address = server.accept()
|
||||
client_handler = threading.Thread(target=handle_client_connection,args=(client_sock,address),daemon=True)
|
||||
client_handler.start()
|
||||
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
import socket
|
||||
import signal
|
||||
import sys
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
print('\nDone!')
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
print('Press Ctrl+C to exit...')
|
||||
|
||||
##
|
||||
|
||||
ip_addr = "0.0.0.0"
|
||||
udp_port = 5005
|
||||
|
||||
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
|
||||
sock.bind((ip_addr, udp_port))
|
||||
|
||||
while True:
|
||||
message, sender_addr_port = sock.recvfrom(2048)
|
||||
print('Message from {}:{} -> {}'.format(sender_addr_port[0],sender_addr_port[1],message.decode()))
|
Loading…
Reference in New Issue