SCTP客户端与服务器

摘要:
套接字描述符*/28constvoid*消息,/*!要发送的消息*/29size_tlen,/*!消息长度*/30structsockaddr*到,/*!邮件的目的地地址*/31鞋垫,/*!目标地址的长度*/32uint32_ tppid,/*!每个用户消息中另一个不可忽略的签名值。33字节指令的发布是不可计数的,并且信息由CTC堆栈或其他方秘密传递34。*/35uint32_tflags,/*!标志由按位或值组成:36MSG_ UNORDERED37此标志请求无需发送消息。38如果标志清晰,则数据将被视为无需发送。39MSG_ ADDR_ OVER40此标志在一个或多个样式中请求SCTP堆栈覆盖主目标地址。41MSG_ ABORT42此标志通过向对等方发送43 ABORT消息(仅一对多样式),导致指定的关联中止。44MSG_ EOF45此标志在指定的关联中调用CTC优雅关闭过程。46正常关闭可确保在取消关联(仅一对多样式)之前成功47传输由端点查询的所有数据*/48uint16_ tstream_否,/*!消息流编号--用于发送消息。49如果指定的流编号无效,则返回错误指示,调用失败*/50uint32_timetoolive,/*!
  1 /**
  2  * @brief - Send a message, using advanced SCTP features
  3  * The sctp_sendmsg() function allows you to send extra information to a remote application.
  4  * Using advanced SCTP features, you can send a message through a specified stream,
  5  * pass extra opaque information to a remote application, or define a timeout for the particular message.
  6  *
  7  * @header - #include <netinet/sctp.h>
  8  *
  9  * @return ssize_t - The number of bytes sent, or -1 if an error occurs (errno is set).
 10  *
 11  * @ERRORS
 12  * EBADF
 13  *     An invalid descriptor was specified.
 14  * EDESTADDRREQ
 15  *     A destination address is required.
 16  * EFAULT
 17  *     An invalid user space address was specified for a parameter.
 18  * EMSGSIZE
 19  *     The socket requires that the message be sent atomically, but the size of the message made this impossible.
 20  * ENOBUFS
 21  *     The system couldn't allocate an internal buffer. The operation may succeed when buffers become available.
 22  * ENOTSOCK
 23  *     The argument s isn't a socket.
 24  * EWOULDBLOCK
 25  *     The socket is marked nonblocking and the requested operation would block.
 26  */
 27 ssize_t sctp_sendmsg(int s,                             /*! Socket descriptor. */
 28                      const void *msg,         /*! Message to be sent. */
 29                      size_t len,                     /*! Length of the message. */
 30                      struct sockaddr *to, /*! Destination address of the message. */
 31                      socklen_t tolen,         /*! Length of the destination address. */
 32                      uint32_t ppid,             /*! An opaque unsigned value that is passed to the remote end in each user message.
 33                                                                         The byte order issues are not accounted for and this information is passed opaquely
 34                                                                           by the SCTP stack from one end to the other. */
 35                      uint32_t flags,             /*! Flags composed of bitwise OR of these values:
 36                                                                          MSG_UNORDERED
 37                                                                              This flag requests the unordered delivery of the message.
 38                                                                              If the flag is clear, the datagram is considered an ordered send.
 39                                                                          MSG_ADDR_OVER
 40                                                                              This flag, in one-to-many style, requests the SCTP stack to override the primary destination address.
 41                                                                          MSG_ABORT
 42                                                                              This flag causes the specified association to abort -- by sending
 43                                                                              an ABORT message to the peer (one-to-many style only).
 44                                                                          MSG_EOF
 45                                                                              This flag invokes the SCTP graceful shutdown procedures on the specified association.
 46                                                                              Graceful shutdown assures that all data enqueued by both endpoints is successfully
 47                                                                              transmitted before closing the association (one-to-many style only). */
 48                      uint16_t stream_no,     /*! Message stream number -- for the application to send a message.
 49                                                                          If a sender specifies an invalid stream number, an error indication is returned and the call fails. */
 50                      uint32_t timetolive,    /*! Message time to live in milliseconds.
 51                                                                          The sending side expires the message within the specified time period
 52                                                                          if the message has not been sent to the peer within this time period.
 53                                                                          This value overrides any default value set using socket option.
 54                                                                          If you use a value of 0, it indicates that no timeout should occur on this message. */
 55                      uint32_t context);        /*! An opaque 32-bit context datum.
 56                                                                          This value is passed back to the upper layer if an error occurs while sending a message,
 57                                                                          and is retrieved with each undelivered message. */
 58 
 59 #include <stdio.h>  
 60 #include <stdlib.h>  
 61 #include <string.h>  
 62 #include <unistd.h>  
 63 #include <time.h>  
 64 #include <sys/socket.h>  
 65 #include <sys/types.h>  
 66 #include <netinet/in.h>  
 67 #include <netinet/sctp.h>  
 68 //#include "common.h"  
 69 #define MAX_BUFFER  1024  
 70 #define MY_PORT_NUM 19000  
 71 #define LOCALTIME_STREAM    0  
 72 #define GMT_STREAM   1  
 73 
 74 int main()
 75 {
 76     int listenSock, connSock, ret;
 77     struct sockaddr_in servaddr;
 78     char buffer[MAX_BUFFER + 1];
 79     time_t currentTime;
 80 
 81     /* Create SCTP TCP-Style. Socket */
 82     listenSock = socket( AF_INET, SOCK_STREAM, IPPROTO_SCTP ); // 注意这里的IPPROTO_SCTP
 83     /* Accept connections from any interface */
 84     bzero( (void *)&servaddr, sizeof(servaddr) );
 85     servaddr.sin_family = AF_INET;
 86     servaddr.sin_addr.s_addr = htonl( INADDR_ANY );
 87     servaddr.sin_port = htons(MY_PORT_NUM);
 88     /* Bind to the wildcard address (all) and MY_PORT_NUM */
 89     ret = bind( listenSock,
 90                 (struct sockaddr *)&servaddr, sizeof(servaddr) );
 91     /* Place the server socket into the listening state */
 92     listen( listenSock, 5 );
 93     /* Server loop... */
 94     while( 1 )
 95     {
 96         /* Await a new client connection */
 97         connSock = accept( listenSock,
 98                            (struct sockaddr *)NULL, (int *)NULL );
 99         /* New client socket has connected */
100         /* Grab the current time */
101         currentTime = time(NULL);
102         /* Send local time on stream 0 (local time stream) */
103         snprintf( buffer, MAX_BUFFER, "%s
", ctime(currentTime) );
104         ret = sctp_sendmsg( connSock,
105                             (void *)buffer, (size_t)strlen(buffer),
106                             NULL, 0, 0, 0, LOCALTIME_STREAM, 0, 0 );
107         /* Send GMT on stream 1 (GMT stream) */
108         snprintf( buffer, MAX_BUFFER, "%s
",
109                   asctime( gmtime( currentTime ) ) );
110         ret = sctp_sendmsg( connSock,
111                             (void *)buffer, (size_t)strlen(buffer),
112                             NULL, 0, 0, 0, GMT_STREAM, 0, 0 );
113         /* Close the client connection */
114         close( connSock );
115     }
116     return 0;
117 }
118 
119 
120 #include <stdio.h>  
121 #include <stdlib.h>  
122 #include <string.h>  
123 #include <unistd.h>  
124 #include <sys/socket.h>  
125 #include <sys/types.h>  
126 #include <netinet/in.h>  
127 #include <netinet/sctp.h>  
128 #include <arpa/inet.h>  
129 //#include "common.h"  
130 #define MAX_BUFFER  1024  
131 #define MY_PORT_NUM 19000  
132 #define LOCALTIME_STREAM    0  
133 #define GMT_STREAM   1  
134 
135 int main()
136 {
137     int connSock, in, i, flags;
138     struct sockaddr_in servaddr;
139     struct sctp_sndrcvinfo sndrcvinfo;
140     struct sctp_event_subscribe events;
141     char buffer[MAX_BUFFER + 1];
142     /* Create an SCTP TCP-Style. Socket */
143     connSock = socket( AF_INET, SOCK_STREAM, IPPROTO_SCTP );
144     /* Specify the peer endpoint to which we'll connect */
145     bzero( (void *)&servaddr, sizeof(servaddr) );
146     servaddr.sin_family = AF_INET;
147     servaddr.sin_port = htons(MY_PORT_NUM);
148     servaddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
149     /* Connect to the server */
150     connect( connSock, (struct sockaddr *)&servaddr, sizeof(servaddr) );
151     /* Enable receipt of SCTP Snd/Rcv Data via sctp_recvmsg */
152     memset( (void *)&events, 0, sizeof(events) );
153     events.sctp_data_io_event = 1;
154     setsockopt( connSock, SOL_SCTP, SCTP_EVENTS,
155                 (const void *)&events, sizeof(events) );
156     /* Expect two messages from the peer */
157     for (i = 0 ; i < 2 ; i++)
158     {
159         in = sctp_recvmsg( connSock, (void *)buffer, sizeof(buffer),
160                            (struct sockaddr *)NULL, 0,
161                            &sndrcvinfo, &flags );
162         /* Null terminate the incoming string */
163         buffer[in] = 0;
164         if (sndrcvinfo.sinfo_stream == LOCALTIME_STREAM)
165         {
166             printf("(Local) %s
", buffer);
167         }
168         else if (sndrcvinfo.sinfo_stream == GMT_STREAM)
169         {
170             printf("(GMT  ) %s
", buffer);
171         }
172     }
173     /* Close our socket and exit */
174     close(connSock);
175     return 0;
176 }

免责声明:文章转载自《SCTP客户端与服务器》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Android_UI_点击按钮切换背景效果实现Koa 框架整理下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

嵌入式linux GUI--DirectFB + GTK至尊秘笈

我开始在x86上构建GTK环境。首先,我选择了最新版本。然后,我看到了GTK在帧缓冲区上以两种模式运行的介绍:DirectFB和linuxfb,而Linuxfb项目似乎已经停止。主要方向是DirectFB。后来,我找到了一个DirectFB+GTK的英文文档,它基本上使用了最新版本。许多软件包可以使用系统自己的,因此您可以编译必要的源代码。一开始,编译并不成...

lstm与bilstm

背景学习和整理lstm和bilstm的理论知识。对于有序数据,bilstm具有数据信息的长、短存储功能。bilstm:它是前lstm和后lstm的组合。为什么需要lstm?它可以更好地捕捉远距离的依赖性。通过培训,你可以了解哪些信息需要记住,哪些信息需要忘记;我不认为他喜欢“否定”,即句子的情感分析是贬义的。“lstm建模有一个问题,它不能从后面到前面对信息...

使用jsPlumb插件实现动态连线功能

jsPlumb是一个强大的JavaScript连线库,它可以将html中的元素用箭头、曲线、直线等连接起来,适用于开发Web上的图表、建模工具等,其实jsPlumb可能主要是用来做流程图的,它在实现这方面的功能上非常强大,我在项目中只使用了它少部分功能,来实现项目中连线的效果。...

SAP OBA1 外币评估是基于财务目的,为了不影响报表而做的估算值,在月末进行评估,在下月初进行冲回。

评估报告按行项目显示结果。4.评估策略外币的未清项评估有三种策略:1)期末评估,下期初冲回。因此目前每年底改变外币汇率时进行外币余额和未清项的评估,不冲回。②资产负债表指定日,一般是一年的最后一天。③资产负债表准备评估。如果选择该项,则视为年结评估,不能产生冲销凭证。外币未清项评估是按借贷分别统计后做的调整凭证。...

mysql状态查看 QPS/TPS/缓存命中率查看

showglobalstatusslike'Com_ commit';showstatslike“无缓冲池读取%”;Thread_cache_Hits=(1-Thread_created/connections)*100%(8)锁定状态mysql&gt;showstatslike“Binlog_缓存%”;...

2.页面绘制和引入组件库uView

文本+背景色的形式,而不是横幅图的形式,可以节省未来的工作量。在index.vue中,关于开关的代码:EFGHIJKLMNOPQRSTUWXYZB˃DEFGHIJKLNNOPQRSTUVWXYZEFGHIJKLMNOPQRSTUVWXYZ导出默认值{data(){return{}},onLoad()},方法:{}}。横幅{width:100%;height:...