2

我一直在尝试测试 SCTP 以进行网络部署。我没有 SCTP 服务器或客户端,希望能够使用 pysctp。

我相当确定我的客户端代码正常工作。

def sctp_client ():
    print("SCTP client")
    sock = sctp.sctpsocket_tcp(socket.AF_INET)
    #sock.connect(('10.10.10.70',int(20003)))
    sock.connect(('10.10.10.41',int(21000)))
    print("Sending message")
    sock.sctp_send(msg='allowed')
    sock.shutdown(0)
    sock.close()

有没有人在服务器端使用 python sctp 模块运气好?先感谢您!

4

2 回答 2

4

我知道这个话题有点过时了,但我想我还是会回复它以帮助社区。

简而言之:

  • 您正在使用pysctpsockets 包来创建客户端或服务器;
  • 因此,您可以像通常使用常规 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() 
于 2015-06-05T09:39:24.377 回答
0

除非您需要特殊的 sctp_ 功能,否则您根本不需要 sctp 模块。只需将协议 132 用作 IPPROTO_SCTP(在我的 python3 套接字模块上定义,但不在我的 python2 套接字模块上定义),您就可以使用标准套接字模块中的套接字、绑定、侦听、连接、发送、接收、发送到、接收从、关闭。

我正在做一些 SCTP C 开发,我使用 python 来更好地理解没有 SCTP 模块的 SCTP 行为。

于 2021-01-10T10:16:48.650 回答