Skip to content

Commit

Permalink
SCTP signaling implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
netrsr committed Jun 23, 2021
1 parent 27f857d commit fb41f36
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 50 deletions.
26 changes: 20 additions & 6 deletions sctp_client.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
from os import wait
import socket
import sctp
import time

sk = sctp.sctpsocket_tcp(socket.AF_INET)
sk.connect(("192.168.100.9", 36412))

cli = sctp.sctpsocket_udp(socket.AF_INET)
tmp = sctp.event_subscribe(cli)
tmp.set_association(1)
tmp.set_data_io(1)

print("Sending Message")
cli.autoclose = 0 # no automatic closing of the associacion

sk.sctp_send(msg='hello world')
sk.shutdown(0)
cli_ = sctp.sctpsocket_udp(socket.AF_INET)
tmp = sctp.event_subscribe(cli_)
tmp.set_association(1)
tmp.set_data_io(1)

cli.bind(("127.0.0.1", 10001))
cli_.bind(("127.0.0.1", 10002))
cli.sctp_send(msg=b'0', to=("127.0.0.1", 10000))
cli_.sctp_send(msg=b'0', to=("127.0.0.1", 10000))
time.sleep(10)

sk.close()
cli.close()
time.sleep(3)
cli_.close()

65 changes: 21 additions & 44 deletions sctp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,27 @@
import sctp
import config
import json
import time

client_base = { "clients":
[
{
"address": "192.168.0.24",
"port": "2006",
"nickname": "bulasie",
"online": True
},
{
"address": "192.168.0.25",
"port": "2106",
"nickname": "bamboleo",
"online": False
},
]
}

sock = sctp.sctpsocket_tcp(socket.AF_INET)
sock.bind((host, config.MULTICAST_PORT))
sock.listen(1)
addr_server = ('127.0.0.1', 10000)
srv = sctp.sctpsocket_udp(socket.AF_INET)
tmp = sctp.event_subscribe(srv)
tmp.set_association(1)
tmp.set_data_io(1)
#srv.events = sctp.event_subscribe(srv)
srv.autoclose = 360
srv.bind(addr_server)
srv.listen(5)

while True:
#srv_to_cli, _addr_client = srv.accept()
try:
fromaddr, flags, msgret, notif = srv.sctp_recv(2048)
_ = "up" if notif.state==0 else "down"
print(f"assoc: {notif.assoc_id} -> {_}")
print('===============================================')
except:
pass

while True:
# wait for a connection
print ('waiting for a connection')
connection, client_address = sock.accept()
srv.close()

try:
# show who connected to us
print ('connection from', client_address)
# print connection
# receive the data in small chunks and print it
while True:
data, address = sock.recvfrom(999)
if data:
# output received data
print ("Data: %s" % data)
connection.sendall("We received " + str(len(data)) + " bytes from you")
print ("sending acknowledgement to ", address)
sock.sendto(json.dumps(client_base).encode('utf-8'), address)
else:
# no more data -- quit the loop
print ("no more data.")
break
finally:
# Clean up the connection
connection.close()

0 comments on commit fb41f36

Please sign in to comment.