我知道这个话题有点过时了,但我想我还是会回复它以帮助社区。
简而言之:
- 您正在使用
pysctp
sockets 包来创建客户端或服务器;
- 因此,您可以像通常使用常规 TCP 连接一样创建服务器连接。
这里有一些代码可以帮助您入门,它有点冗长,但它说明了一个完整的连接、发送、接收和关闭连接。
您可以在您的开发计算机上运行它,然后使用ncat
(nmap
的实现netcat
) 之类的工具进行连接,即:ncat --sctp localhost 80
.
事不宜迟,这里的代码... HTH:
# Here are the packages that we need for our SCTP server
import socket
import sctp
from sctp import *
import threading
# Let's create a socket:
my_tcp_socket = sctpsocket_tcp(socket.AF_INET)
my_tcp_port = 80
# Here are a couple of parameters for the server
server_ip = "0.0.0.0"
backlog_conns = 3
# Let's set up a connection:
my_tcp_socket.events.clear()
my_tcp_socket.bind((server_ip, my_tcp_port))
my_tcp_socket.listen(backlog_conns)
# Here's a method for handling a connection:
def handle_client(client_socket):
client_socket.send("Howdy! What's your name?\n")
name = client_socket.recv(1024) # This might be a problem for someone with a reaaallly long name.
name = name.strip()
print "His name was Robert Paulson. Er, scratch that. It was {0}.".format(name)
client_socket.send("Thanks for calling, {0}. Bye, now.".format(name))
client_socket.close()
# Now, let's handle an actual connection:
while True:
client, addr = my_tcp_socket.accept()
print "Call from {0}:{1}".format(addr[0], addr[1])
client_handler = threading.Thread(target = handle_client,
args = (client,))
client_handler.start()