我知道這個話題有點過時,但我想我會迴應它來幫助社區。
一言以蔽之:您正在使用pysctp
與插座包來創建客戶端或者服務器
- ;
- 因此,您可以像平常使用常規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()
有什麼問題嗎? – 2014-09-02 17:08:14