/**
* @brief - Send a message, using advanced SCTP features
* The sctp_sendmsg() function allows you to send extra information to a remote application.
* Using advanced SCTP features, you can send a message through a specified stream,
* pass extra opaque information to a remote application, or define a timeout for the particular message.
*
* @header - #include <netinet/sctp.h>
*
* @return ssize_t - The number of bytes sent, or -1 if an error occurs (errno is set).
*
* @ERRORS
* EBADF
* An invalid descriptor was specified.
* EDESTADDRREQ
* A destination address is required.
* EFAULT
* An invalid user space address was specified for a parameter.
* EMSGSIZE
* The socket requires that the message be sent atomically, but the size of the message made this impossible.
* ENOBUFS
* The system couldn't allocate an internal buffer. The operation may succeed when buffers become available.
* ENOTSOCK
* The argument s isn't a socket.
* EWOULDBLOCK
* The socket is marked nonblocking and the requested operation would block.
*/
ssize_t sctp_sendmsg(int s, /*! Socket descriptor. */
const void *msg, /*! Message to be sent. */
size_t len, /*! Length of the message. */
struct sockaddr *to, /*! Destination address of the message. */
socklen_t tolen, /*! Length of the destination address. */
uint32_t ppid, /*! An opaque unsigned value that is passed to the remote end in each user message.
The byte order issues are not accounted for and this information is passed opaquely
by the SCTP stack from one end to the other. */
uint32_t flags, /*! Flags composed of bitwise OR of these values:
MSG_UNORDERED
This flag requests the unordered delivery of the message.
If the flag is clear, the datagram is considered an ordered send.
MSG_ADDR_OVER
This flag, in one-to-many style, requests the SCTP stack to override the primary destination address.
MSG_ABORT
This flag causes the specified association to abort -- by sending
an ABORT message to the peer (one-to-many style only).
MSG_EOF
This flag invokes the SCTP graceful shutdown procedures on the specified association.
Graceful shutdown assures that all data enqueued by both endpoints is successfully
transmitted before closing the association (one-to-many style only). */
uint16_t stream_no, /*! Message stream number -- for the application to send a message.
If a sender specifies an invalid stream number, an error indication is returned and the call fails. */
uint32_t timetolive, /*! Message time to live in milliseconds.
The sending side expires the message within the specified time period
if the message has not been sent to the peer within this time period.
This value overrides any default value set using socket option.
If you use a value of 0, it indicates that no timeout should occur on this message. */
uint32_t context); /*! An opaque 32-bit context datum.
This value is passed back to the upper layer if an error occurs while sending a message,
and is retrieved with each undelivered message. */ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
//#include "common.h"
#define MAX_BUFFER 1024
#define MY_PORT_NUM 19000
#define LOCALTIME_STREAM 0
#define GMT_STREAM 1 int main()
{
int listenSock, connSock, ret;
struct sockaddr_in servaddr;
char buffer[MAX_BUFFER + ];
time_t currentTime; /* Create SCTP TCP-Style. Socket */
listenSock = socket( AF_INET, SOCK_STREAM, IPPROTO_SCTP ); // 注意这里的IPPROTO_SCTP
/* Accept connections from any interface */
bzero( (void *)&servaddr, sizeof(servaddr) );
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl( INADDR_ANY );
servaddr.sin_port = htons(MY_PORT_NUM);
/* Bind to the wildcard address (all) and MY_PORT_NUM */
ret = bind( listenSock,
(struct sockaddr *)&servaddr, sizeof(servaddr) );
/* Place the server socket into the listening state */
listen( listenSock, );
/* Server loop... */
while( )
{
/* Await a new client connection */
connSock = accept( listenSock,
(struct sockaddr *)NULL, (int *)NULL );
/* New client socket has connected */
/* Grab the current time */
currentTime = time(NULL);
/* Send local time on stream 0 (local time stream) */
snprintf( buffer, MAX_BUFFER, "%s\n", ctime(currentTime) );
ret = sctp_sendmsg( connSock,
(void *)buffer, (size_t)strlen(buffer),
NULL, , , , LOCALTIME_STREAM, , );
/* Send GMT on stream 1 (GMT stream) */
snprintf( buffer, MAX_BUFFER, "%s\n",
asctime( gmtime( currentTime ) ) );
ret = sctp_sendmsg( connSock,
(void *)buffer, (size_t)strlen(buffer),
NULL, , , , GMT_STREAM, , );
/* Close the client connection */
close( connSock );
}
return ;
} #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <arpa/inet.h>
//#include "common.h"
#define MAX_BUFFER 1024
#define MY_PORT_NUM 19000
#define LOCALTIME_STREAM 0
#define GMT_STREAM 1 int main()
{
int connSock, in, i, flags;
struct sockaddr_in servaddr;
struct sctp_sndrcvinfo sndrcvinfo;
struct sctp_event_subscribe events;
char buffer[MAX_BUFFER + ];
/* Create an SCTP TCP-Style. Socket */
connSock = socket( AF_INET, SOCK_STREAM, IPPROTO_SCTP );
/* Specify the peer endpoint to which we'll connect */
bzero( (void *)&servaddr, sizeof(servaddr) );
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(MY_PORT_NUM);
servaddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
/* Connect to the server */
connect( connSock, (struct sockaddr *)&servaddr, sizeof(servaddr) );
/* Enable receipt of SCTP Snd/Rcv Data via sctp_recvmsg */
memset( (void *)&events, , sizeof(events) );
events.sctp_data_io_event = ;
setsockopt( connSock, SOL_SCTP, SCTP_EVENTS,
(const void *)&events, sizeof(events) );
/* Expect two messages from the peer */
for (i = ; i < ; i++)
{
in = sctp_recvmsg( connSock, (void *)buffer, sizeof(buffer),
(struct sockaddr *)NULL, ,
&sndrcvinfo, &flags );
/* Null terminate the incoming string */
buffer[in] = ;
if (sndrcvinfo.sinfo_stream == LOCALTIME_STREAM)
{
printf("(Local) %s\n", buffer);
}
else if (sndrcvinfo.sinfo_stream == GMT_STREAM)
{
printf("(GMT ) %s\n", buffer);
}
}
/* Close our socket and exit */
close(connSock);
return ;
}

SCTP客户端与服务器的更多相关文章

  1. Android客户端与服务器

    就是普通的服务器端编程,还不用写界面,其实还比服务器编程简单一些.跟J2EE一样的服务器,你android这一方面只要用json或者gson直接拿数据,后台的话用tomcat接受请求操作数据,功能不复 ...

  2. Socket与SocketServer结合多线程实现多客户端与服务器通信

    需求说明:实现多客户端用户登录,实现多客户端登录一般都需要使用线程技术: (1)创建服务器端线程类,run()方法中实现对一个请求的响应处理: (2)修改服务器端代码,实现循环监听状态: (3)服务器 ...

  3. xmpp笔记2(客户端到服务器的例子)--xml

    xmpp( 客户端到服务器的例子 ) 1 步:客户端初始流给服务器: <stream:stream xmlns='jabber:client' xmlns:stream='http://ethe ...

  4. [ActionScript 3.0] NetConnection建立客户端与服务器的双向连接

    一个客户端与服务器之间的接口测试的工具 <?xml version="1.0" encoding="utf-8"?> <!--- - - - ...

  5. Oracle客户端与服务器字符集不统一的处理

    当Oracle客户端与服务器的字符集不统一时. 症状: 如:ORA-00283: ?????????? 提示信息中有好多问号. 解决方法: 1查询服务器的字符集: SQL> conn / as ...

  6. SignalR一个集成的客户端与服务器库。内部的两个对象类:PersistentConnection和Hub

    SignalR 将整个交换信息的行为封装得非常漂亮,客户端和服务器全部都使用 JSON 来沟通,在服务器端声明的所有 hub 的信息,都会一般生成 JavaScript 输出到客户端. 它是基于浏览器 ...

  7. Java实验四 TCP客户端和服务器的应用

    实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全 4.对通信内容进行摘要计算并验证 实验步骤 1.信息安全传送: 发送方A——————>接收方B A加密时,用B ...

  8. Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)

    转载地址:http://blog.csdn.net/mad1989/article/details/9147661 ZERO.前言 有关通信原理内容是在网上或百科整理得到,代码部分为本人所写,如果不当 ...

  9. nodejs的cs模式聊天客户端和服务器实现

    学习完nodejs的基础后,自然要写点东西练练手,以下是一个基于nodejs的cs模式的聊天软件代码: net模块是nodejs的网络编程必定用到的一个模块,对socket通信进行了封装 实现的功能: ...

随机推荐

  1. [学习笔记]平衡树(Splay)——旋转的灵魂舞蹈家

    1.简介 首先要知道什么是二叉查找树. 这是一棵二叉树,每个节点最多有一个左儿子,一个右儿子. 它能支持查找功能. 具体来说,每个儿子有一个权值,保证一个节点的左儿子权值小于这个节点,右儿子权值大于这 ...

  2. Python之旅:流程控制

    流程控制之if...else 每条if语句的核心都是一个值为True或False的表达式,这种表达式被称为条件测试. Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定 ...

  3. python检测服务器是否ping通

    好想在2014结束前再赶出个10篇博文来,~(>_<)~,不写博客真不是一个好兆头,至少说明对学习的欲望和对知识的研究都不是那么积极了,如果说这1天的时间我能赶出几篇精致的博文,你们信不信 ...

  4. Activiti工作流的应用示例

    1.新建流程模型 模型管理->模型工作区 点击"创建"后会立即跳转到"流程在线设计器"页面,请参考下一节 2.在线流程设计器 模型管理->模型工作区 ...

  5. C/C++:copy control (拷贝控制)

    前言:当定义一个类的时候,我们显示或者隐式地指定在此类型的对象拷贝,移动,赋值,销毁时做些什么,一个类通过定义五种特殊的成员函数来控制这些操作,包括拷贝构造函数,拷贝赋值运算符,移动构造函数,移动赋值 ...

  6. 虚拟机中在centos6.7环境下安装eclipse

    采用的是在线安装的方式,所以省去了很多配置环境变量的步骤,经过以下5步. 1,  yum install eclipse 2,  安装xmanager -> windows下远程eclipse可 ...

  7. python安装pip、numpy、scipy、statsmodels、pandas、matplotlib等

    1.安装python 2.安装numpy(开源的数值计算扩展,可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多. 很多库都是以此库为依 ...

  8. SiteMesh的使用--笔记

    本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处. http://www.cnblogs.com/king-xg/p/6472659.html Sitemesh ...

  9. 转:UIView之userInteractionEnabled属性介绍

    属性作用 该属性值为布尔类型,如属性本身的名称所释,该属性决定UIView是否接受并响应用户的交互. 当值设置为NO后,UIView会忽略那些原本应该发生在其自身的诸如touch和keyboard等用 ...

  10. 模块型css样式

    <div id="dowork"> <div id="dowork_on">۞作业进行中</div> <div id= ...